blob: d6b969a3b1abb911d1d0c310f951bc3bae152c24 [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 Tran0059b692023-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 {
63 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) {
64 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")
Colin Cross98be1bb2019-12-13 20:41:13 -0800657 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900658
Colin Cross45bce852021-11-11 22:47:54 -0800659 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700660 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900661 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900662}
663
664func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400665 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800666 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900667 cc_library {
668 name: "libvndk",
669 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900670 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900671 vndk: {
672 enabled: true,
673 },
674 nocrt: true,
675 }
676
677 cc_library {
678 name: "libvndk_sp",
679 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900680 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900681 vndk: {
682 enabled: true,
683 support_system_process: true,
684 },
685 nocrt: true,
686 }
687
688 cc_library {
689 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900690 vendor_available: true,
691 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900692 vndk: {
693 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900694 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900695 },
696 nocrt: true,
697 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900698
Colin Crosse4e44bc2020-12-28 13:50:21 -0800699 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900700 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800701 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900702 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800703 `
704
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000705 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800706 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900707 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800708 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
709
710 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
711
712 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900713
Jooyung Han2216fb12019-11-06 16:46:15 +0900714 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900715}
716
Chris Parsons79d66a52020-06-05 17:26:16 -0400717func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400718 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400719 bp := `
720 cc_test_library {
721 name: "test_lib",
722 srcs: ["test_lib.cpp"],
723 gtest: false,
724 }
725
726 cc_test {
727 name: "main_test",
728 data_libs: ["test_lib"],
729 gtest: false,
730 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400731 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400732
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000733 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400734 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900735 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400736 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
737
738 ctx := testCcWithConfig(t, config)
739 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
740 testBinary := module.(*Module).linker.(*testBinary)
741 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
742 if err != nil {
743 t.Errorf("Expected cc_test to produce output files, error: %s", err)
744 return
745 }
746 if len(outputFiles) != 1 {
747 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
748 return
749 }
750 if len(testBinary.dataPaths()) != 1 {
751 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
752 return
753 }
754
755 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400756 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400757
758 if !strings.HasSuffix(outputPath, "/main_test") {
759 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
760 return
761 }
762 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
763 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
764 return
765 }
766}
767
Chris Parsons216e10a2020-07-09 17:12:52 -0400768func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400769 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400770 bp := `
771 cc_test_library {
772 name: "test_lib",
773 srcs: ["test_lib.cpp"],
774 relative_install_path: "foo/bar/baz",
775 gtest: false,
776 }
777
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400778 cc_binary {
779 name: "test_bin",
780 relative_install_path: "foo/bar/baz",
781 compile_multilib: "both",
782 }
783
Chris Parsons216e10a2020-07-09 17:12:52 -0400784 cc_test {
785 name: "main_test",
786 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400787 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400788 gtest: false,
789 }
790 `
791
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000792 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400793 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900794 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400795 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
796
797 ctx := testCcWithConfig(t, config)
798 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
799 testBinary := module.(*Module).linker.(*testBinary)
800 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
801 if err != nil {
802 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
803 }
804 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400805 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400806 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400807 if len(testBinary.dataPaths()) != 2 {
808 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400809 }
810
811 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400812
813 if !strings.HasSuffix(outputPath, "/main_test") {
814 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
815 }
Colin Crossaa255532020-07-03 13:18:24 -0700816 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400817 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
818 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400819 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400820 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400821 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
822 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
823 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
824 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400825}
826
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000827func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400828 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000829 bp := `
830 cc_test {
831 name: "main_test",
832 srcs: ["main_test.cpp"],
833 test_suites: [
834 "suite_1",
835 "suite_2",
836 ],
837 gtest: false,
838 }
839 `
840
841 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
842 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
843
844 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
845 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
846 if len(compatEntries) != 2 {
847 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
848 }
849 if compatEntries[0] != "suite_1" {
850 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
851 " but was '%s'", compatEntries[0])
852 }
853 if compatEntries[1] != "suite_2" {
854 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
855 " but was '%s'", compatEntries[1])
856 }
857}
858
859func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400860 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000861 bp := `
862 cc_test_library {
863 name: "main_test_lib",
864 srcs: ["main_test_lib.cpp"],
865 test_suites: [
866 "suite_1",
867 "suite_2",
868 ],
869 gtest: false,
870 }
871 `
872
873 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
874 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
875
876 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
877 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
878 if len(compatEntries) != 2 {
879 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
880 }
881 if compatEntries[0] != "suite_1" {
882 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
883 " but was '%s'", compatEntries[0])
884 }
885 if compatEntries[1] != "suite_2" {
886 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
887 " but was '%s'", compatEntries[1])
888 }
889}
890
Jooyung Han0302a842019-10-30 18:43:49 +0900891func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400892 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900893 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900894 cc_library {
895 name: "libvndk",
896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900897 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900898 vndk: {
899 enabled: true,
900 },
901 nocrt: true,
902 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900903 cc_library {
904 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900905 vendor_available: true,
906 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900907 vndk: {
908 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900909 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900910 },
911 nocrt: true,
912 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800913
914 cc_library {
915 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700916 llndk: {
917 symbol_file: "libllndk.map.txt",
918 export_llndk_headers: ["libllndk_headers"],
919 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800920 }
921
Colin Cross627280f2021-04-26 16:53:58 -0700922 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800923 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700924 llndk: {
925 symbol_file: "libllndk.map.txt",
926 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800927 export_include_dirs: ["include"],
928 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900929 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900930
931 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
932 "LLNDK: libc.so",
933 "LLNDK: libdl.so",
934 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800935 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900936 "LLNDK: libm.so",
937 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900938 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900939 "VNDK-core: libvndk.so",
940 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900941 "VNDK-private: libvndk-private.so",
942 "VNDK-product: libc++.so",
943 "VNDK-product: libvndk-private.so",
944 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900945 })
Logan Chienf3511742017-10-31 18:04:35 +0800946}
947
Justin Yun63e9ec72020-10-29 16:49:43 +0900948func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400949 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900950 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900951 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900952 cc_library {
953 name: "libvndk",
954 vndk: {
955 enabled: true,
956 },
957 nocrt: true,
958 }
959 `)
960
Justin Yunc0d8c492021-01-07 17:45:31 +0900961 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900962 cc_library {
963 name: "libvndk",
964 product_available: true,
965 vndk: {
966 enabled: true,
967 },
968 nocrt: true,
969 }
970 `)
971
Justin Yun6977e8a2020-10-29 18:24:11 +0900972 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
973 cc_library {
974 name: "libvndkprop",
975 vendor_available: true,
976 product_available: true,
977 vndk: {
978 enabled: true,
979 },
980 nocrt: true,
981 target: {
982 vendor: {
983 cflags: ["-DTEST",],
984 },
985 },
986 }
987 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900988}
989
Logan Chiend3c59a22018-03-29 14:08:15 +0800990func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400991 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800992 // Check whether an error is emitted when a VNDK lib depends on a system lib.
993 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
994 cc_library {
995 name: "libvndk",
996 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900997 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800998 vndk: {
999 enabled: true,
1000 },
1001 shared_libs: ["libfwk"], // Cause error
1002 nocrt: true,
1003 }
1004
1005 cc_library {
1006 name: "libfwk",
1007 nocrt: true,
1008 }
1009 `)
1010
1011 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
1012 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1013 cc_library {
1014 name: "libvndk",
1015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001016 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001017 vndk: {
1018 enabled: true,
1019 },
1020 shared_libs: ["libvendor"], // Cause error
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libvendor",
1026 vendor: true,
1027 nocrt: true,
1028 }
1029 `)
1030
1031 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1032 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1033 cc_library {
1034 name: "libvndk_sp",
1035 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001036 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001037 vndk: {
1038 enabled: true,
1039 support_system_process: true,
1040 },
1041 shared_libs: ["libfwk"], // Cause error
1042 nocrt: true,
1043 }
1044
1045 cc_library {
1046 name: "libfwk",
1047 nocrt: true,
1048 }
1049 `)
1050
1051 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1052 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1053 cc_library {
1054 name: "libvndk_sp",
1055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001056 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001057 vndk: {
1058 enabled: true,
1059 support_system_process: true,
1060 },
1061 shared_libs: ["libvendor"], // Cause error
1062 nocrt: true,
1063 }
1064
1065 cc_library {
1066 name: "libvendor",
1067 vendor: true,
1068 nocrt: true,
1069 }
1070 `)
1071
1072 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1073 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1074 cc_library {
1075 name: "libvndk_sp",
1076 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001077 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001078 vndk: {
1079 enabled: true,
1080 support_system_process: true,
1081 },
1082 shared_libs: ["libvndk"], // Cause error
1083 nocrt: true,
1084 }
1085
1086 cc_library {
1087 name: "libvndk",
1088 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001089 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001090 vndk: {
1091 enabled: true,
1092 },
1093 nocrt: true,
1094 }
1095 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001096
1097 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1098 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1099 cc_library {
1100 name: "libvndk",
1101 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001102 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001103 vndk: {
1104 enabled: true,
1105 },
1106 shared_libs: ["libnonvndk"],
1107 nocrt: true,
1108 }
1109
1110 cc_library {
1111 name: "libnonvndk",
1112 vendor_available: true,
1113 nocrt: true,
1114 }
1115 `)
1116
1117 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1118 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1119 cc_library {
1120 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001121 vendor_available: true,
1122 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001123 vndk: {
1124 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001125 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001126 },
1127 shared_libs: ["libnonvndk"],
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libnonvndk",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136 `)
1137
1138 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1139 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1140 cc_library {
1141 name: "libvndksp",
1142 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001143 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001144 vndk: {
1145 enabled: true,
1146 support_system_process: true,
1147 },
1148 shared_libs: ["libnonvndk"],
1149 nocrt: true,
1150 }
1151
1152 cc_library {
1153 name: "libnonvndk",
1154 vendor_available: true,
1155 nocrt: true,
1156 }
1157 `)
1158
1159 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1160 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1161 cc_library {
1162 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001163 vendor_available: true,
1164 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001165 vndk: {
1166 enabled: true,
1167 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001168 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001169 },
1170 shared_libs: ["libnonvndk"],
1171 nocrt: true,
1172 }
1173
1174 cc_library {
1175 name: "libnonvndk",
1176 vendor_available: true,
1177 nocrt: true,
1178 }
1179 `)
1180}
1181
1182func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001183 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001184 // okay to link : LLNDK -> double_loadable VNDK
1185 testCc(t, `
1186 cc_library {
1187 name: "libllndk",
1188 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001189 llndk: {
1190 symbol_file: "libllndk.map.txt",
1191 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001192 }
1193
1194 cc_library {
1195 name: "libdoubleloadable",
1196 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001197 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001198 vndk: {
1199 enabled: true,
1200 },
1201 double_loadable: true,
1202 }
1203 `)
1204 // okay to link : LLNDK -> VNDK-SP
1205 testCc(t, `
1206 cc_library {
1207 name: "libllndk",
1208 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001209 llndk: {
1210 symbol_file: "libllndk.map.txt",
1211 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001212 }
1213
1214 cc_library {
1215 name: "libvndksp",
1216 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001217 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001218 vndk: {
1219 enabled: true,
1220 support_system_process: true,
1221 },
1222 }
1223 `)
1224 // okay to link : double_loadable -> double_loadable
1225 testCc(t, `
1226 cc_library {
1227 name: "libdoubleloadable1",
1228 shared_libs: ["libdoubleloadable2"],
1229 vendor_available: true,
1230 double_loadable: true,
1231 }
1232
1233 cc_library {
1234 name: "libdoubleloadable2",
1235 vendor_available: true,
1236 double_loadable: true,
1237 }
1238 `)
1239 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1240 testCc(t, `
1241 cc_library {
1242 name: "libdoubleloadable",
1243 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001244 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001245 vndk: {
1246 enabled: true,
1247 },
1248 double_loadable: true,
1249 shared_libs: ["libnondoubleloadable"],
1250 }
1251
1252 cc_library {
1253 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001254 vendor_available: true,
1255 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001256 vndk: {
1257 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001258 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001259 },
1260 double_loadable: true,
1261 }
1262 `)
1263 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1264 testCc(t, `
1265 cc_library {
1266 name: "libllndk",
1267 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001268 llndk: {
1269 symbol_file: "libllndk.map.txt",
1270 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001271 }
1272
1273 cc_library {
1274 name: "libcoreonly",
1275 shared_libs: ["libvendoravailable"],
1276 }
1277
1278 // indirect dependency of LLNDK
1279 cc_library {
1280 name: "libvendoravailable",
1281 vendor_available: true,
1282 double_loadable: true,
1283 }
1284 `)
1285}
1286
1287func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001288 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001289 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1290 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1291 cc_library {
1292 name: "libllndk",
1293 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001294 llndk: {
1295 symbol_file: "libllndk.map.txt",
1296 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001297 }
1298
1299 cc_library {
1300 name: "libnondoubleloadable",
1301 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001302 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001303 vndk: {
1304 enabled: true,
1305 },
1306 }
1307 `)
1308
1309 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1310 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1311 cc_library {
1312 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001313 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001314 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001315 llndk: {
1316 symbol_file: "libllndk.map.txt",
1317 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001318 }
1319
1320 cc_library {
1321 name: "libnondoubleloadable",
1322 vendor_available: true,
1323 }
1324 `)
1325
Jooyung Hana70f0672019-01-18 15:20:43 +09001326 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1327 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1328 cc_library {
1329 name: "libllndk",
1330 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001331 llndk: {
1332 symbol_file: "libllndk.map.txt",
1333 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001334 }
1335
1336 cc_library {
1337 name: "libcoreonly",
1338 shared_libs: ["libvendoravailable"],
1339 }
1340
1341 // indirect dependency of LLNDK
1342 cc_library {
1343 name: "libvendoravailable",
1344 vendor_available: true,
1345 }
1346 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001347
1348 // The error is not from 'client' but from 'libllndk'
1349 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1350 cc_library {
1351 name: "client",
1352 vendor_available: true,
1353 double_loadable: true,
1354 shared_libs: ["libllndk"],
1355 }
1356 cc_library {
1357 name: "libllndk",
1358 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001359 llndk: {
1360 symbol_file: "libllndk.map.txt",
1361 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001362 }
1363 cc_library {
1364 name: "libnondoubleloadable",
1365 vendor_available: true,
1366 }
1367 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001368}
1369
Jooyung Han479ca172020-10-19 18:51:07 +09001370func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001371 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001372 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1373 cc_library {
1374 name: "libvndksp",
1375 shared_libs: ["libanothervndksp"],
1376 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001377 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001378 vndk: {
1379 enabled: true,
1380 support_system_process: true,
1381 }
1382 }
1383
1384 cc_library {
1385 name: "libllndk",
1386 shared_libs: ["libanothervndksp"],
1387 }
1388
Jooyung Han479ca172020-10-19 18:51:07 +09001389 cc_library {
1390 name: "libanothervndksp",
1391 vendor_available: true,
1392 }
1393 `)
1394}
1395
Logan Chienf3511742017-10-31 18:04:35 +08001396func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001397 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001398 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001399 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001400 cc_library {
1401 name: "libvndk",
1402 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001403 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001404 vndk: {
1405 enabled: true,
1406 },
1407 nocrt: true,
1408 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001409 cc_library {
1410 name: "libvndk2",
1411 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001412 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001413 vndk: {
1414 enabled: true,
1415 },
1416 target: {
1417 vendor: {
1418 suffix: "-suffix",
1419 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001420 product: {
1421 suffix: "-suffix",
1422 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001423 },
1424 nocrt: true,
1425 }
Logan Chienf3511742017-10-31 18:04:35 +08001426
1427 cc_library {
1428 name: "libvndk_ext",
1429 vendor: true,
1430 vndk: {
1431 enabled: true,
1432 extends: "libvndk",
1433 },
1434 nocrt: true,
1435 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001436
1437 cc_library {
1438 name: "libvndk2_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 extends: "libvndk2",
1443 },
1444 nocrt: true,
1445 }
Logan Chienf3511742017-10-31 18:04:35 +08001446
Justin Yun0ecf0b22020-02-28 15:07:59 +09001447 cc_library {
1448 name: "libvndk_ext_product",
1449 product_specific: true,
1450 vndk: {
1451 enabled: true,
1452 extends: "libvndk",
1453 },
1454 nocrt: true,
1455 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001456
Justin Yun0ecf0b22020-02-28 15:07:59 +09001457 cc_library {
1458 name: "libvndk2_ext_product",
1459 product_specific: true,
1460 vndk: {
1461 enabled: true,
1462 extends: "libvndk2",
1463 },
1464 nocrt: true,
1465 }
1466 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001467 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001468 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1469 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001470 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001471
1472 ctx := testCcWithConfig(t, config)
1473
1474 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1475 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1476
1477 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1478 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1479
1480 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1481 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001482}
1483
Logan Chiend3c59a22018-03-29 14:08:15 +08001484func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001485 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001486 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1487 ctx := testCcNoVndk(t, `
1488 cc_library {
1489 name: "libvndk",
1490 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001491 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001492 vndk: {
1493 enabled: true,
1494 },
1495 nocrt: true,
1496 }
1497
1498 cc_library {
1499 name: "libvndk_ext",
1500 vendor: true,
1501 vndk: {
1502 enabled: true,
1503 extends: "libvndk",
1504 },
1505 nocrt: true,
1506 }
1507 `)
1508
1509 // Ensures that the core variant of "libvndk_ext" can be found.
1510 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1511 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1512 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1513 }
1514}
1515
Justin Yun0ecf0b22020-02-28 15:07:59 +09001516func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001517 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001518 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001519 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001520 cc_library {
1521 name: "libvndk",
1522 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001523 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001524 vndk: {
1525 enabled: true,
1526 },
1527 nocrt: true,
1528 }
1529
1530 cc_library {
1531 name: "libvndk_ext_product",
1532 product_specific: true,
1533 vndk: {
1534 enabled: true,
1535 extends: "libvndk",
1536 },
1537 nocrt: true,
1538 }
1539 `)
1540
1541 // Ensures that the core variant of "libvndk_ext_product" can be found.
1542 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1543 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1544 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1545 }
1546}
1547
Logan Chienf3511742017-10-31 18:04:35 +08001548func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001549 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001550 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001551 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001552 cc_library {
1553 name: "libvndk",
1554 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001555 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001556 vndk: {
1557 enabled: true,
1558 },
1559 nocrt: true,
1560 }
1561
1562 cc_library {
1563 name: "libvndk_ext",
1564 vndk: {
1565 enabled: true,
1566 extends: "libvndk",
1567 },
1568 nocrt: true,
1569 }
1570 `)
1571
1572 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1573 cc_library {
1574 name: "libvndk",
1575 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001576 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001577 vndk: {
1578 enabled: true,
1579 },
1580 nocrt: true,
1581 }
1582
1583 cc_library {
1584 name: "libvndk_ext",
1585 vendor: true,
1586 vndk: {
1587 enabled: true,
1588 },
1589 nocrt: true,
1590 }
1591 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001592
1593 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1594 cc_library {
1595 name: "libvndk",
1596 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001597 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001598 vndk: {
1599 enabled: true,
1600 },
1601 nocrt: true,
1602 }
1603
1604 cc_library {
1605 name: "libvndk_ext_product",
1606 product_specific: true,
1607 vndk: {
1608 enabled: true,
1609 },
1610 nocrt: true,
1611 }
1612 `)
1613
1614 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1615 cc_library {
1616 name: "libvndk",
1617 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001618 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001619 vndk: {
1620 enabled: true,
1621 },
1622 nocrt: true,
1623 }
1624
1625 cc_library {
1626 name: "libvndk_ext_product",
1627 product_specific: true,
1628 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001629 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001630 vndk: {
1631 enabled: true,
1632 extends: "libvndk",
1633 },
1634 nocrt: true,
1635 }
1636 `)
Logan Chienf3511742017-10-31 18:04:35 +08001637}
1638
1639func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001640 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001641 // This test ensures an error is emitted for inconsistent support_system_process.
1642 testCcError(t, "module \".*\" with mismatched support_system_process", `
1643 cc_library {
1644 name: "libvndk",
1645 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001646 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001647 vndk: {
1648 enabled: true,
1649 },
1650 nocrt: true,
1651 }
1652
1653 cc_library {
1654 name: "libvndk_sp_ext",
1655 vendor: true,
1656 vndk: {
1657 enabled: true,
1658 extends: "libvndk",
1659 support_system_process: true,
1660 },
1661 nocrt: true,
1662 }
1663 `)
1664
1665 testCcError(t, "module \".*\" with mismatched support_system_process", `
1666 cc_library {
1667 name: "libvndk_sp",
1668 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001669 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001670 vndk: {
1671 enabled: true,
1672 support_system_process: true,
1673 },
1674 nocrt: true,
1675 }
1676
1677 cc_library {
1678 name: "libvndk_ext",
1679 vendor: true,
1680 vndk: {
1681 enabled: true,
1682 extends: "libvndk_sp",
1683 },
1684 nocrt: true,
1685 }
1686 `)
1687}
1688
1689func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001690 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001691 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001692 // with `private: true`.
1693 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001694 cc_library {
1695 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001696 vendor_available: true,
1697 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001698 vndk: {
1699 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001700 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001701 },
1702 nocrt: true,
1703 }
1704
1705 cc_library {
1706 name: "libvndk_ext",
1707 vendor: true,
1708 vndk: {
1709 enabled: true,
1710 extends: "libvndk",
1711 },
1712 nocrt: true,
1713 }
1714 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001715
Justin Yunfd9e8042020-12-23 18:23:14 +09001716 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001717 cc_library {
1718 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001719 vendor_available: true,
1720 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001721 vndk: {
1722 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001723 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001724 },
1725 nocrt: true,
1726 }
1727
1728 cc_library {
1729 name: "libvndk_ext_product",
1730 product_specific: true,
1731 vndk: {
1732 enabled: true,
1733 extends: "libvndk",
1734 },
1735 nocrt: true,
1736 }
1737 `)
Logan Chienf3511742017-10-31 18:04:35 +08001738}
1739
Logan Chiend3c59a22018-03-29 14:08:15 +08001740func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001741 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001742 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001743 testCc(t, `
1744 cc_library {
1745 name: "libvndk",
1746 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001747 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001748 vndk: {
1749 enabled: true,
1750 },
1751 nocrt: true,
1752 }
1753
1754 cc_library {
1755 name: "libvndk_ext",
1756 vendor: true,
1757 vndk: {
1758 enabled: true,
1759 extends: "libvndk",
1760 },
1761 nocrt: true,
1762 }
1763
1764 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001765 name: "libvndk_sp",
1766 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001767 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001768 vndk: {
1769 enabled: true,
1770 support_system_process: true,
1771 },
1772 nocrt: true,
1773 }
1774
1775 cc_library {
1776 name: "libvndk_sp_ext",
1777 vendor: true,
1778 vndk: {
1779 enabled: true,
1780 extends: "libvndk_sp",
1781 support_system_process: true,
1782 },
1783 nocrt: true,
1784 }
1785
1786 cc_library {
1787 name: "libvendor",
1788 vendor: true,
1789 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1790 nocrt: true,
1791 }
1792 `)
1793}
1794
Logan Chiend3c59a22018-03-29 14:08:15 +08001795func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001796 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001797 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001798 testCc(t, `
1799 cc_library {
1800 name: "libvndk",
1801 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001802 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001803 vndk: {
1804 enabled: true,
1805 },
1806 nocrt: true,
1807 }
1808
1809 cc_library {
1810 name: "libvndk_ext",
1811 vendor: true,
1812 vndk: {
1813 enabled: true,
1814 extends: "libvndk",
1815 },
1816 shared_libs: ["libvendor"],
1817 nocrt: true,
1818 }
1819
1820 cc_library {
1821 name: "libvendor",
1822 vendor: true,
1823 nocrt: true,
1824 }
1825 `)
Logan Chienf3511742017-10-31 18:04:35 +08001826
Logan Chiend3c59a22018-03-29 14:08:15 +08001827 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1828 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001829 cc_library {
1830 name: "libvndk_sp",
1831 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001832 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001833 vndk: {
1834 enabled: true,
1835 support_system_process: true,
1836 },
1837 nocrt: true,
1838 }
1839
1840 cc_library {
1841 name: "libvndk_sp_ext",
1842 vendor: true,
1843 vndk: {
1844 enabled: true,
1845 extends: "libvndk_sp",
1846 support_system_process: true,
1847 },
1848 shared_libs: ["libvendor"], // Cause an error
1849 nocrt: true,
1850 }
1851
1852 cc_library {
1853 name: "libvendor",
1854 vendor: true,
1855 nocrt: true,
1856 }
1857 `)
1858}
1859
Justin Yun0ecf0b22020-02-28 15:07:59 +09001860func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001861 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001862 bp := `
1863 cc_library {
1864 name: "libvndk",
1865 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001866 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001867 vndk: {
1868 enabled: true,
1869 },
1870 nocrt: true,
1871 }
1872
1873 cc_library {
1874 name: "libvndk_ext_product",
1875 product_specific: true,
1876 vndk: {
1877 enabled: true,
1878 extends: "libvndk",
1879 },
1880 shared_libs: ["libproduct_for_vndklibs"],
1881 nocrt: true,
1882 }
1883
1884 cc_library {
1885 name: "libvndk_sp",
1886 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001887 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001888 vndk: {
1889 enabled: true,
1890 support_system_process: true,
1891 },
1892 nocrt: true,
1893 }
1894
1895 cc_library {
1896 name: "libvndk_sp_ext_product",
1897 product_specific: true,
1898 vndk: {
1899 enabled: true,
1900 extends: "libvndk_sp",
1901 support_system_process: true,
1902 },
1903 shared_libs: ["libproduct_for_vndklibs"],
1904 nocrt: true,
1905 }
1906
1907 cc_library {
1908 name: "libproduct",
1909 product_specific: true,
1910 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1911 nocrt: true,
1912 }
1913
1914 cc_library {
1915 name: "libproduct_for_vndklibs",
1916 product_specific: true,
1917 nocrt: true,
1918 }
1919 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001920 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001921 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1922 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001923 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001924
1925 testCcWithConfig(t, config)
1926}
1927
Logan Chiend3c59a22018-03-29 14:08:15 +08001928func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001929 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001930 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1931 // library.
1932 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1933 cc_library {
1934 name: "libvndk",
1935 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001936 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001937 vndk: {
1938 enabled: true,
1939 },
1940 nocrt: true,
1941 }
1942
1943 cc_library {
1944 name: "libvndk_sp",
1945 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001946 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001947 vndk: {
1948 enabled: true,
1949 support_system_process: true,
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_sp_ext",
1956 vendor: true,
1957 vndk: {
1958 enabled: true,
1959 extends: "libvndk_sp",
1960 support_system_process: true,
1961 },
1962 shared_libs: ["libvndk"], // Cause an error
1963 nocrt: true,
1964 }
1965 `)
1966
1967 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1968 // library.
1969 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1970 cc_library {
1971 name: "libvndk",
1972 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001973 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001974 vndk: {
1975 enabled: true,
1976 },
1977 nocrt: true,
1978 }
1979
1980 cc_library {
1981 name: "libvndk_ext",
1982 vendor: true,
1983 vndk: {
1984 enabled: true,
1985 extends: "libvndk",
1986 },
1987 nocrt: true,
1988 }
1989
1990 cc_library {
1991 name: "libvndk_sp",
1992 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001993 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001994 vndk: {
1995 enabled: true,
1996 support_system_process: true,
1997 },
1998 nocrt: true,
1999 }
2000
2001 cc_library {
2002 name: "libvndk_sp_ext",
2003 vendor: true,
2004 vndk: {
2005 enabled: true,
2006 extends: "libvndk_sp",
2007 support_system_process: true,
2008 },
2009 shared_libs: ["libvndk_ext"], // Cause an error
2010 nocrt: true,
2011 }
2012 `)
2013}
2014
2015func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002016 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08002017 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2018 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002019 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2020 cc_library {
2021 name: "libvndk",
2022 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002023 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002024 vndk: {
2025 enabled: true,
2026 },
2027 nocrt: true,
2028 }
2029
2030 cc_library {
2031 name: "libvndk_ext",
2032 vendor: true,
2033 vndk: {
2034 enabled: true,
2035 extends: "libvndk",
2036 },
2037 nocrt: true,
2038 }
2039
2040 cc_library {
2041 name: "libvndk2",
2042 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002043 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002044 vndk: {
2045 enabled: true,
2046 },
2047 shared_libs: ["libvndk_ext"],
2048 nocrt: true,
2049 }
2050 `)
2051
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002052 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002053 cc_library {
2054 name: "libvndk",
2055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002056 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002057 vndk: {
2058 enabled: true,
2059 },
2060 nocrt: true,
2061 }
2062
2063 cc_library {
2064 name: "libvndk_ext",
2065 vendor: true,
2066 vndk: {
2067 enabled: true,
2068 extends: "libvndk",
2069 },
2070 nocrt: true,
2071 }
2072
2073 cc_library {
2074 name: "libvndk2",
2075 vendor_available: true,
2076 vndk: {
2077 enabled: true,
2078 },
2079 target: {
2080 vendor: {
2081 shared_libs: ["libvndk_ext"],
2082 },
2083 },
2084 nocrt: true,
2085 }
2086 `)
2087
2088 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2089 cc_library {
2090 name: "libvndk_sp",
2091 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002092 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002093 vndk: {
2094 enabled: true,
2095 support_system_process: true,
2096 },
2097 nocrt: true,
2098 }
2099
2100 cc_library {
2101 name: "libvndk_sp_ext",
2102 vendor: true,
2103 vndk: {
2104 enabled: true,
2105 extends: "libvndk_sp",
2106 support_system_process: true,
2107 },
2108 nocrt: true,
2109 }
2110
2111 cc_library {
2112 name: "libvndk_sp_2",
2113 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002114 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002115 vndk: {
2116 enabled: true,
2117 support_system_process: true,
2118 },
2119 shared_libs: ["libvndk_sp_ext"],
2120 nocrt: true,
2121 }
2122 `)
2123
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002124 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002125 cc_library {
2126 name: "libvndk_sp",
2127 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002128 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002129 vndk: {
2130 enabled: true,
2131 },
2132 nocrt: true,
2133 }
2134
2135 cc_library {
2136 name: "libvndk_sp_ext",
2137 vendor: true,
2138 vndk: {
2139 enabled: true,
2140 extends: "libvndk_sp",
2141 },
2142 nocrt: true,
2143 }
2144
2145 cc_library {
2146 name: "libvndk_sp2",
2147 vendor_available: true,
2148 vndk: {
2149 enabled: true,
2150 },
2151 target: {
2152 vendor: {
2153 shared_libs: ["libvndk_sp_ext"],
2154 },
2155 },
2156 nocrt: true,
2157 }
2158 `)
2159}
2160
Justin Yun5f7f7e82019-11-18 19:52:14 +09002161func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002162 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002163 bp := `
2164 cc_library {
2165 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002166 llndk: {
2167 symbol_file: "libllndk.map.txt",
2168 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002169 }
2170 cc_library {
2171 name: "libvndk",
2172 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002173 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002174 vndk: {
2175 enabled: true,
2176 },
2177 nocrt: true,
2178 }
2179 cc_library {
2180 name: "libvndk_sp",
2181 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002182 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002183 vndk: {
2184 enabled: true,
2185 support_system_process: true,
2186 },
2187 nocrt: true,
2188 }
2189 cc_library {
2190 name: "libva",
2191 vendor_available: true,
2192 nocrt: true,
2193 }
2194 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002195 name: "libpa",
2196 product_available: true,
2197 nocrt: true,
2198 }
2199 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002200 name: "libboth_available",
2201 vendor_available: true,
2202 product_available: true,
2203 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002204 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002205 target: {
2206 vendor: {
2207 suffix: "-vendor",
2208 },
2209 product: {
2210 suffix: "-product",
2211 },
2212 }
2213 }
2214 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002215 name: "libproduct_va",
2216 product_specific: true,
2217 vendor_available: true,
2218 nocrt: true,
2219 }
2220 cc_library {
2221 name: "libprod",
2222 product_specific: true,
2223 shared_libs: [
2224 "libllndk",
2225 "libvndk",
2226 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002227 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002228 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002229 "libproduct_va",
2230 ],
2231 nocrt: true,
2232 }
2233 cc_library {
2234 name: "libvendor",
2235 vendor: true,
2236 shared_libs: [
2237 "libllndk",
2238 "libvndk",
2239 "libvndk_sp",
2240 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002241 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002242 "libproduct_va",
2243 ],
2244 nocrt: true,
2245 }
2246 `
2247
Paul Duffin8567f222021-03-23 00:02:06 +00002248 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002249
Jooyung Han261e1582020-10-20 18:54:21 +09002250 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2251 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002252
2253 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2254 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2255
2256 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2257 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002258
2259 ensureStringContains := func(t *testing.T, str string, substr string) {
2260 t.Helper()
2261 if !strings.Contains(str, substr) {
2262 t.Errorf("%q is not found in %v", substr, str)
2263 }
2264 }
2265 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2266 t.Helper()
2267 if strings.Contains(str, substr) {
2268 t.Errorf("%q is found in %v", substr, str)
2269 }
2270 }
2271
2272 // _static variant is used since _shared reuses *.o from the static variant
2273 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2274 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2275
2276 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2277 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2278 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2279 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2280
2281 product_cflags := product_static.Rule("cc").Args["cFlags"]
2282 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2283 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2284 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002285}
2286
2287func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002288 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002289 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002290 cc_library {
2291 name: "libprod",
2292 product_specific: true,
2293 shared_libs: [
2294 "libvendor",
2295 ],
2296 nocrt: true,
2297 }
2298 cc_library {
2299 name: "libvendor",
2300 vendor: true,
2301 nocrt: true,
2302 }
2303 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002304 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002305 cc_library {
2306 name: "libprod",
2307 product_specific: true,
2308 shared_libs: [
2309 "libsystem",
2310 ],
2311 nocrt: true,
2312 }
2313 cc_library {
2314 name: "libsystem",
2315 nocrt: true,
2316 }
2317 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002318 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002319 cc_library {
2320 name: "libprod",
2321 product_specific: true,
2322 shared_libs: [
2323 "libva",
2324 ],
2325 nocrt: true,
2326 }
2327 cc_library {
2328 name: "libva",
2329 vendor_available: true,
2330 nocrt: true,
2331 }
2332 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002333 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002334 cc_library {
2335 name: "libprod",
2336 product_specific: true,
2337 shared_libs: [
2338 "libvndk_private",
2339 ],
2340 nocrt: true,
2341 }
2342 cc_library {
2343 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002344 vendor_available: true,
2345 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002346 vndk: {
2347 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002348 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002349 },
2350 nocrt: true,
2351 }
2352 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002353 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002354 cc_library {
2355 name: "libprod",
2356 product_specific: true,
2357 shared_libs: [
2358 "libsystem_ext",
2359 ],
2360 nocrt: true,
2361 }
2362 cc_library {
2363 name: "libsystem_ext",
2364 system_ext_specific: true,
2365 nocrt: true,
2366 }
2367 `)
2368 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2369 cc_library {
2370 name: "libsystem",
2371 shared_libs: [
2372 "libproduct_va",
2373 ],
2374 nocrt: true,
2375 }
2376 cc_library {
2377 name: "libproduct_va",
2378 product_specific: true,
2379 vendor_available: true,
2380 nocrt: true,
2381 }
2382 `)
2383}
2384
Jooyung Han38002912019-05-16 04:01:54 +09002385func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002386 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002387 bp := `
2388 cc_library {
2389 name: "libvndk",
2390 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002391 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002392 vndk: {
2393 enabled: true,
2394 },
2395 }
2396 cc_library {
2397 name: "libvndksp",
2398 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002399 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002400 vndk: {
2401 enabled: true,
2402 support_system_process: true,
2403 },
2404 }
2405 cc_library {
2406 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002407 vendor_available: true,
2408 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002409 vndk: {
2410 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002411 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002412 },
2413 }
2414 cc_library {
2415 name: "libvendor",
2416 vendor: true,
2417 }
2418 cc_library {
2419 name: "libvndkext",
2420 vendor: true,
2421 vndk: {
2422 enabled: true,
2423 extends: "libvndk",
2424 },
2425 }
2426 vndk_prebuilt_shared {
2427 name: "prevndk",
2428 version: "27",
2429 target_arch: "arm",
2430 binder32bit: true,
2431 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002432 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002433 vndk: {
2434 enabled: true,
2435 },
2436 arch: {
2437 arm: {
2438 srcs: ["liba.so"],
2439 },
2440 },
2441 }
2442 cc_library {
2443 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002444 llndk: {
2445 symbol_file: "libllndk.map.txt",
2446 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002447 }
2448 cc_library {
2449 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002450 llndk: {
2451 symbol_file: "libllndkprivate.map.txt",
2452 private: true,
2453 }
Colin Cross78212242021-01-06 14:51:30 -08002454 }
2455
2456 llndk_libraries_txt {
2457 name: "llndk.libraries.txt",
2458 }
2459 vndkcore_libraries_txt {
2460 name: "vndkcore.libraries.txt",
2461 }
2462 vndksp_libraries_txt {
2463 name: "vndksp.libraries.txt",
2464 }
2465 vndkprivate_libraries_txt {
2466 name: "vndkprivate.libraries.txt",
2467 }
2468 vndkcorevariant_libraries_txt {
2469 name: "vndkcorevariant.libraries.txt",
2470 insert_vndk_version: false,
2471 }
2472 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002473
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002474 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002475 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002476 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002477 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002478 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002479
Colin Cross78212242021-01-06 14:51:30 -08002480 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2481 []string{"libvndk.so", "libvndkprivate.so"})
2482 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2483 []string{"libc++.so", "libvndksp.so"})
2484 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2485 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2486 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2487 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002488
Colin Crossfb0c16e2019-11-20 17:12:35 -08002489 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002490
Jooyung Han38002912019-05-16 04:01:54 +09002491 tests := []struct {
2492 variant string
2493 name string
2494 expected string
2495 }{
2496 {vendorVariant, "libvndk", "native:vndk"},
2497 {vendorVariant, "libvndksp", "native:vndk"},
2498 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2499 {vendorVariant, "libvendor", "native:vendor"},
2500 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002501 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002502 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002503 {coreVariant, "libvndk", "native:platform"},
2504 {coreVariant, "libvndkprivate", "native:platform"},
2505 {coreVariant, "libllndk", "native:platform"},
2506 }
2507 for _, test := range tests {
2508 t.Run(test.name, func(t *testing.T) {
2509 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2510 assertString(t, module.makeLinkType, test.expected)
2511 })
2512 }
2513}
2514
Jeff Gaston294356f2017-09-27 17:05:30 -07002515var staticLinkDepOrderTestCases = []struct {
2516 // This is a string representation of a map[moduleName][]moduleDependency .
2517 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002518 inStatic string
2519
2520 // This is a string representation of a map[moduleName][]moduleDependency .
2521 // It models the dependencies declared in an Android.bp file.
2522 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002523
2524 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2525 // The keys of allOrdered specify which modules we would like to check.
2526 // The values of allOrdered specify the expected result (of the transitive closure of all
2527 // dependencies) for each module to test
2528 allOrdered string
2529
2530 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2531 // The keys of outOrdered specify which modules we would like to check.
2532 // The values of outOrdered specify the expected result (of the ordered linker command line)
2533 // for each module to test.
2534 outOrdered string
2535}{
2536 // Simple tests
2537 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002538 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002539 outOrdered: "",
2540 },
2541 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002542 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002543 outOrdered: "a:",
2544 },
2545 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002546 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002547 outOrdered: "a:b; b:",
2548 },
2549 // Tests of reordering
2550 {
2551 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002552 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002553 outOrdered: "a:b,c,d; b:d; c:d; d:",
2554 },
2555 {
2556 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002557 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002558 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2559 },
2560 {
2561 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002562 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002563 outOrdered: "a:d,b,e,c; d:b; e:c",
2564 },
2565 {
2566 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002567 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002568 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2569 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2570 },
2571 {
2572 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002573 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 -07002574 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2575 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2576 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002577 // shared dependencies
2578 {
2579 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2580 // So, we don't actually have to check that a shared dependency of c will change the order
2581 // of a library that depends statically on b and on c. We only need to check that if c has
2582 // a shared dependency on b, that that shows up in allOrdered.
2583 inShared: "c:b",
2584 allOrdered: "c:b",
2585 outOrdered: "c:",
2586 },
2587 {
2588 // This test doesn't actually include any shared dependencies but it's a reminder of what
2589 // the second phase of the above test would look like
2590 inStatic: "a:b,c; c:b",
2591 allOrdered: "a:c,b; c:b",
2592 outOrdered: "a:c,b; c:b",
2593 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002594 // tiebreakers for when two modules specifying different orderings and there is no dependency
2595 // to dictate an order
2596 {
2597 // 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 -08002598 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002599 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2600 },
2601 {
2602 // 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 -08002603 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 -07002604 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2605 },
2606 // Tests involving duplicate dependencies
2607 {
2608 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002609 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002610 outOrdered: "a:c,b",
2611 },
2612 {
2613 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002614 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002615 outOrdered: "a:d,c,b",
2616 },
2617 // Tests to confirm the nonexistence of infinite loops.
2618 // These cases should never happen, so as long as the test terminates and the
2619 // result is deterministic then that should be fine.
2620 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002621 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002622 outOrdered: "a:a",
2623 },
2624 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002625 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002626 allOrdered: "a:b,c; b:c,a; c:a,b",
2627 outOrdered: "a:b; b:c; c:a",
2628 },
2629 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002630 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002631 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2632 outOrdered: "a:c,b; b:a,c; c:b,a",
2633 },
2634}
2635
2636// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2637func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2638 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2639 strippedText := strings.Replace(text, " ", "", -1)
2640 if len(strippedText) < 1 {
2641 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2642 }
2643 allDeps = make(map[android.Path][]android.Path, 0)
2644
2645 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2646 moduleTexts := strings.Split(strippedText, ";")
2647
2648 outputForModuleName := func(moduleName string) android.Path {
2649 return android.PathForTesting(moduleName)
2650 }
2651
2652 for _, moduleText := range moduleTexts {
2653 // convert from "a:b,c" to ["a", "b,c"]
2654 components := strings.Split(moduleText, ":")
2655 if len(components) != 2 {
2656 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2657 }
2658 moduleName := components[0]
2659 moduleOutput := outputForModuleName(moduleName)
2660 modulesInOrder = append(modulesInOrder, moduleOutput)
2661
2662 depString := components[1]
2663 // convert from "b,c" to ["b", "c"]
2664 depNames := strings.Split(depString, ",")
2665 if len(depString) < 1 {
2666 depNames = []string{}
2667 }
2668 var deps []android.Path
2669 for _, depName := range depNames {
2670 deps = append(deps, outputForModuleName(depName))
2671 }
2672 allDeps[moduleOutput] = deps
2673 }
2674 return modulesInOrder, allDeps
2675}
2676
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002677func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002678 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002679 ctx := testCc(t, `
2680 cc_library {
2681 name: "a",
2682 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002683 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002684 }
2685 cc_library {
2686 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002687 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002688 }
2689 cc_library {
2690 name: "c",
2691 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002692 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002693 }
2694 cc_library {
2695 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002696 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002697 }
2698
2699 `)
2700
Colin Cross7113d202019-11-20 16:39:12 -08002701 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002702 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002703 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2704 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002705 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002706
2707 if !reflect.DeepEqual(actual, expected) {
2708 t.Errorf("staticDeps orderings were not propagated correctly"+
2709 "\nactual: %v"+
2710 "\nexpected: %v",
2711 actual,
2712 expected,
2713 )
2714 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002715}
Jeff Gaston294356f2017-09-27 17:05:30 -07002716
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002717func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002718 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002719 ctx := testCc(t, `
2720 cc_library {
2721 name: "a",
2722 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002723 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002724 }
2725 cc_library {
2726 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002727 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002728 }
2729 cc_library {
2730 name: "c",
2731 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002732 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002733 }
2734
2735 `)
2736
Colin Cross7113d202019-11-20 16:39:12 -08002737 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002738 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002739 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2740 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002741 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002742
2743 if !reflect.DeepEqual(actual, expected) {
2744 t.Errorf("staticDeps orderings did not account for shared libs"+
2745 "\nactual: %v"+
2746 "\nexpected: %v",
2747 actual,
2748 expected,
2749 )
2750 }
2751}
2752
Jooyung Hanb04a4992020-03-13 18:57:35 +09002753func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002754 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002755 if !reflect.DeepEqual(actual, expected) {
2756 t.Errorf(message+
2757 "\nactual: %v"+
2758 "\nexpected: %v",
2759 actual,
2760 expected,
2761 )
2762 }
2763}
2764
Jooyung Han61b66e92020-03-21 14:21:46 +00002765func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002766 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002767 result := prepareForCcTest.RunTestWithBp(t, `
2768 cc_library {
2769 name: "libllndk",
2770 stubs: { versions: ["1", "2"] },
2771 llndk: {
2772 symbol_file: "libllndk.map.txt",
2773 },
2774 export_include_dirs: ["include"],
2775 }
2776
2777 cc_prebuilt_library_shared {
2778 name: "libllndkprebuilt",
2779 stubs: { versions: ["1", "2"] },
2780 llndk: {
2781 symbol_file: "libllndkprebuilt.map.txt",
2782 },
2783 }
2784
2785 cc_library {
2786 name: "libllndk_with_external_headers",
2787 stubs: { versions: ["1", "2"] },
2788 llndk: {
2789 symbol_file: "libllndk.map.txt",
2790 export_llndk_headers: ["libexternal_llndk_headers"],
2791 },
2792 header_libs: ["libexternal_headers"],
2793 export_header_lib_headers: ["libexternal_headers"],
2794 }
2795 cc_library_headers {
2796 name: "libexternal_headers",
2797 export_include_dirs: ["include"],
2798 vendor_available: true,
2799 }
2800 cc_library_headers {
2801 name: "libexternal_llndk_headers",
2802 export_include_dirs: ["include_llndk"],
2803 llndk: {
2804 symbol_file: "libllndk.map.txt",
2805 },
2806 vendor_available: true,
2807 }
2808
2809 cc_library {
2810 name: "libllndk_with_override_headers",
2811 stubs: { versions: ["1", "2"] },
2812 llndk: {
2813 symbol_file: "libllndk.map.txt",
2814 override_export_include_dirs: ["include_llndk"],
2815 },
2816 export_include_dirs: ["include"],
2817 }
2818 `)
2819 actual := result.ModuleVariantsForTests("libllndk")
2820 for i := 0; i < len(actual); i++ {
2821 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2822 actual = append(actual[:i], actual[i+1:]...)
2823 i--
2824 }
2825 }
2826 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002827 "android_vendor.29_arm64_armv8-a_shared_current",
2828 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002829 "android_vendor.29_arm_armv7-a-neon_shared_current",
2830 "android_vendor.29_arm_armv7-a-neon_shared",
2831 }
2832 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2833
2834 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2835 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2836
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002837 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2838 t.Helper()
2839 m := result.ModuleForTests(module, variant).Module()
2840 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2841 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2842 expectedDirs, f.IncludeDirs)
2843 }
2844
2845 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2846 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2847 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2848 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2849 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2850 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2851}
2852
Jiyong Parka46a4d52017-12-14 19:54:34 +09002853func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002854 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002855 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002856 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002857 name: "libllndk_headers",
2858 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002859 llndk: {
2860 llndk_headers: true,
2861 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002862 }
2863 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002864 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002865 llndk: {
2866 symbol_file: "libllndk.map.txt",
2867 export_llndk_headers: ["libllndk_headers"],
2868 }
Colin Cross0477b422020-10-13 18:43:54 -07002869 }
2870
2871 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002872 name: "libvendor",
2873 shared_libs: ["libllndk"],
2874 vendor: true,
2875 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002876 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002877 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002878 }
2879 `)
2880
2881 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002882 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002883 cflags := cc.Args["cFlags"]
2884 if !strings.Contains(cflags, "-Imy_include") {
2885 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2886 }
2887}
2888
Logan Chien43d34c32017-12-20 01:17:32 +08002889func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2890 actual := module.Properties.AndroidMkRuntimeLibs
2891 if !reflect.DeepEqual(actual, expected) {
2892 t.Errorf("incorrect runtime_libs for shared libs"+
2893 "\nactual: %v"+
2894 "\nexpected: %v",
2895 actual,
2896 expected,
2897 )
2898 }
2899}
2900
2901const runtimeLibAndroidBp = `
2902 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002903 name: "liball_available",
2904 vendor_available: true,
2905 product_available: true,
2906 no_libcrt : true,
2907 nocrt : true,
2908 system_shared_libs : [],
2909 }
2910 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002911 name: "libvendor_available1",
2912 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002913 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002914 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002915 nocrt : true,
2916 system_shared_libs : [],
2917 }
2918 cc_library {
2919 name: "libvendor_available2",
2920 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002921 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002922 target: {
2923 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002924 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002925 }
2926 },
Yi Konge7fe9912019-06-02 00:53:50 -07002927 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002928 nocrt : true,
2929 system_shared_libs : [],
2930 }
2931 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002932 name: "libproduct_vendor",
2933 product_specific: true,
2934 vendor_available: true,
2935 no_libcrt : true,
2936 nocrt : true,
2937 system_shared_libs : [],
2938 }
2939 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002940 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002941 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002942 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002943 nocrt : true,
2944 system_shared_libs : [],
2945 }
2946 cc_library {
2947 name: "libvendor1",
2948 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002949 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002950 nocrt : true,
2951 system_shared_libs : [],
2952 }
2953 cc_library {
2954 name: "libvendor2",
2955 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002956 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002957 no_libcrt : true,
2958 nocrt : true,
2959 system_shared_libs : [],
2960 }
2961 cc_library {
2962 name: "libproduct_available1",
2963 product_available: true,
2964 runtime_libs: ["liball_available"],
2965 no_libcrt : true,
2966 nocrt : true,
2967 system_shared_libs : [],
2968 }
2969 cc_library {
2970 name: "libproduct1",
2971 product_specific: true,
2972 no_libcrt : true,
2973 nocrt : true,
2974 system_shared_libs : [],
2975 }
2976 cc_library {
2977 name: "libproduct2",
2978 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002979 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002980 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002981 nocrt : true,
2982 system_shared_libs : [],
2983 }
2984`
2985
2986func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002987 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002988 ctx := testCc(t, runtimeLibAndroidBp)
2989
2990 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002991 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002992
Justin Yun8a2600c2020-12-07 12:44:03 +09002993 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2994 checkRuntimeLibs(t, []string{"liball_available"}, module)
2995
2996 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2997 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002998
2999 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003000 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003001
3002 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3003 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003004 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003005
Justin Yun8a2600c2020-12-07 12:44:03 +09003006 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3007 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003008
3009 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003010 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003011
3012 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3013 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003014 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003015
3016 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3017 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3018
3019 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003020 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003021}
3022
3023func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003024 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003025 ctx := testCc(t, runtimeLibAndroidBp)
3026
Colin Cross7113d202019-11-20 16:39:12 -08003027 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003028 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3029 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003030
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003031 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003032 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003033 checkRuntimeLibs(t, nil, module)
3034}
3035
3036func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003037 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003038 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3039
3040 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3041
Colin Cross7113d202019-11-20 16:39:12 -08003042 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003043
Justin Yun8a2600c2020-12-07 12:44:03 +09003044 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3045 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003046
3047 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003048 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003049
3050 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003051 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003052}
3053
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003054func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003055 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003056 actual := module.Properties.AndroidMkStaticLibs
3057 if !reflect.DeepEqual(actual, expected) {
3058 t.Errorf("incorrect static_libs"+
3059 "\nactual: %v"+
3060 "\nexpected: %v",
3061 actual,
3062 expected,
3063 )
3064 }
3065}
3066
3067const staticLibAndroidBp = `
3068 cc_library {
3069 name: "lib1",
3070 }
3071 cc_library {
3072 name: "lib2",
3073 static_libs: ["lib1"],
3074 }
3075`
3076
3077func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003078 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003079 ctx := testCc(t, staticLibAndroidBp)
3080
3081 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003082 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003083 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003084 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003085
3086 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003087 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003088 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3089 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003090 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003091}
3092
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003093func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3094 bp := `
3095 cc_library {
3096 name: "static_dep",
3097 }
3098 cc_library {
3099 name: "whole_static_dep",
3100 }
3101 cc_library {
3102 name: "shared_dep",
3103 }
3104 cc_library {
3105 name: "lib",
3106 bazel_module: { label: "//:lib" },
3107 static_libs: ["static_dep"],
3108 whole_static_libs: ["whole_static_dep"],
3109 shared_libs: ["shared_dep"],
3110 }
3111 cc_test {
3112 name: "test",
3113 bazel_module: { label: "//:test" },
3114 static_libs: ["static_dep"],
3115 whole_static_libs: ["whole_static_dep"],
3116 shared_libs: ["shared_dep"],
3117 gtest: false,
3118 }
3119 cc_binary {
3120 name: "binary",
3121 bazel_module: { label: "//:binary" },
3122 static_libs: ["static_dep"],
3123 whole_static_libs: ["whole_static_dep"],
3124 shared_libs: ["shared_dep"],
3125 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003126 cc_library_headers {
3127 name: "lib_headers",
3128 bazel_module: { label: "//:lib_headers" },
3129 static_libs: ["static_dep"],
3130 whole_static_libs: ["whole_static_dep"],
3131 shared_libs: ["shared_dep"],
3132 }
3133 cc_prebuilt_library {
3134 name: "lib_prebuilt",
3135 bazel_module: { label: "//:lib_prebuilt" },
3136 static_libs: ["static_dep"],
3137 whole_static_libs: ["whole_static_dep"],
3138 shared_libs: ["shared_dep"],
3139 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003140 `
3141
3142 testCases := []struct {
3143 name string
3144 moduleName string
3145 variant string
3146 androidMkInfo cquery.CcAndroidMkInfo
3147 }{
3148 {
3149 name: "shared lib",
3150 moduleName: "lib",
3151 variant: "android_arm64_armv8-a_shared",
3152 androidMkInfo: cquery.CcAndroidMkInfo{
3153 LocalStaticLibs: []string{"static_dep"},
3154 LocalWholeStaticLibs: []string{"whole_static_dep"},
3155 LocalSharedLibs: []string{"shared_dep"},
3156 },
3157 },
3158 {
3159 name: "static lib",
3160 moduleName: "lib",
3161 variant: "android_arm64_armv8-a_static",
3162 androidMkInfo: cquery.CcAndroidMkInfo{
3163 LocalStaticLibs: []string{"static_dep"},
3164 LocalWholeStaticLibs: []string{"whole_static_dep"},
3165 LocalSharedLibs: []string{"shared_dep"},
3166 },
3167 },
3168 {
3169 name: "cc_test arm64",
3170 moduleName: "test",
3171 variant: "android_arm64_armv8-a",
3172 androidMkInfo: cquery.CcAndroidMkInfo{
3173 LocalStaticLibs: []string{"static_dep"},
3174 LocalWholeStaticLibs: []string{"whole_static_dep"},
3175 LocalSharedLibs: []string{"shared_dep"},
3176 },
3177 },
3178 {
3179 name: "cc_test arm",
3180 moduleName: "test",
3181 variant: "android_arm_armv7-a-neon",
3182 androidMkInfo: cquery.CcAndroidMkInfo{
3183 LocalStaticLibs: []string{"static_dep"},
3184 LocalWholeStaticLibs: []string{"whole_static_dep"},
3185 LocalSharedLibs: []string{"shared_dep"},
3186 },
3187 },
3188 {
3189 name: "cc_binary",
3190 moduleName: "binary",
3191 variant: "android_arm64_armv8-a",
3192 androidMkInfo: cquery.CcAndroidMkInfo{
3193 LocalStaticLibs: []string{"static_dep"},
3194 LocalWholeStaticLibs: []string{"whole_static_dep"},
3195 LocalSharedLibs: []string{"shared_dep"},
3196 },
3197 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003198 {
3199 name: "cc_library_headers",
3200 moduleName: "lib_headers",
3201 variant: "android_arm64_armv8-a",
3202 androidMkInfo: cquery.CcAndroidMkInfo{
3203 LocalStaticLibs: []string{"static_dep"},
3204 LocalWholeStaticLibs: []string{"whole_static_dep"},
3205 LocalSharedLibs: []string{"shared_dep"},
3206 },
3207 },
3208 {
3209 name: "prebuilt lib static",
3210 moduleName: "lib_prebuilt",
3211 variant: "android_arm64_armv8-a_static",
3212 androidMkInfo: cquery.CcAndroidMkInfo{
3213 LocalStaticLibs: []string{"static_dep"},
3214 LocalWholeStaticLibs: []string{"whole_static_dep"},
3215 LocalSharedLibs: []string{"shared_dep"},
3216 },
3217 },
3218 {
3219 name: "prebuilt lib shared",
3220 moduleName: "lib_prebuilt",
3221 variant: "android_arm64_armv8-a_shared",
3222 androidMkInfo: cquery.CcAndroidMkInfo{
3223 LocalStaticLibs: []string{"static_dep"},
3224 LocalWholeStaticLibs: []string{"whole_static_dep"},
3225 LocalSharedLibs: []string{"shared_dep"},
3226 },
3227 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003228 }
3229
3230 outputBaseDir := "out/bazel"
3231 for _, tc := range testCases {
3232 t.Run(tc.name, func(t *testing.T) {
3233 result := android.GroupFixturePreparers(
3234 prepareForCcTest,
3235 android.FixtureModifyConfig(func(config android.Config) {
3236 config.BazelContext = android.MockBazelContext{
3237 OutputBaseDir: outputBaseDir,
3238 LabelToCcInfo: map[string]cquery.CcInfo{
3239 "//:lib": cquery.CcInfo{
3240 CcAndroidMkInfo: tc.androidMkInfo,
3241 RootDynamicLibraries: []string{""},
3242 },
3243 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3244 CcAndroidMkInfo: tc.androidMkInfo,
3245 RootStaticArchives: []string{""},
3246 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003247 "//:lib_headers": cquery.CcInfo{
3248 CcAndroidMkInfo: tc.androidMkInfo,
3249 OutputFiles: []string{""},
3250 },
3251 "//:lib_prebuilt": cquery.CcInfo{
3252 CcAndroidMkInfo: tc.androidMkInfo,
3253 },
3254 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3255 CcAndroidMkInfo: tc.androidMkInfo,
3256 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003257 },
3258 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3259 "//:test": cquery.CcUnstrippedInfo{
3260 CcAndroidMkInfo: tc.androidMkInfo,
3261 },
3262 "//:binary": cquery.CcUnstrippedInfo{
3263 CcAndroidMkInfo: tc.androidMkInfo,
3264 },
3265 },
3266 }
3267 }),
3268 ).RunTestWithBp(t, bp)
3269 ctx := result.TestContext
3270
3271 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3272 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003273 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3274 t.Errorf("incorrect static_libs"+
3275 "\nactual: %v"+
3276 "\nexpected: %v",
3277 module.Properties.AndroidMkStaticLibs,
3278 tc.androidMkInfo.LocalStaticLibs,
3279 )
3280 }
3281 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3282 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3283 tc.androidMkInfo.LocalStaticLibs,
3284 )
3285 if staticDepsDiffer {
3286 t.Errorf(
3287 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3288 tc.androidMkInfo.LocalStaticLibs,
3289 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3290 missingStaticDeps,
3291 additionalStaticDeps,
3292 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003293 }
3294
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003295 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3296 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3297 tc.androidMkInfo.LocalWholeStaticLibs,
3298 module.Properties.AndroidMkWholeStaticLibs,
3299 )
3300 }
3301 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3302 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3303 tc.androidMkInfo.LocalWholeStaticLibs,
3304 )
3305 if wholeStaticDepsDiffer {
3306 t.Errorf(
3307 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3308 tc.androidMkInfo.LocalWholeStaticLibs,
3309 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3310 missingWholeStaticDeps,
3311 additionalWholeStaticDeps,
3312 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003313 }
3314
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003315 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3316 t.Errorf("incorrect shared_libs"+
3317 "\nactual: %v"+
3318 "\nexpected: %v",
3319 module.Properties.AndroidMkSharedLibs,
3320 tc.androidMkInfo.LocalSharedLibs,
3321 )
3322 }
3323 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3324 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3325 tc.androidMkInfo.LocalSharedLibs,
3326 )
3327 if sharedDepsDiffer {
3328 t.Errorf(
3329 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3330 tc.androidMkInfo.LocalSharedLibs,
3331 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3332 missingSharedDeps,
3333 additionalSharedDeps,
3334 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003335 }
3336 })
3337 }
3338}
3339
Jiyong Parkd08b6972017-09-26 10:50:54 +09003340var compilerFlagsTestCases = []struct {
3341 in string
3342 out bool
3343}{
3344 {
3345 in: "a",
3346 out: false,
3347 },
3348 {
3349 in: "-a",
3350 out: true,
3351 },
3352 {
3353 in: "-Ipath/to/something",
3354 out: false,
3355 },
3356 {
3357 in: "-isystempath/to/something",
3358 out: false,
3359 },
3360 {
3361 in: "--coverage",
3362 out: false,
3363 },
3364 {
3365 in: "-include a/b",
3366 out: true,
3367 },
3368 {
3369 in: "-include a/b c/d",
3370 out: false,
3371 },
3372 {
3373 in: "-DMACRO",
3374 out: true,
3375 },
3376 {
3377 in: "-DMAC RO",
3378 out: false,
3379 },
3380 {
3381 in: "-a -b",
3382 out: false,
3383 },
3384 {
3385 in: "-DMACRO=definition",
3386 out: true,
3387 },
3388 {
3389 in: "-DMACRO=defi nition",
3390 out: true, // TODO(jiyong): this should be false
3391 },
3392 {
3393 in: "-DMACRO(x)=x + 1",
3394 out: true,
3395 },
3396 {
3397 in: "-DMACRO=\"defi nition\"",
3398 out: true,
3399 },
3400}
3401
3402type mockContext struct {
3403 BaseModuleContext
3404 result bool
3405}
3406
3407func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3408 // CheckBadCompilerFlags calls this function when the flag should be rejected
3409 ctx.result = false
3410}
3411
3412func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003413 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003414 for _, testCase := range compilerFlagsTestCases {
3415 ctx := &mockContext{result: true}
3416 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3417 if ctx.result != testCase.out {
3418 t.Errorf("incorrect output:")
3419 t.Errorf(" input: %#v", testCase.in)
3420 t.Errorf(" expected: %#v", testCase.out)
3421 t.Errorf(" got: %#v", ctx.result)
3422 }
3423 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003424}
Jiyong Park374510b2018-03-19 18:23:01 +09003425
Jiyong Park37b25202018-07-11 10:49:27 +09003426func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003427 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003428 ctx := testCc(t, `
3429 cc_library_shared {
3430 name: "librecovery",
3431 recovery: true,
3432 }
3433 cc_library_shared {
3434 name: "librecovery32",
3435 recovery: true,
3436 compile_multilib:"32",
3437 }
Jiyong Park5baac542018-08-28 09:55:37 +09003438 cc_library_shared {
3439 name: "libHalInRecovery",
3440 recovery_available: true,
3441 vendor: true,
3442 }
Jiyong Park37b25202018-07-11 10:49:27 +09003443 `)
3444
3445 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003446 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003447 if len(variants) != 1 || !android.InList(arm64, variants) {
3448 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3449 }
3450
3451 variants = ctx.ModuleVariantsForTests("librecovery32")
3452 if android.InList(arm64, variants) {
3453 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3454 }
Jiyong Park5baac542018-08-28 09:55:37 +09003455
3456 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3457 if !recoveryModule.Platform() {
3458 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3459 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003460}
Jiyong Park5baac542018-08-28 09:55:37 +09003461
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003462func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003463 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003464 bp := `
3465 cc_prebuilt_test_library_shared {
3466 name: "test_lib",
3467 relative_install_path: "foo/bar/baz",
3468 srcs: ["srcpath/dontusethispath/baz.so"],
3469 }
3470
3471 cc_test {
3472 name: "main_test",
3473 data_libs: ["test_lib"],
3474 gtest: false,
3475 }
3476 `
3477
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003478 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003479 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003480 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003481 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3482
3483 ctx := testCcWithConfig(t, config)
3484 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3485 testBinary := module.(*Module).linker.(*testBinary)
3486 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3487 if err != nil {
3488 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3489 }
3490 if len(outputFiles) != 1 {
3491 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3492 }
3493 if len(testBinary.dataPaths()) != 1 {
3494 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3495 }
3496
3497 outputPath := outputFiles[0].String()
3498
3499 if !strings.HasSuffix(outputPath, "/main_test") {
3500 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3501 }
Colin Crossaa255532020-07-03 13:18:24 -07003502 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003503 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3504 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3505 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3506 }
3507}
3508
Jiyong Park7ed9de32018-10-15 22:25:07 +09003509func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003510 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003511 ctx := testCc(t, `
3512 cc_library_shared {
3513 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003514 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003515 stubs: {
3516 symbol_file: "foo.map.txt",
3517 versions: ["1", "2", "3"],
3518 },
3519 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003520
Jiyong Park7ed9de32018-10-15 22:25:07 +09003521 cc_library_shared {
3522 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003523 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003524 shared_libs: ["libFoo#1"],
3525 }`)
3526
3527 variants := ctx.ModuleVariantsForTests("libFoo")
3528 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003529 "android_arm64_armv8-a_shared",
3530 "android_arm64_armv8-a_shared_1",
3531 "android_arm64_armv8-a_shared_2",
3532 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003533 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003534 "android_arm_armv7-a-neon_shared",
3535 "android_arm_armv7-a-neon_shared_1",
3536 "android_arm_armv7-a-neon_shared_2",
3537 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003538 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003539 }
3540 variantsMismatch := false
3541 if len(variants) != len(expectedVariants) {
3542 variantsMismatch = true
3543 } else {
3544 for _, v := range expectedVariants {
3545 if !inList(v, variants) {
3546 variantsMismatch = false
3547 }
3548 }
3549 }
3550 if variantsMismatch {
3551 t.Errorf("variants of libFoo expected:\n")
3552 for _, v := range expectedVariants {
3553 t.Errorf("%q\n", v)
3554 }
3555 t.Errorf(", but got:\n")
3556 for _, v := range variants {
3557 t.Errorf("%q\n", v)
3558 }
3559 }
3560
Colin Cross7113d202019-11-20 16:39:12 -08003561 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003562 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003563 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003564 if !strings.Contains(libFlags, libFoo1StubPath) {
3565 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3566 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003567
Colin Cross7113d202019-11-20 16:39:12 -08003568 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003569 cFlags := libBarCompileRule.Args["cFlags"]
3570 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3571 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3572 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3573 }
Jiyong Park37b25202018-07-11 10:49:27 +09003574}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003575
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003576func TestStubsForLibraryInMultipleApexes(t *testing.T) {
Liz Kammera22c4b62023-03-27 10:04:58 -04003577 // TODO(b/275313114): Test exposes non-determinism which should be corrected and the test
3578 // reenabled.
3579 t.Skip()
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003580 t.Parallel()
3581 ctx := testCc(t, `
3582 cc_library_shared {
3583 name: "libFoo",
3584 srcs: ["foo.c"],
3585 stubs: {
3586 symbol_file: "foo.map.txt",
3587 versions: ["current"],
3588 },
3589 apex_available: ["bar", "a1"],
3590 }
3591
3592 cc_library_shared {
3593 name: "libBar",
3594 srcs: ["bar.c"],
3595 shared_libs: ["libFoo"],
3596 apex_available: ["a1"],
3597 }
3598
3599 cc_library_shared {
3600 name: "libA1",
3601 srcs: ["a1.c"],
3602 shared_libs: ["libFoo"],
3603 apex_available: ["a1"],
3604 }
3605
3606 cc_library_shared {
3607 name: "libBarA1",
3608 srcs: ["bara1.c"],
3609 shared_libs: ["libFoo"],
3610 apex_available: ["bar", "a1"],
3611 }
3612
3613 cc_library_shared {
3614 name: "libAnyApex",
3615 srcs: ["anyApex.c"],
3616 shared_libs: ["libFoo"],
3617 apex_available: ["//apex_available:anyapex"],
3618 }
3619
3620 cc_library_shared {
3621 name: "libBaz",
3622 srcs: ["baz.c"],
3623 shared_libs: ["libFoo"],
3624 apex_available: ["baz"],
3625 }
3626
3627 cc_library_shared {
3628 name: "libQux",
3629 srcs: ["qux.c"],
3630 shared_libs: ["libFoo"],
3631 apex_available: ["qux", "bar"],
3632 }`)
3633
3634 variants := ctx.ModuleVariantsForTests("libFoo")
3635 expectedVariants := []string{
3636 "android_arm64_armv8-a_shared",
3637 "android_arm64_armv8-a_shared_current",
3638 "android_arm_armv7-a-neon_shared",
3639 "android_arm_armv7-a-neon_shared_current",
3640 }
3641 variantsMismatch := false
3642 if len(variants) != len(expectedVariants) {
3643 variantsMismatch = true
3644 } else {
3645 for _, v := range expectedVariants {
3646 if !inList(v, variants) {
3647 variantsMismatch = false
3648 }
3649 }
3650 }
3651 if variantsMismatch {
3652 t.Errorf("variants of libFoo expected:\n")
3653 for _, v := range expectedVariants {
3654 t.Errorf("%q\n", v)
3655 }
3656 t.Errorf(", but got:\n")
3657 for _, v := range variants {
3658 t.Errorf("%q\n", v)
3659 }
3660 }
3661
3662 linkAgainstFoo := []string{"libBarA1"}
3663 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3664
3665 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3666 for _, lib := range linkAgainstFoo {
3667 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3668 libFlags := libLinkRule.Args["libFlags"]
3669 if !strings.Contains(libFlags, libFooPath) {
3670 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3671 }
3672 }
3673
3674 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3675 for _, lib := range linkAgainstFooStubs {
3676 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3677 libFlags := libLinkRule.Args["libFlags"]
3678 if !strings.Contains(libFlags, libFooStubPath) {
3679 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3680 }
3681 }
3682}
3683
Sam Delmericodb5d6622023-04-20 13:13:25 +00003684func TestMixedBuildUsesStubs(t *testing.T) {
3685 // TODO(b/275313114): Test exposes non-determinism which should be corrected and the test
3686 // reenabled.
3687 t.Skip()
3688 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 Tran0358fb72023-05-16 16:03:20 -04004423func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran0059b692023-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 Tran0358fb72023-05-16 16:03:20 -04004433 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran0059b692023-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 Tran0358fb72023-05-16 16:03:20 -04004442 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran0059b692023-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 Tran0358fb72023-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 Tran0059b692023-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 Tran0358fb72023-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 Tran0059b692023-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 Tran0358fb72023-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 Tran0059b692023-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 Tran0059b692023-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 Tran0358fb72023-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 Tran0059b692023-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 Tran0059b692023-04-28 11:21:25 -04004847 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004848 export_aidl_headers: true,
4849 }
4850 }
Vinh Tran0059b692023-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 Tran0358fb72023-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 Tran0358fb72023-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 Tran0358fb72023-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 Hughesab5e4c62022-03-28 16:47:17 -07004985 cstd := []string{"-std=gnu11", "-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",
Stephen Hinese24303f2021-12-14 15:07:08 -08005009 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",
Stephen Hinese24303f2021-12-14 15:07:08 -08005014 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",
Liz Kammere4d1bda2022-06-22 21:02:08 +00005019 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}