blob: d95ed3f3e177c83e19e90aea0dca3f5510a5ac5d [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
Vinh Tran367d89d2023-04-28 11:21:25 -040027 "android/soong/aidl_library"
Colin Crosse1bb5d02019-09-24 14:55:04 -070028 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050029 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070030)
31
Yu Liue4312402023-01-18 09:15:31 -080032func init() {
33 registerTestMutators(android.InitRegistrationContext)
34}
35
Jiyong Park6a43f042017-10-12 23:05:00 +090036func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000037 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090038}
39
Paul Duffin2e6f90e2021-03-22 23:20:25 +000040var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000041 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
43 variables.DeviceVndkVersion = StringPtr("current")
44 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090045 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000046 }),
47)
48
Yu Liue4312402023-01-18 09:15:31 -080049var ccLibInApex = "cc_lib_in_apex"
50var apexVariationName = "apex28"
51var apexVersion = "28"
52
53func registerTestMutators(ctx android.RegistrationContext) {
54 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
55 ctx.BottomUp("apex", testApexMutator).Parallel()
56 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
57 })
58}
59
60func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
61 if m := ctx.Module(); m.Enabled() {
62 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000063 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080064 mixedBuildMod.QueueBazelCall(ctx)
65 }
66 }
67 }
68}
69
70func testApexMutator(mctx android.BottomUpMutatorContext) {
71 modules := mctx.CreateVariations(apexVariationName)
72 apexInfo := android.ApexInfo{
73 ApexVariationName: apexVariationName,
74 MinSdkVersion: android.ApiLevelForTest(apexVersion),
75 }
76 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
77}
78
Paul Duffin8567f222021-03-23 00:02:06 +000079// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000080//
81// See testCc for an explanation as to how to stop using this deprecated method.
82//
83// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080084func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070085 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000086 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000087 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090088}
89
Paul Duffin8567f222021-03-23 00:02:06 +000090// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000091//
Paul Duffin8567f222021-03-23 00:02:06 +000092// Do not add any new usages of this, instead use the prepareForCcTest directly as it makes it much
Paul Duffin02a3d652021-02-24 18:51:54 +000093// easier to customize the test behavior.
94//
95// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin8567f222021-03-23 00:02:06 +000096// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000097// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
98// that it did not change the test behavior unexpectedly.
99//
100// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800101func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800102 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000103 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000104 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800105}
106
Paul Duffin8567f222021-03-23 00:02:06 +0000107// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000108//
109// See testCc for an explanation as to how to stop using this deprecated method.
110//
111// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800112func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800113 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000114 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900115 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +0800116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800118}
119
Paul Duffin8567f222021-03-23 00:02:06 +0000120// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900125func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
126 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000127 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +0900128 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900129 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +0900130
131 return testCcWithConfig(t, config)
132}
133
Paul Duffin8567f222021-03-23 00:02:06 +0000134// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000135//
136// See testCc for an explanation as to how to stop using this deprecated method.
137//
138// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900139func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800140 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800141
Paul Duffin8567f222021-03-23 00:02:06 +0000142 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000143 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
144 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800145}
146
Paul Duffin8567f222021-03-23 00:02:06 +0000147// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000148//
149// See testCc for an explanation as to how to stop using this deprecated method.
150//
151// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900152func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900153 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000154 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900156 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900157 testCcErrorWithConfig(t, pattern, config)
158 return
159}
160
Paul Duffin8567f222021-03-23 00:02:06 +0000161// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000162//
163// See testCc for an explanation as to how to stop using this deprecated method.
164//
165// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900166func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900167 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000168 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900169 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
170 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900171 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900172 testCcErrorWithConfig(t, pattern, config)
173 return
174}
175
Logan Chienf3511742017-10-31 18:04:35 +0800176const (
Colin Cross7113d202019-11-20 16:39:12 -0800177 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900178 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
179 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800180 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800181)
182
Paul Duffindb462dd2021-03-21 22:01:55 +0000183// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
184// running it in a fixture that requires all source files to exist.
185func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
186 android.GroupFixturePreparers(
187 PrepareForTestWithCcDefaultModules,
188 android.PrepareForTestDisallowNonExistentPaths,
189 ).RunTest(t)
190}
191
Jiyong Park6a43f042017-10-12 23:05:00 +0900192func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400193 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Justin Yun7f99ec72021-04-12 13:19:28 +0900220func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
221 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
222 partitionDefined := false
223 checkPartition := func(specific bool, partition string) {
224 if specific {
225 if expected != partition && !partitionDefined {
226 // The variant is installed to the 'partition'
227 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
228 }
229 partitionDefined = true
230 } else {
231 // The variant is not installed to the 'partition'
232 if expected == partition {
233 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
234 }
235 }
236 }
237 socSpecific := func(m *Module) bool {
238 return m.SocSpecific() || m.socSpecificModuleContext()
239 }
240 deviceSpecific := func(m *Module) bool {
241 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
242 }
243 productSpecific := func(m *Module) bool {
244 return m.ProductSpecific() || m.productSpecificModuleContext()
245 }
246 systemExtSpecific := func(m *Module) bool {
247 return m.SystemExtSpecific()
248 }
249 checkPartition(socSpecific(mod), "vendor")
250 checkPartition(deviceSpecific(mod), "odm")
251 checkPartition(productSpecific(mod), "product")
252 checkPartition(systemExtSpecific(mod), "system_ext")
253 if !partitionDefined && expected != "system" {
254 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
255 " but installed to system partition", variant, name, expected)
256 }
257}
258
259func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400260 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900261 t.Helper()
262 ctx := prepareForCcTest.RunTestWithBp(t, `
263 cc_library {
264 name: "libsystem",
265 }
266 cc_library {
267 name: "libsystem_ext",
268 system_ext_specific: true,
269 }
270 cc_library {
271 name: "libproduct",
272 product_specific: true,
273 }
274 cc_library {
275 name: "libvendor",
276 vendor: true,
277 }
278 cc_library {
279 name: "libodm",
280 device_specific: true,
281 }
282 cc_library {
283 name: "liball_available",
284 vendor_available: true,
285 product_available: true,
286 }
287 cc_library {
288 name: "libsystem_ext_all_available",
289 system_ext_specific: true,
290 vendor_available: true,
291 product_available: true,
292 }
293 cc_library {
294 name: "liball_available_odm",
295 odm_available: true,
296 product_available: true,
297 }
298 cc_library {
299 name: "libproduct_vendoravailable",
300 product_specific: true,
301 vendor_available: true,
302 }
303 cc_library {
304 name: "libproduct_odmavailable",
305 product_specific: true,
306 odm_available: true,
307 }
308 `).TestContext
309
310 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
311 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
312 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
313 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
314 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
315
316 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
317 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
318 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
319
320 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
321 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
322 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
323
324 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
325 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
326 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
327
328 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
329 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
330
331 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
332 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
333}
334
Logan Chienf3511742017-10-31 18:04:35 +0800335func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900336 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800337
Logan Chiend3c59a22018-03-29 14:08:15 +0800338 t.Helper()
339
Justin Yun0ecf0b22020-02-28 15:07:59 +0900340 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800341
342 // Check library properties.
343 lib, ok := mod.compiler.(*libraryDecorator)
344 if !ok {
345 t.Errorf("%q must have libraryDecorator", name)
346 } else if lib.baseInstaller.subDir != subDir {
347 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
348 lib.baseInstaller.subDir)
349 }
350
351 // Check VNDK properties.
352 if mod.vndkdep == nil {
353 t.Fatalf("%q must have `vndkdep`", name)
354 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 if !mod.IsVndk() {
356 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800357 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400358 if mod.IsVndkSp() != isVndkSp {
359 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800360 }
361
362 // Check VNDK extension properties.
363 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500364 if mod.IsVndkExt() != isVndkExt {
365 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800366 }
367
368 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
369 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
370 }
371}
372
Jooyung Han2216fb12019-11-06 16:46:15 +0900373func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
374 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800375 content := android.ContentFromFileRuleForTests(t, params)
376 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900377 assertArrayString(t, actual, expected)
378}
379
Jooyung Han097087b2019-10-22 19:32:18 +0900380func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
381 t.Helper()
382 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900383 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
384}
385
386func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
387 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800388 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800389 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900390}
391
Logan Chienf3511742017-10-31 18:04:35 +0800392func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400393 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800394 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800395 cc_library {
396 name: "libvndk",
397 vendor_available: true,
398 vndk: {
399 enabled: true,
400 },
401 nocrt: true,
402 }
403
404 cc_library {
405 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900406 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800407 vndk: {
408 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900409 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 },
411 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900412 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800413 }
414
415 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900416 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800417 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900418 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800419 vndk: {
420 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900421 },
422 nocrt: true,
423 target: {
424 vendor: {
425 cflags: ["-DTEST"],
426 },
427 product: {
428 cflags: ["-DTEST"],
429 },
430 },
431 }
432
433 cc_library {
434 name: "libvndk_sp",
435 vendor_available: true,
436 vndk: {
437 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800438 support_system_process: true,
439 },
440 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900441 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800442 }
443
444 cc_library {
445 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900446 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800447 vndk: {
448 enabled: true,
449 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900450 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800451 },
452 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900453 target: {
454 vendor: {
455 suffix: "-x",
456 },
457 },
Logan Chienf3511742017-10-31 18:04:35 +0800458 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900459
460 cc_library {
461 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900462 vendor_available: true,
463 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900464 vndk: {
465 enabled: true,
466 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900467 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900468 },
469 nocrt: true,
470 target: {
471 vendor: {
472 suffix: "-x",
473 },
474 product: {
475 suffix: "-x",
476 },
477 },
478 }
479
Justin Yun450ae722021-04-16 19:58:18 +0900480 cc_library {
481 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700482 llndk: {
483 symbol_file: "libllndk.map.txt",
484 export_llndk_headers: ["libllndk_headers"],
485 }
Justin Yun450ae722021-04-16 19:58:18 +0900486 }
487
Justin Yun611e8862021-05-24 18:17:33 +0900488 cc_library {
489 name: "libclang_rt.hwasan-llndk",
490 llndk: {
491 symbol_file: "libclang_rt.hwasan.map.txt",
492 }
493 }
494
Colin Cross627280f2021-04-26 16:53:58 -0700495 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900496 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700497 llndk: {
498 llndk_headers: true,
499 },
Justin Yun450ae722021-04-16 19:58:18 +0900500 export_include_dirs: ["include"],
501 }
502
Colin Crosse4e44bc2020-12-28 13:50:21 -0800503 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900504 name: "llndk.libraries.txt",
505 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800506 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900507 name: "vndkcore.libraries.txt",
508 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800509 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900510 name: "vndksp.libraries.txt",
511 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800512 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900513 name: "vndkprivate.libraries.txt",
514 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800515 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900516 name: "vndkproduct.libraries.txt",
517 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800518 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900519 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800520 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900521 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800522 `
523
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000524 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800525 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900526 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900527 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800528
529 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800530
Jooyung Han261e1582020-10-20 18:54:21 +0900531 // subdir == "" because VNDK libs are not supposed to be installed separately.
532 // They are installed as part of VNDK APEX instead.
533 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
534 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900535 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900536 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
537 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900538 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900539
Justin Yun6977e8a2020-10-29 18:24:11 +0900540 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
541 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900542
Inseob Kim1f086e22019-05-09 13:29:15 +0900543 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900544 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000545 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900546
547 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
548 "arm64", "armv8-a"))
549 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
550 "arm", "armv7-a-neon"))
551
552 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
553 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900554 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
555
Inseob Kim1f086e22019-05-09 13:29:15 +0900556 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
557 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900558 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900559
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900560 variant := "android_vendor.29_arm64_armv8-a_shared"
561 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900562
Inseob Kim7f283f42020-06-01 21:53:49 +0900563 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
564
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400565 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
566 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
567 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
568 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
569 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
570 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
571 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
572 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900573
Jooyung Han39edb6c2019-11-06 16:53:07 +0900574 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800575 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
576 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
577 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
578 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
579 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900580
Jooyung Han097087b2019-10-22 19:32:18 +0900581 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
582 "LLNDK: libc.so",
583 "LLNDK: libdl.so",
584 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900585 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900586 "LLNDK: libm.so",
587 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900588 "VNDK-SP: libvndk_sp-x.so",
589 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900590 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900591 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900592 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900593 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900594 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900595 "VNDK-private: libvndk-private.so",
596 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900598 "VNDK-product: libc++.so",
599 "VNDK-product: libvndk_product.so",
600 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900601 })
Justin Yun611e8862021-05-24 18:17:33 +0900602 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libclang_rt.hwasan-llndk.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900603 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
604 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
605 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
Justin Yun8a2600c2020-12-07 12:44:03 +0900606 checkVndkLibrariesOutput(t, ctx, "vndkproduct.libraries.txt", []string{"libc++.so", "libvndk_product.so", "libvndk_sp_product_private-x.so"})
Jooyung Han2216fb12019-11-06 16:46:15 +0900607 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
608}
609
Yo Chiangbba545e2020-06-09 16:15:37 +0800610func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400611 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800612 ctx := testCc(t, `
613 cc_library {
614 name: "libvndk_host_supported",
615 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900616 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800617 vndk: {
618 enabled: true,
619 },
620 host_supported: true,
621 }
622
623 cc_library {
624 name: "libvndk_host_supported_but_disabled_on_device",
625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900626 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800627 vndk: {
628 enabled: true,
629 },
630 host_supported: true,
631 enabled: false,
632 target: {
633 host: {
634 enabled: true,
635 }
636 }
637 }
638
Colin Crosse4e44bc2020-12-28 13:50:21 -0800639 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800640 name: "vndkcore.libraries.txt",
641 }
642 `)
643
644 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
645}
646
Jooyung Han2216fb12019-11-06 16:46:15 +0900647func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400648 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800649 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800650 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900651 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800652 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800653 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000654 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800655 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900656 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
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)
Colin Crossc85750b2022-04-21 12:50:51 -07002703 actual := android.Paths(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)
Colin Crossc85750b2022-04-21 12:50:51 -07002739 actual := android.Paths(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,
Sam Delmericoef69d472023-04-18 17:32:43 -04003118 sanitize: {
3119 // cc_test modules default to memtag_heap: true,
3120 // but this adds extra dependencies that we don't care about
3121 never: true,
3122 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003123 }
3124 cc_binary {
3125 name: "binary",
3126 bazel_module: { label: "//:binary" },
3127 static_libs: ["static_dep"],
3128 whole_static_libs: ["whole_static_dep"],
3129 shared_libs: ["shared_dep"],
3130 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003131 cc_library_headers {
3132 name: "lib_headers",
3133 bazel_module: { label: "//:lib_headers" },
3134 static_libs: ["static_dep"],
3135 whole_static_libs: ["whole_static_dep"],
3136 shared_libs: ["shared_dep"],
3137 }
3138 cc_prebuilt_library {
3139 name: "lib_prebuilt",
3140 bazel_module: { label: "//:lib_prebuilt" },
3141 static_libs: ["static_dep"],
3142 whole_static_libs: ["whole_static_dep"],
3143 shared_libs: ["shared_dep"],
3144 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003145 `
3146
3147 testCases := []struct {
3148 name string
3149 moduleName string
3150 variant string
3151 androidMkInfo cquery.CcAndroidMkInfo
3152 }{
3153 {
3154 name: "shared lib",
3155 moduleName: "lib",
3156 variant: "android_arm64_armv8-a_shared",
3157 androidMkInfo: cquery.CcAndroidMkInfo{
3158 LocalStaticLibs: []string{"static_dep"},
3159 LocalWholeStaticLibs: []string{"whole_static_dep"},
3160 LocalSharedLibs: []string{"shared_dep"},
3161 },
3162 },
3163 {
3164 name: "static lib",
3165 moduleName: "lib",
3166 variant: "android_arm64_armv8-a_static",
3167 androidMkInfo: cquery.CcAndroidMkInfo{
3168 LocalStaticLibs: []string{"static_dep"},
3169 LocalWholeStaticLibs: []string{"whole_static_dep"},
3170 LocalSharedLibs: []string{"shared_dep"},
3171 },
3172 },
3173 {
3174 name: "cc_test arm64",
3175 moduleName: "test",
3176 variant: "android_arm64_armv8-a",
3177 androidMkInfo: cquery.CcAndroidMkInfo{
3178 LocalStaticLibs: []string{"static_dep"},
3179 LocalWholeStaticLibs: []string{"whole_static_dep"},
3180 LocalSharedLibs: []string{"shared_dep"},
3181 },
3182 },
3183 {
3184 name: "cc_test arm",
3185 moduleName: "test",
3186 variant: "android_arm_armv7-a-neon",
3187 androidMkInfo: cquery.CcAndroidMkInfo{
3188 LocalStaticLibs: []string{"static_dep"},
3189 LocalWholeStaticLibs: []string{"whole_static_dep"},
3190 LocalSharedLibs: []string{"shared_dep"},
3191 },
3192 },
3193 {
3194 name: "cc_binary",
3195 moduleName: "binary",
3196 variant: "android_arm64_armv8-a",
3197 androidMkInfo: cquery.CcAndroidMkInfo{
3198 LocalStaticLibs: []string{"static_dep"},
3199 LocalWholeStaticLibs: []string{"whole_static_dep"},
3200 LocalSharedLibs: []string{"shared_dep"},
3201 },
3202 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003203 {
3204 name: "cc_library_headers",
3205 moduleName: "lib_headers",
3206 variant: "android_arm64_armv8-a",
3207 androidMkInfo: cquery.CcAndroidMkInfo{
3208 LocalStaticLibs: []string{"static_dep"},
3209 LocalWholeStaticLibs: []string{"whole_static_dep"},
3210 LocalSharedLibs: []string{"shared_dep"},
3211 },
3212 },
3213 {
3214 name: "prebuilt lib static",
3215 moduleName: "lib_prebuilt",
3216 variant: "android_arm64_armv8-a_static",
3217 androidMkInfo: cquery.CcAndroidMkInfo{
3218 LocalStaticLibs: []string{"static_dep"},
3219 LocalWholeStaticLibs: []string{"whole_static_dep"},
3220 LocalSharedLibs: []string{"shared_dep"},
3221 },
3222 },
3223 {
3224 name: "prebuilt lib shared",
3225 moduleName: "lib_prebuilt",
3226 variant: "android_arm64_armv8-a_shared",
3227 androidMkInfo: cquery.CcAndroidMkInfo{
3228 LocalStaticLibs: []string{"static_dep"},
3229 LocalWholeStaticLibs: []string{"whole_static_dep"},
3230 LocalSharedLibs: []string{"shared_dep"},
3231 },
3232 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003233 }
3234
3235 outputBaseDir := "out/bazel"
3236 for _, tc := range testCases {
3237 t.Run(tc.name, func(t *testing.T) {
3238 result := android.GroupFixturePreparers(
3239 prepareForCcTest,
3240 android.FixtureModifyConfig(func(config android.Config) {
3241 config.BazelContext = android.MockBazelContext{
3242 OutputBaseDir: outputBaseDir,
3243 LabelToCcInfo: map[string]cquery.CcInfo{
3244 "//:lib": cquery.CcInfo{
3245 CcAndroidMkInfo: tc.androidMkInfo,
3246 RootDynamicLibraries: []string{""},
3247 },
3248 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3249 CcAndroidMkInfo: tc.androidMkInfo,
3250 RootStaticArchives: []string{""},
3251 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003252 "//:lib_headers": cquery.CcInfo{
3253 CcAndroidMkInfo: tc.androidMkInfo,
3254 OutputFiles: []string{""},
3255 },
3256 "//:lib_prebuilt": cquery.CcInfo{
3257 CcAndroidMkInfo: tc.androidMkInfo,
3258 },
3259 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3260 CcAndroidMkInfo: tc.androidMkInfo,
3261 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003262 },
3263 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003264 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003265 CcAndroidMkInfo: tc.androidMkInfo,
3266 },
3267 "//:binary": cquery.CcUnstrippedInfo{
3268 CcAndroidMkInfo: tc.androidMkInfo,
3269 },
3270 },
3271 }
3272 }),
3273 ).RunTestWithBp(t, bp)
3274 ctx := result.TestContext
3275
3276 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3277 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003278 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3279 t.Errorf("incorrect static_libs"+
3280 "\nactual: %v"+
3281 "\nexpected: %v",
3282 module.Properties.AndroidMkStaticLibs,
3283 tc.androidMkInfo.LocalStaticLibs,
3284 )
3285 }
3286 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3287 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3288 tc.androidMkInfo.LocalStaticLibs,
3289 )
3290 if staticDepsDiffer {
3291 t.Errorf(
3292 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3293 tc.androidMkInfo.LocalStaticLibs,
3294 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3295 missingStaticDeps,
3296 additionalStaticDeps,
3297 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003298 }
3299
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003300 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3301 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3302 tc.androidMkInfo.LocalWholeStaticLibs,
3303 module.Properties.AndroidMkWholeStaticLibs,
3304 )
3305 }
3306 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3307 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3308 tc.androidMkInfo.LocalWholeStaticLibs,
3309 )
3310 if wholeStaticDepsDiffer {
3311 t.Errorf(
3312 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3313 tc.androidMkInfo.LocalWholeStaticLibs,
3314 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3315 missingWholeStaticDeps,
3316 additionalWholeStaticDeps,
3317 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003318 }
3319
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003320 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3321 t.Errorf("incorrect shared_libs"+
3322 "\nactual: %v"+
3323 "\nexpected: %v",
3324 module.Properties.AndroidMkSharedLibs,
3325 tc.androidMkInfo.LocalSharedLibs,
3326 )
3327 }
3328 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3329 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3330 tc.androidMkInfo.LocalSharedLibs,
3331 )
3332 if sharedDepsDiffer {
3333 t.Errorf(
3334 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3335 tc.androidMkInfo.LocalSharedLibs,
3336 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3337 missingSharedDeps,
3338 additionalSharedDeps,
3339 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003340 }
3341 })
3342 }
3343}
3344
Jiyong Parkd08b6972017-09-26 10:50:54 +09003345var compilerFlagsTestCases = []struct {
3346 in string
3347 out bool
3348}{
3349 {
3350 in: "a",
3351 out: false,
3352 },
3353 {
3354 in: "-a",
3355 out: true,
3356 },
3357 {
3358 in: "-Ipath/to/something",
3359 out: false,
3360 },
3361 {
3362 in: "-isystempath/to/something",
3363 out: false,
3364 },
3365 {
3366 in: "--coverage",
3367 out: false,
3368 },
3369 {
3370 in: "-include a/b",
3371 out: true,
3372 },
3373 {
3374 in: "-include a/b c/d",
3375 out: false,
3376 },
3377 {
3378 in: "-DMACRO",
3379 out: true,
3380 },
3381 {
3382 in: "-DMAC RO",
3383 out: false,
3384 },
3385 {
3386 in: "-a -b",
3387 out: false,
3388 },
3389 {
3390 in: "-DMACRO=definition",
3391 out: true,
3392 },
3393 {
3394 in: "-DMACRO=defi nition",
3395 out: true, // TODO(jiyong): this should be false
3396 },
3397 {
3398 in: "-DMACRO(x)=x + 1",
3399 out: true,
3400 },
3401 {
3402 in: "-DMACRO=\"defi nition\"",
3403 out: true,
3404 },
3405}
3406
3407type mockContext struct {
3408 BaseModuleContext
3409 result bool
3410}
3411
3412func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3413 // CheckBadCompilerFlags calls this function when the flag should be rejected
3414 ctx.result = false
3415}
3416
3417func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003418 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003419 for _, testCase := range compilerFlagsTestCases {
3420 ctx := &mockContext{result: true}
3421 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3422 if ctx.result != testCase.out {
3423 t.Errorf("incorrect output:")
3424 t.Errorf(" input: %#v", testCase.in)
3425 t.Errorf(" expected: %#v", testCase.out)
3426 t.Errorf(" got: %#v", ctx.result)
3427 }
3428 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003429}
Jiyong Park374510b2018-03-19 18:23:01 +09003430
Jiyong Park37b25202018-07-11 10:49:27 +09003431func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003432 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003433 ctx := testCc(t, `
3434 cc_library_shared {
3435 name: "librecovery",
3436 recovery: true,
3437 }
3438 cc_library_shared {
3439 name: "librecovery32",
3440 recovery: true,
3441 compile_multilib:"32",
3442 }
Jiyong Park5baac542018-08-28 09:55:37 +09003443 cc_library_shared {
3444 name: "libHalInRecovery",
3445 recovery_available: true,
3446 vendor: true,
3447 }
Jiyong Park37b25202018-07-11 10:49:27 +09003448 `)
3449
3450 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003451 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003452 if len(variants) != 1 || !android.InList(arm64, variants) {
3453 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3454 }
3455
3456 variants = ctx.ModuleVariantsForTests("librecovery32")
3457 if android.InList(arm64, variants) {
3458 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3459 }
Jiyong Park5baac542018-08-28 09:55:37 +09003460
3461 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3462 if !recoveryModule.Platform() {
3463 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3464 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003465}
Jiyong Park5baac542018-08-28 09:55:37 +09003466
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003467func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003468 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003469 bp := `
3470 cc_prebuilt_test_library_shared {
3471 name: "test_lib",
3472 relative_install_path: "foo/bar/baz",
3473 srcs: ["srcpath/dontusethispath/baz.so"],
3474 }
3475
3476 cc_test {
3477 name: "main_test",
3478 data_libs: ["test_lib"],
3479 gtest: false,
3480 }
3481 `
3482
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003483 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003484 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003485 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003486 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3487
3488 ctx := testCcWithConfig(t, config)
3489 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3490 testBinary := module.(*Module).linker.(*testBinary)
3491 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3492 if err != nil {
3493 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3494 }
3495 if len(outputFiles) != 1 {
3496 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3497 }
3498 if len(testBinary.dataPaths()) != 1 {
3499 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3500 }
3501
3502 outputPath := outputFiles[0].String()
3503
3504 if !strings.HasSuffix(outputPath, "/main_test") {
3505 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3506 }
Colin Crossaa255532020-07-03 13:18:24 -07003507 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003508 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3509 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3510 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3511 }
3512}
3513
Jiyong Park7ed9de32018-10-15 22:25:07 +09003514func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003515 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003516 ctx := testCc(t, `
3517 cc_library_shared {
3518 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003519 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003520 stubs: {
3521 symbol_file: "foo.map.txt",
3522 versions: ["1", "2", "3"],
3523 },
3524 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003525
Jiyong Park7ed9de32018-10-15 22:25:07 +09003526 cc_library_shared {
3527 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003528 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003529 shared_libs: ["libFoo#1"],
3530 }`)
3531
3532 variants := ctx.ModuleVariantsForTests("libFoo")
3533 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003534 "android_arm64_armv8-a_shared",
3535 "android_arm64_armv8-a_shared_1",
3536 "android_arm64_armv8-a_shared_2",
3537 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003538 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003539 "android_arm_armv7-a-neon_shared",
3540 "android_arm_armv7-a-neon_shared_1",
3541 "android_arm_armv7-a-neon_shared_2",
3542 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003543 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003544 }
3545 variantsMismatch := false
3546 if len(variants) != len(expectedVariants) {
3547 variantsMismatch = true
3548 } else {
3549 for _, v := range expectedVariants {
3550 if !inList(v, variants) {
3551 variantsMismatch = false
3552 }
3553 }
3554 }
3555 if variantsMismatch {
3556 t.Errorf("variants of libFoo expected:\n")
3557 for _, v := range expectedVariants {
3558 t.Errorf("%q\n", v)
3559 }
3560 t.Errorf(", but got:\n")
3561 for _, v := range variants {
3562 t.Errorf("%q\n", v)
3563 }
3564 }
3565
Colin Cross7113d202019-11-20 16:39:12 -08003566 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003567 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003568 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003569 if !strings.Contains(libFlags, libFoo1StubPath) {
3570 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3571 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003572
Colin Cross7113d202019-11-20 16:39:12 -08003573 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003574 cFlags := libBarCompileRule.Args["cFlags"]
3575 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3576 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3577 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3578 }
Jiyong Park37b25202018-07-11 10:49:27 +09003579}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003580
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003581func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3582 t.Parallel()
3583 ctx := testCc(t, `
3584 cc_library_shared {
3585 name: "libFoo",
3586 srcs: ["foo.c"],
3587 stubs: {
3588 symbol_file: "foo.map.txt",
3589 versions: ["current"],
3590 },
3591 apex_available: ["bar", "a1"],
3592 }
3593
3594 cc_library_shared {
3595 name: "libBar",
3596 srcs: ["bar.c"],
3597 shared_libs: ["libFoo"],
3598 apex_available: ["a1"],
3599 }
3600
3601 cc_library_shared {
3602 name: "libA1",
3603 srcs: ["a1.c"],
3604 shared_libs: ["libFoo"],
3605 apex_available: ["a1"],
3606 }
3607
3608 cc_library_shared {
3609 name: "libBarA1",
3610 srcs: ["bara1.c"],
3611 shared_libs: ["libFoo"],
3612 apex_available: ["bar", "a1"],
3613 }
3614
3615 cc_library_shared {
3616 name: "libAnyApex",
3617 srcs: ["anyApex.c"],
3618 shared_libs: ["libFoo"],
3619 apex_available: ["//apex_available:anyapex"],
3620 }
3621
3622 cc_library_shared {
3623 name: "libBaz",
3624 srcs: ["baz.c"],
3625 shared_libs: ["libFoo"],
3626 apex_available: ["baz"],
3627 }
3628
3629 cc_library_shared {
3630 name: "libQux",
3631 srcs: ["qux.c"],
3632 shared_libs: ["libFoo"],
3633 apex_available: ["qux", "bar"],
3634 }`)
3635
3636 variants := ctx.ModuleVariantsForTests("libFoo")
3637 expectedVariants := []string{
3638 "android_arm64_armv8-a_shared",
3639 "android_arm64_armv8-a_shared_current",
3640 "android_arm_armv7-a-neon_shared",
3641 "android_arm_armv7-a-neon_shared_current",
3642 }
3643 variantsMismatch := false
3644 if len(variants) != len(expectedVariants) {
3645 variantsMismatch = true
3646 } else {
3647 for _, v := range expectedVariants {
3648 if !inList(v, variants) {
3649 variantsMismatch = false
3650 }
3651 }
3652 }
3653 if variantsMismatch {
3654 t.Errorf("variants of libFoo expected:\n")
3655 for _, v := range expectedVariants {
3656 t.Errorf("%q\n", v)
3657 }
3658 t.Errorf(", but got:\n")
3659 for _, v := range variants {
3660 t.Errorf("%q\n", v)
3661 }
3662 }
3663
3664 linkAgainstFoo := []string{"libBarA1"}
3665 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3666
3667 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3668 for _, lib := range linkAgainstFoo {
3669 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3670 libFlags := libLinkRule.Args["libFlags"]
3671 if !strings.Contains(libFlags, libFooPath) {
3672 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3673 }
3674 }
3675
3676 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3677 for _, lib := range linkAgainstFooStubs {
3678 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3679 libFlags := libLinkRule.Args["libFlags"]
3680 if !strings.Contains(libFlags, libFooStubPath) {
3681 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3682 }
3683 }
3684}
3685
Sam Delmerico75dbca22023-04-20 13:13:25 +00003686func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003687 t.Parallel()
3688 bp := `
3689 cc_library_shared {
3690 name: "libFoo",
3691 bazel_module: { label: "//:libFoo" },
3692 srcs: ["foo.c"],
3693 stubs: {
3694 symbol_file: "foo.map.txt",
3695 versions: ["current"],
3696 },
3697 apex_available: ["bar", "a1"],
3698 }
3699
3700 cc_library_shared {
3701 name: "libBar",
3702 srcs: ["bar.c"],
3703 shared_libs: ["libFoo"],
3704 apex_available: ["a1"],
3705 }
3706
3707 cc_library_shared {
3708 name: "libA1",
3709 srcs: ["a1.c"],
3710 shared_libs: ["libFoo"],
3711 apex_available: ["a1"],
3712 }
3713
3714 cc_library_shared {
3715 name: "libBarA1",
3716 srcs: ["bara1.c"],
3717 shared_libs: ["libFoo"],
3718 apex_available: ["bar", "a1"],
3719 }
3720
3721 cc_library_shared {
3722 name: "libAnyApex",
3723 srcs: ["anyApex.c"],
3724 shared_libs: ["libFoo"],
3725 apex_available: ["//apex_available:anyapex"],
3726 }
3727
3728 cc_library_shared {
3729 name: "libBaz",
3730 srcs: ["baz.c"],
3731 shared_libs: ["libFoo"],
3732 apex_available: ["baz"],
3733 }
3734
3735 cc_library_shared {
3736 name: "libQux",
3737 srcs: ["qux.c"],
3738 shared_libs: ["libFoo"],
3739 apex_available: ["qux", "bar"],
3740 }`
3741
3742 result := android.GroupFixturePreparers(
3743 prepareForCcTest,
3744 android.FixtureModifyConfig(func(config android.Config) {
3745 config.BazelContext = android.MockBazelContext{
3746 OutputBaseDir: "out/bazel",
3747 LabelToCcInfo: map[string]cquery.CcInfo{
3748 "//:libFoo": {
3749 RootDynamicLibraries: []string{"libFoo.so"},
3750 },
3751 "//:libFoo_stub_libs-current": {
3752 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3753 },
3754 },
3755 }
3756 }),
3757 ).RunTestWithBp(t, bp)
3758 ctx := result.TestContext
3759
3760 variants := ctx.ModuleVariantsForTests("libFoo")
3761 expectedVariants := []string{
3762 "android_arm64_armv8-a_shared",
3763 "android_arm64_armv8-a_shared_current",
3764 "android_arm_armv7-a-neon_shared",
3765 "android_arm_armv7-a-neon_shared_current",
3766 }
3767 variantsMismatch := false
3768 if len(variants) != len(expectedVariants) {
3769 variantsMismatch = true
3770 } else {
3771 for _, v := range expectedVariants {
3772 if !inList(v, variants) {
3773 variantsMismatch = false
3774 }
3775 }
3776 }
3777 if variantsMismatch {
3778 t.Errorf("variants of libFoo expected:\n")
3779 for _, v := range expectedVariants {
3780 t.Errorf("%q\n", v)
3781 }
3782 t.Errorf(", but got:\n")
3783 for _, v := range variants {
3784 t.Errorf("%q\n", v)
3785 }
3786 }
3787
3788 linkAgainstFoo := []string{"libBarA1"}
3789 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3790
3791 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3792 for _, lib := range linkAgainstFoo {
3793 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3794 libFlags := libLinkRule.Args["libFlags"]
3795 if !strings.Contains(libFlags, libFooPath) {
3796 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3797 }
3798 }
3799
3800 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3801 for _, lib := range linkAgainstFooStubs {
3802 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3803 libFlags := libLinkRule.Args["libFlags"]
3804 if !strings.Contains(libFlags, libFooStubPath) {
3805 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3806 }
3807 }
3808}
3809
Jooyung Hanb04a4992020-03-13 18:57:35 +09003810func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003811 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003812 for _, tc := range []struct{ moduleName, expected string }{
3813 {"libc", "__LIBC_API__"},
3814 {"libfoo", "__LIBFOO_API__"},
3815 {"libfoo@1", "__LIBFOO_1_API__"},
3816 {"libfoo-v1", "__LIBFOO_V1_API__"},
3817 {"libfoo.v1", "__LIBFOO_V1_API__"},
3818 } {
3819 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3820 }
3821}
3822
Liz Kammer83cf81b2022-09-22 08:24:20 -04003823func pathsToBase(paths android.Paths) []string {
3824 var ret []string
3825 for _, p := range paths {
3826 ret = append(ret, p.Base())
3827 }
3828 return ret
3829}
3830
3831func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003832 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003833 ctx := testCc(t, `
3834 cc_library_static {
3835 name: "foo",
3836 srcs: ["foo.c"],
3837 }
3838
3839 cc_library_static {
3840 name: "bar",
3841 srcs: ["bar.c"],
3842 }
3843
3844 cc_library_shared {
3845 name: "qux",
3846 srcs: ["qux.c"],
3847 }
3848
3849 cc_library_static {
3850 name: "baz",
3851 srcs: ["baz.c"],
3852 static_libs: ["foo"],
3853 shared_libs: ["qux"],
3854 whole_static_libs: ["bar"],
3855 }`)
3856
3857 variant := "android_arm64_armv8-a_static"
3858 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3859
3860 // For static libraries, the object files of a whole static dep are included in the archive
3861 // directly
3862 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3863 t.Errorf("Expected input objects %q, got %q", w, g)
3864 }
3865
3866 // non whole static dependencies are not linked into the archive
3867 if len(arRule.Implicits) > 0 {
3868 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3869 }
3870}
3871
3872func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003873 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003874 ctx := testCc(t, `
3875 cc_library_static {
3876 name: "foo",
3877 srcs: ["foo.c"],
3878 }
3879
3880 cc_library_static {
3881 name: "bar",
3882 srcs: ["bar.c"],
3883 }
3884
3885 cc_library_shared {
3886 name: "qux",
3887 srcs: ["qux.c"],
3888 }
3889
3890 cc_library_shared {
3891 name: "baz",
3892 srcs: ["baz.c"],
3893 static_libs: ["foo"],
3894 shared_libs: ["qux"],
3895 whole_static_libs: ["bar"],
3896 }`)
3897
3898 variant := "android_arm64_armv8-a_shared"
3899 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3900 libFlags := linkRule.Args["libFlags"]
3901 // When dynamically linking, we expect static dependencies to be found on the command line
3902 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3903 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3904 }
3905 // When dynamically linking, we expect whole static dependencies to be found on the command line
3906 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3907 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3908 }
3909
3910 // When dynamically linking, we expect shared dependencies to be found on the command line
3911 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3912 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3913 }
3914
3915 // We should only have the objects from the shared library srcs, not the whole static dependencies
3916 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3917 t.Errorf("Expected input objects %q, got %q", w, g)
3918 }
3919}
3920
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003921func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003922 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003923 ctx := testCc(t, `
3924 cc_binary {
3925 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003926 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003927 static_executable: true,
3928 }`)
3929
Colin Cross7113d202019-11-20 16:39:12 -08003930 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003931 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3932 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003933 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003934 for _, lib := range systemStaticLibs {
3935 if !strings.Contains(libFlags, lib) {
3936 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3937 }
3938 }
3939 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3940 for _, lib := range systemSharedLibs {
3941 if strings.Contains(libFlags, lib) {
3942 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3943 }
3944 }
3945}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003946
3947func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003948 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003949 ctx := testCc(t, `
3950 cc_binary {
3951 name: "mybin",
3952 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003953 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003954 static_executable: true,
3955 stl: "none",
3956 }
3957
3958 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003959 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003960 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003961 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003962 stl: "none",
3963 }
3964
3965 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003966 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003967 srcs: ["foo.c"],
3968 stl: "none",
3969 stubs: {
3970 versions: ["1"],
3971 },
3972 }`)
3973
Colin Cross0de8a1e2020-09-18 14:15:30 -07003974 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3975 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003976 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003977
3978 if !reflect.DeepEqual(actual, expected) {
3979 t.Errorf("staticDeps orderings were not propagated correctly"+
3980 "\nactual: %v"+
3981 "\nexpected: %v",
3982 actual,
3983 expected,
3984 )
3985 }
3986}
Jooyung Han38002912019-05-16 04:01:54 +09003987
Jooyung Hand48f3c32019-08-23 11:18:57 +09003988func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003989 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003990 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3991 cc_library {
3992 name: "libA",
3993 srcs: ["foo.c"],
3994 shared_libs: ["libB"],
3995 stl: "none",
3996 }
3997
3998 cc_library {
3999 name: "libB",
4000 srcs: ["foo.c"],
4001 enabled: false,
4002 stl: "none",
4003 }
4004 `)
4005}
4006
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004007func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
4008 bp := `
4009 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00004010 name: "test_afl_fuzz_target",
4011 srcs: ["foo.c"],
4012 host_supported: true,
4013 static_libs: [
4014 "afl_fuzz_static_lib",
4015 ],
4016 shared_libs: [
4017 "afl_fuzz_shared_lib",
4018 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004019 fuzzing_frameworks: {
4020 afl: true,
4021 libfuzzer: false,
4022 },
Cory Barkera1da26f2022-06-07 20:12:06 +00004023 }
4024 cc_library {
4025 name: "afl_fuzz_static_lib",
4026 host_supported: true,
4027 srcs: ["static_file.c"],
4028 }
4029 cc_library {
4030 name: "libfuzzer_only_static_lib",
4031 host_supported: true,
4032 srcs: ["static_file.c"],
4033 }
4034 cc_library {
4035 name: "afl_fuzz_shared_lib",
4036 host_supported: true,
4037 srcs: ["shared_file.c"],
4038 static_libs: [
4039 "second_static_lib",
4040 ],
4041 }
4042 cc_library_headers {
4043 name: "libafl_headers",
4044 vendor_available: true,
4045 host_supported: true,
4046 export_include_dirs: [
4047 "include",
4048 "instrumentation",
4049 ],
4050 }
4051 cc_object {
4052 name: "afl-compiler-rt",
4053 vendor_available: true,
4054 host_supported: true,
4055 cflags: [
4056 "-fPIC",
4057 ],
4058 srcs: [
4059 "instrumentation/afl-compiler-rt.o.c",
4060 ],
4061 }
4062 cc_library {
4063 name: "second_static_lib",
4064 host_supported: true,
4065 srcs: ["second_file.c"],
4066 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004067 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00004068 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004069 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00004070 srcs: [
4071 "aflpp_driver.c",
4072 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004073 }`
4074
4075 testEnv := map[string]string{
4076 "FUZZ_FRAMEWORK": "AFL",
4077 }
4078
4079 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00004080
4081 checkPcGuardFlag := func(
4082 modName string, variantName string, shouldHave bool) {
4083 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
4084
4085 cFlags, ok := cc.Args["cFlags"]
4086 if !ok {
4087 t.Errorf("Could not find cFlags for module %s and variant %s",
4088 modName, variantName)
4089 }
4090
4091 if strings.Contains(
4092 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
4093 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
4094 "Test failed for module %s and variant %s",
4095 !shouldHave, shouldHave, modName, variantName)
4096 }
4097 }
4098
Cory Barkera1da26f2022-06-07 20:12:06 +00004099 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004100 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004101
4102 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004103 checkPcGuardFlag(moduleName, variant+"_static", false)
4104 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004105
4106 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004107 checkPcGuardFlag(moduleName, variant+"_static", false)
4108 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004109
4110 ctx.ModuleForTests("afl_fuzz_shared_lib",
4111 "android_arm64_armv8-a_shared").Rule("cc")
4112 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004113 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
4114}
4115
4116func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004117 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004118 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
4119}
4120
4121func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004122 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004123 if runtime.GOOS != "linux" {
4124 t.Skip("requires linux")
4125 }
4126
4127 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00004128}
4129
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004130// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4131// correctly.
4132func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004133 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004134 ctx := testCc(t, `
4135 cc_fuzz {
4136 name: "fuzz_smoke_test",
4137 srcs: ["foo.c"],
4138 }`)
4139
Paul Duffin075c4172019-12-19 19:06:13 +00004140 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004141 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4142}
4143
Jooyung Han38002912019-05-16 04:01:54 +09004144func assertString(t *testing.T, got, expected string) {
4145 t.Helper()
4146 if got != expected {
4147 t.Errorf("expected %q got %q", expected, got)
4148 }
4149}
4150
4151func assertArrayString(t *testing.T, got, expected []string) {
4152 t.Helper()
4153 if len(got) != len(expected) {
4154 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4155 return
4156 }
4157 for i := range got {
4158 if got[i] != expected[i] {
4159 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4160 i, expected[i], expected, got[i], got)
4161 return
4162 }
4163 }
4164}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004165
Jooyung Han0302a842019-10-30 18:43:49 +09004166func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4167 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004168 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004169}
4170
Colin Crosse1bb5d02019-09-24 14:55:04 -07004171func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004172 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004173 ctx := testCc(t, `
4174 cc_defaults {
4175 name: "defaults",
4176 srcs: ["foo.c"],
4177 static: {
4178 srcs: ["bar.c"],
4179 },
4180 shared: {
4181 srcs: ["baz.c"],
4182 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004183 bazel_module: {
4184 bp2build_available: true,
4185 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004186 }
4187
4188 cc_library_static {
4189 name: "libstatic",
4190 defaults: ["defaults"],
4191 }
4192
4193 cc_library_shared {
4194 name: "libshared",
4195 defaults: ["defaults"],
4196 }
4197
4198 cc_library {
4199 name: "libboth",
4200 defaults: ["defaults"],
4201 }
4202
4203 cc_binary {
4204 name: "binary",
4205 defaults: ["defaults"],
4206 }`)
4207
Colin Cross7113d202019-11-20 16:39:12 -08004208 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004209 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4210 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4211 }
Colin Cross7113d202019-11-20 16:39:12 -08004212 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004213 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4214 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4215 }
Colin Cross7113d202019-11-20 16:39:12 -08004216 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004217 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4218 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4219 }
4220
Colin Cross7113d202019-11-20 16:39:12 -08004221 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004222 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4223 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4224 }
Colin Cross7113d202019-11-20 16:39:12 -08004225 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004226 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4227 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4228 }
4229}
Colin Crosseabaedd2020-02-06 17:01:55 -08004230
4231func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004232 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004233 bp := `
4234 cc_defaults {
4235 name: "libfoo_defaults",
4236 srcs: ["foo.c"],
4237 cppflags: ["-DFOO"],
4238 product_variables: {
4239 debuggable: {
4240 cppflags: ["-DBAR"],
4241 },
4242 },
4243 }
4244
4245 cc_library {
4246 name: "libfoo",
4247 defaults: ["libfoo_defaults"],
4248 }
4249 `
4250
Paul Duffin8567f222021-03-23 00:02:06 +00004251 result := android.GroupFixturePreparers(
4252 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004253 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004254
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004255 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4256 variables.Debuggable = BoolPtr(true)
4257 }),
4258 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004259
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004260 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004261 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004262}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004263
4264func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4265 t.Parallel()
4266 bp := `
4267 cc_library_static {
4268 name: "libfoo",
4269 srcs: ["foo.c"],
4270 whole_static_libs: ["libbar"],
4271 }
4272
4273 cc_library_static {
4274 name: "libbar",
4275 whole_static_libs: ["libmissing"],
4276 }
4277 `
4278
Paul Duffin8567f222021-03-23 00:02:06 +00004279 result := android.GroupFixturePreparers(
4280 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004281 android.PrepareForTestWithAllowMissingDependencies,
4282 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004283
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004284 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004285 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004286
Paul Duffine84b1332021-03-12 11:59:43 +00004287 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004288
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004289 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004290 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004291}
Colin Crosse9fe2942020-11-10 18:12:15 -08004292
4293func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004294 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004295 bp := `
4296 cc_binary {
4297 name: "bin",
4298 host_supported: true,
4299 shared_libs: ["libshared"],
4300 runtime_libs: ["libruntime"],
4301 srcs: [":gen"],
4302 }
4303
4304 cc_library_shared {
4305 name: "libshared",
4306 host_supported: true,
4307 shared_libs: ["libtransitive"],
4308 }
4309
4310 cc_library_shared {
4311 name: "libtransitive",
4312 host_supported: true,
4313 }
4314
4315 cc_library_shared {
4316 name: "libruntime",
4317 host_supported: true,
4318 }
4319
4320 cc_binary_host {
4321 name: "tool",
4322 srcs: ["foo.cpp"],
4323 }
4324
4325 genrule {
4326 name: "gen",
4327 tools: ["tool"],
4328 out: ["gen.cpp"],
4329 cmd: "$(location tool) $(out)",
4330 }
4331 `
4332
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004333 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004334 ctx := testCcWithConfig(t, config)
4335
4336 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4337 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4338 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4339 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4340 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4341
4342 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4343 t.Errorf("expected host bin dependency %q, got %q", w, g)
4344 }
4345
4346 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4347 t.Errorf("expected host bin dependency %q, got %q", w, g)
4348 }
4349
4350 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4351 t.Errorf("expected host bin dependency %q, got %q", w, g)
4352 }
4353
4354 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4355 t.Errorf("expected host bin dependency %q, got %q", w, g)
4356 }
4357
4358 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4359 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4360 }
4361
4362 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4363 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4364 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4365 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4366
4367 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4368 t.Errorf("expected device bin dependency %q, got %q", w, g)
4369 }
4370
4371 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4372 t.Errorf("expected device bin dependency %q, got %q", w, g)
4373 }
4374
4375 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4376 t.Errorf("expected device bin dependency %q, got %q", w, g)
4377 }
4378
4379 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4380 t.Errorf("expected device bin dependency %q, got %q", w, g)
4381 }
4382
4383 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4384 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4385 }
4386
4387}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004388
4389func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004390 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004391 ctx := testCc(t, `
4392 cc_library_shared {
4393 name: "libclient",
4394 srcs: ["foo.c"],
4395 shared_libs: ["libfoo#1"],
4396 }
4397
4398 cc_library_shared {
4399 name: "libfoo",
4400 srcs: ["foo.c"],
4401 shared_libs: ["libbar"],
4402 export_shared_lib_headers: ["libbar"],
4403 stubs: {
4404 symbol_file: "foo.map.txt",
4405 versions: ["1", "2", "3"],
4406 },
4407 }
4408
4409 cc_library_shared {
4410 name: "libbar",
4411 export_include_dirs: ["include/libbar"],
4412 srcs: ["foo.c"],
4413 }`)
4414
4415 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4416
4417 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4418 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4419 }
4420}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004421
Vinh Tran09581952023-05-16 16:03:20 -04004422func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004423 t.Parallel()
4424 ctx := android.GroupFixturePreparers(
4425 prepareForCcTest,
4426 aidl_library.PrepareForTestWithAidlLibrary,
4427 android.MockFS{
4428 "package_bar/Android.bp": []byte(`
4429 aidl_library {
4430 name: "bar",
4431 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004432 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004433 strip_import_prefix: "x",
4434 }
4435 `)}.AddToFixture(),
4436 android.MockFS{
4437 "package_foo/Android.bp": []byte(`
4438 aidl_library {
4439 name: "foo",
4440 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004441 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004442 strip_import_prefix: "a",
4443 deps: ["bar"],
4444 }
4445 cc_library {
4446 name: "libfoo",
4447 aidl: {
4448 libs: ["foo"],
4449 }
4450 }
4451 `),
4452 }.AddToFixture(),
4453 ).RunTest(t).TestContext
4454
4455 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004456
4457 android.AssertPathsRelativeToTopEquals(
4458 t,
4459 "aidl headers",
4460 []string{
4461 "package_bar/x/HeaderBar.aidl",
4462 "package_foo/a/HeaderFoo.aidl",
4463 "package_foo/a/b/Foo.aidl",
4464 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4465 },
4466 libfoo.Rule("aidl_library").Implicits,
4467 )
4468
4469 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004470 aidlCommand := manifest.Commands[0].GetCommand()
4471
4472 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4473 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4474 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4475 }
4476
4477 outputs := strings.Join(libfoo.AllOutputs(), " ")
4478
Vinh Tran09581952023-05-16 16:03:20 -04004479 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4480 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4481 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004482 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4483 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004484 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4485 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4486 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004487 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4488}
4489
Jooyung Hane197d8b2021-01-05 10:33:16 +09004490func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004491 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004492 ctx := android.GroupFixturePreparers(
4493 prepareForCcTest,
4494 aidl_library.PrepareForTestWithAidlLibrary,
4495 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004496 cc_library {
4497 name: "libfoo",
4498 srcs: ["a/Foo.aidl"],
4499 aidl: { flags: ["-Werror"], },
4500 }
4501 `)
4502
4503 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4504 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4505 aidlCommand := manifest.Commands[0].GetCommand()
4506 expectedAidlFlag := "-Werror"
4507 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4508 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4509 }
4510}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004511
Jooyung Han07f70c02021-11-06 07:08:45 +09004512func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004513 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004514 for _, tc := range []struct {
4515 name string
4516 sdkVersion string
4517 variant string
4518 expected string
4519 }{
4520 {
4521 name: "default is current",
4522 sdkVersion: "",
4523 variant: "android_arm64_armv8-a_static",
4524 expected: "platform_apis",
4525 },
4526 {
4527 name: "use sdk_version",
4528 sdkVersion: `sdk_version: "29"`,
4529 variant: "android_arm64_armv8-a_static",
4530 expected: "platform_apis",
4531 },
4532 {
4533 name: "use sdk_version(sdk variant)",
4534 sdkVersion: `sdk_version: "29"`,
4535 variant: "android_arm64_armv8-a_sdk_static",
4536 expected: "29",
4537 },
4538 {
4539 name: "use min_sdk_version",
4540 sdkVersion: `min_sdk_version: "29"`,
4541 variant: "android_arm64_armv8-a_static",
4542 expected: "29",
4543 },
4544 } {
4545 t.Run(tc.name, func(t *testing.T) {
4546 ctx := testCc(t, `
4547 cc_library {
4548 name: "libfoo",
4549 stl: "none",
4550 srcs: ["a/Foo.aidl"],
4551 `+tc.sdkVersion+`
4552 }
4553 `)
4554 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4555 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4556 aidlCommand := manifest.Commands[0].GetCommand()
4557 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4558 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4559 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4560 }
4561 })
4562 }
4563}
4564
Vinh Tran09581952023-05-16 16:03:20 -04004565func TestInvalidAidlProp(t *testing.T) {
4566 t.Parallel()
4567
4568 testCases := []struct {
4569 description string
4570 bp string
4571 }{
4572 {
4573 description: "Invalid use of aidl.libs and aidl.include_dirs",
4574 bp: `
4575 cc_library {
4576 name: "foo",
4577 aidl: {
4578 libs: ["foo_aidl"],
4579 include_dirs: ["bar/include"],
4580 }
4581 }
4582 `,
4583 },
4584 {
4585 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4586 bp: `
4587 cc_library {
4588 name: "foo",
4589 aidl: {
4590 libs: ["foo_aidl"],
4591 local_include_dirs: ["include"],
4592 }
4593 }
4594 `,
4595 },
4596 }
4597
4598 for _, testCase := range testCases {
4599 t.Run(testCase.description, func(t *testing.T) {
4600 bp := `
4601 aidl_library {
4602 name: "foo_aidl",
4603 srcs: ["Foo.aidl"],
4604 } ` + testCase.bp
4605 android.GroupFixturePreparers(
4606 prepareForCcTest,
4607 aidl_library.PrepareForTestWithAidlLibrary.
4608 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4609 ).RunTestWithBp(t, bp)
4610 })
4611 }
4612}
4613
Jiyong Parka008fb02021-03-16 17:15:53 +09004614func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004615 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004616 ctx := testCc(t, `
4617 cc_library_shared {
4618 name: "libfoo",
4619 srcs: ["foo.c"],
4620 min_sdk_version: "29",
4621 }`)
4622
4623 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4624 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4625}
4626
Vinh Tranf1924742022-06-24 16:40:11 -04004627func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004628 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004629 bp := `
4630 cc_library_shared {
4631 name: "libfoo",
4632 srcs: ["foo.c"],
4633 min_sdk_version: "S",
4634 }
4635 `
4636 result := android.GroupFixturePreparers(
4637 prepareForCcTest,
4638 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4639 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4640 }),
4641 ).RunTestWithBp(t, bp)
4642 ctx := result.TestContext
4643 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4644 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4645}
4646
Paul Duffin3cb603e2021-02-19 13:57:10 +00004647func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004648 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004649
4650 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4651 // embedded newline characters alone.
4652 trimIndentingSpaces := func(s string) string {
4653 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4654 }
4655
4656 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4657 t.Helper()
4658 expected = trimIndentingSpaces(expected)
4659 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4660 if expected != actual {
4661 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4662 }
4663 }
4664
4665 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4666
4667 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4668 t.Helper()
4669 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4670 name := module.Name()
4671
4672 for _, checker := range checkers {
4673 checker(t, name, exported)
4674 }
4675 }
4676
4677 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4678 return func(t *testing.T, name string, exported FlagExporterInfo) {
4679 t.Helper()
4680 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4681 }
4682 }
4683
4684 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4685 return func(t *testing.T, name string, exported FlagExporterInfo) {
4686 t.Helper()
4687 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4688 }
4689 }
4690
4691 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4692 return func(t *testing.T, name string, exported FlagExporterInfo) {
4693 t.Helper()
4694 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4695 }
4696 }
4697
4698 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4699 return func(t *testing.T, name string, exported FlagExporterInfo) {
4700 t.Helper()
4701 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4702 }
4703 }
4704
4705 genRuleModules := `
4706 genrule {
4707 name: "genrule_foo",
4708 cmd: "generate-foo",
4709 out: [
4710 "generated_headers/foo/generated_header.h",
4711 ],
4712 export_include_dirs: [
4713 "generated_headers",
4714 ],
4715 }
4716
4717 genrule {
4718 name: "genrule_bar",
4719 cmd: "generate-bar",
4720 out: [
4721 "generated_headers/bar/generated_header.h",
4722 ],
4723 export_include_dirs: [
4724 "generated_headers",
4725 ],
4726 }
4727 `
4728
4729 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4730 ctx := testCc(t, genRuleModules+`
4731 cc_library {
4732 name: "libfoo",
4733 srcs: ["foo.c"],
4734 export_include_dirs: ["foo/standard"],
4735 export_system_include_dirs: ["foo/system"],
4736 generated_headers: ["genrule_foo"],
4737 export_generated_headers: ["genrule_foo"],
4738 }
4739
4740 cc_library {
4741 name: "libbar",
4742 srcs: ["bar.c"],
4743 shared_libs: ["libfoo"],
4744 export_include_dirs: ["bar/standard"],
4745 export_system_include_dirs: ["bar/system"],
4746 generated_headers: ["genrule_bar"],
4747 export_generated_headers: ["genrule_bar"],
4748 }
4749 `)
4750 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4751 checkIncludeDirs(t, ctx, foo,
4752 expectedIncludeDirs(`
4753 foo/standard
4754 .intermediates/genrule_foo/gen/generated_headers
4755 `),
4756 expectedSystemIncludeDirs(`foo/system`),
4757 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4758 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4759 )
4760
4761 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4762 checkIncludeDirs(t, ctx, bar,
4763 expectedIncludeDirs(`
4764 bar/standard
4765 .intermediates/genrule_bar/gen/generated_headers
4766 `),
4767 expectedSystemIncludeDirs(`bar/system`),
4768 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4769 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4770 )
4771 })
4772
4773 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4774 ctx := testCc(t, genRuleModules+`
4775 cc_library {
4776 name: "libfoo",
4777 srcs: ["foo.c"],
4778 export_include_dirs: ["foo/standard"],
4779 export_system_include_dirs: ["foo/system"],
4780 generated_headers: ["genrule_foo"],
4781 export_generated_headers: ["genrule_foo"],
4782 }
4783
4784 cc_library {
4785 name: "libbar",
4786 srcs: ["bar.c"],
4787 whole_static_libs: ["libfoo"],
4788 export_include_dirs: ["bar/standard"],
4789 export_system_include_dirs: ["bar/system"],
4790 generated_headers: ["genrule_bar"],
4791 export_generated_headers: ["genrule_bar"],
4792 }
4793 `)
4794 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4795 checkIncludeDirs(t, ctx, foo,
4796 expectedIncludeDirs(`
4797 foo/standard
4798 .intermediates/genrule_foo/gen/generated_headers
4799 `),
4800 expectedSystemIncludeDirs(`foo/system`),
4801 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4802 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4803 )
4804
4805 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4806 checkIncludeDirs(t, ctx, bar,
4807 expectedIncludeDirs(`
4808 bar/standard
4809 foo/standard
4810 .intermediates/genrule_foo/gen/generated_headers
4811 .intermediates/genrule_bar/gen/generated_headers
4812 `),
4813 expectedSystemIncludeDirs(`
4814 bar/system
4815 foo/system
4816 `),
4817 expectedGeneratedHeaders(`
4818 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4819 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4820 `),
4821 expectedOrderOnlyDeps(`
4822 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4823 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4824 `),
4825 )
4826 })
4827
Paul Duffin3cb603e2021-02-19 13:57:10 +00004828 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004829 ctx := android.GroupFixturePreparers(
4830 prepareForCcTest,
4831 aidl_library.PrepareForTestWithAidlLibrary,
4832 ).RunTestWithBp(t, `
4833 aidl_library {
4834 name: "libfoo_aidl",
4835 srcs: ["x/y/Bar.aidl"],
4836 strip_import_prefix: "x",
4837 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004838 cc_library_shared {
4839 name: "libfoo",
4840 srcs: [
4841 "foo.c",
4842 "b.aidl",
4843 "a.proto",
4844 ],
4845 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004846 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004847 export_aidl_headers: true,
4848 }
4849 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004850 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004851 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4852 checkIncludeDirs(t, ctx, foo,
4853 expectedIncludeDirs(`
4854 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004855 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004856 `),
4857 expectedSystemIncludeDirs(``),
4858 expectedGeneratedHeaders(`
4859 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4860 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4861 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004862 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4863 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4864 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004865 `),
4866 expectedOrderOnlyDeps(`
4867 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4868 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4869 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004870 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4871 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4872 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004873 `),
4874 )
4875 })
4876
Paul Duffin3cb603e2021-02-19 13:57:10 +00004877 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4878 ctx := testCc(t, genRuleModules+`
4879 cc_library_shared {
4880 name: "libfoo",
4881 srcs: [
4882 "foo.c",
4883 "b.aidl",
4884 "a.proto",
4885 ],
4886 proto: {
4887 export_proto_headers: true,
4888 }
4889 }
4890 `)
4891 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4892 checkIncludeDirs(t, ctx, foo,
4893 expectedIncludeDirs(`
4894 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4895 `),
4896 expectedSystemIncludeDirs(``),
4897 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004898 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4899 `),
4900 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004901 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4902 `),
4903 )
4904 })
4905
Paul Duffin33056e82021-02-19 13:49:08 +00004906 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004907 ctx := testCc(t, genRuleModules+`
4908 cc_library_shared {
4909 name: "libfoo",
4910 srcs: [
4911 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004912 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004913 "b.aidl",
4914 "a.proto",
4915 ],
4916 }
4917 `)
4918 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4919 checkIncludeDirs(t, ctx, foo,
4920 expectedIncludeDirs(`
4921 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4922 `),
4923 expectedSystemIncludeDirs(``),
4924 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004925 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004926 `),
4927 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004928 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4929 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004930 `),
4931 )
4932 })
4933}
Colin Crossae628182021-06-14 16:52:28 -07004934
4935func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004936 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004937 baseExpectedFlags := []string{
4938 "${config.ArmThumbCflags}",
4939 "${config.ArmCflags}",
4940 "${config.CommonGlobalCflags}",
4941 "${config.DeviceGlobalCflags}",
4942 "${config.ExternalCflags}",
4943 "${config.ArmToolchainCflags}",
4944 "${config.ArmArmv7ANeonCflags}",
4945 "${config.ArmGenericCflags}",
4946 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004947 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004948 }
4949
4950 expectedIncludes := []string{
4951 "external/foo/android_arm_export_include_dirs",
4952 "external/foo/lib32_export_include_dirs",
4953 "external/foo/arm_export_include_dirs",
4954 "external/foo/android_export_include_dirs",
4955 "external/foo/linux_export_include_dirs",
4956 "external/foo/export_include_dirs",
4957 "external/foo/android_arm_local_include_dirs",
4958 "external/foo/lib32_local_include_dirs",
4959 "external/foo/arm_local_include_dirs",
4960 "external/foo/android_local_include_dirs",
4961 "external/foo/linux_local_include_dirs",
4962 "external/foo/local_include_dirs",
4963 "external/foo",
4964 "external/foo/libheader1",
4965 "external/foo/libheader2",
4966 "external/foo/libwhole1",
4967 "external/foo/libwhole2",
4968 "external/foo/libstatic1",
4969 "external/foo/libstatic2",
4970 "external/foo/libshared1",
4971 "external/foo/libshared2",
4972 "external/foo/liblinux",
4973 "external/foo/libandroid",
4974 "external/foo/libarm",
4975 "external/foo/lib32",
4976 "external/foo/libandroid_arm",
4977 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004978 }
4979
4980 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4981 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4982
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004983 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004984 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004985 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004986
4987 lastIncludes := []string{
4988 "out/soong/ndk/sysroot/usr/include",
4989 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4990 }
4991
4992 combineSlices := func(slices ...[]string) []string {
4993 var ret []string
4994 for _, s := range slices {
4995 ret = append(ret, s...)
4996 }
4997 return ret
4998 }
4999
5000 testCases := []struct {
5001 name string
5002 src string
5003 expected []string
5004 }{
5005 {
5006 name: "c",
5007 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09005008 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005009 },
5010 {
5011 name: "cc",
5012 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09005013 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005014 },
5015 {
5016 name: "assemble",
5017 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09005018 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04005019 },
5020 }
5021
5022 for _, tc := range testCases {
5023 t.Run(tc.name, func(t *testing.T) {
5024 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005025 cc_library {
5026 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04005027 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05005028 cflags: ["-std=candcpp"],
5029 conlyflags: ["-std=conly"],
5030 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07005031 local_include_dirs: ["local_include_dirs"],
5032 export_include_dirs: ["export_include_dirs"],
5033 export_system_include_dirs: ["export_system_include_dirs"],
5034 static_libs: ["libstatic1", "libstatic2"],
5035 whole_static_libs: ["libwhole1", "libwhole2"],
5036 shared_libs: ["libshared1", "libshared2"],
5037 header_libs: ["libheader1", "libheader2"],
5038 target: {
5039 android: {
5040 shared_libs: ["libandroid"],
5041 local_include_dirs: ["android_local_include_dirs"],
5042 export_include_dirs: ["android_export_include_dirs"],
5043 },
5044 android_arm: {
5045 shared_libs: ["libandroid_arm"],
5046 local_include_dirs: ["android_arm_local_include_dirs"],
5047 export_include_dirs: ["android_arm_export_include_dirs"],
5048 },
5049 linux: {
5050 shared_libs: ["liblinux"],
5051 local_include_dirs: ["linux_local_include_dirs"],
5052 export_include_dirs: ["linux_export_include_dirs"],
5053 },
5054 },
5055 multilib: {
5056 lib32: {
5057 shared_libs: ["lib32"],
5058 local_include_dirs: ["lib32_local_include_dirs"],
5059 export_include_dirs: ["lib32_export_include_dirs"],
5060 },
5061 },
5062 arch: {
5063 arm: {
5064 shared_libs: ["libarm"],
5065 local_include_dirs: ["arm_local_include_dirs"],
5066 export_include_dirs: ["arm_export_include_dirs"],
5067 },
5068 },
5069 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005070 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005071 }
5072
5073 cc_library_headers {
5074 name: "libheader1",
5075 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005076 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005077 stl: "none",
5078 }
5079
5080 cc_library_headers {
5081 name: "libheader2",
5082 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005083 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005084 stl: "none",
5085 }
Liz Kammer08572c62021-09-30 10:11:04 -04005086 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07005087
Liz Kammer08572c62021-09-30 10:11:04 -04005088 libs := []string{
5089 "libstatic1",
5090 "libstatic2",
5091 "libwhole1",
5092 "libwhole2",
5093 "libshared1",
5094 "libshared2",
5095 "libandroid",
5096 "libandroid_arm",
5097 "liblinux",
5098 "lib32",
5099 "libarm",
5100 }
Colin Crossae628182021-06-14 16:52:28 -07005101
Liz Kammer08572c62021-09-30 10:11:04 -04005102 for _, lib := range libs {
5103 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005104 cc_library {
5105 name: "%s",
5106 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005107 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005108 stl: "none",
5109 }
5110 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04005111 }
5112
5113 ctx := android.GroupFixturePreparers(
5114 PrepareForIntegrationTestWithCc,
5115 android.FixtureAddTextFile("external/foo/Android.bp", bp),
5116 ).RunTest(t)
5117 // Use the arm variant instead of the arm64 variant so that it gets headers from
5118 // ndk_libandroid_support to test LateStaticLibs.
5119 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
5120
5121 var includes []string
5122 flags := strings.Split(cflags, " ")
5123 for _, flag := range flags {
5124 if strings.HasPrefix(flag, "-I") {
5125 includes = append(includes, strings.TrimPrefix(flag, "-I"))
5126 } else if flag == "-isystem" {
5127 // skip isystem, include next
5128 } else if len(flag) > 0 {
5129 includes = append(includes, flag)
5130 }
5131 }
5132
5133 android.AssertArrayString(t, "includes", tc.expected, includes)
5134 })
Colin Crossae628182021-06-14 16:52:28 -07005135 }
5136
Colin Crossae628182021-06-14 16:52:28 -07005137}
Alixb5f6d9e2022-04-20 23:00:58 +00005138
zijunzhao933e3802023-01-12 07:26:20 +00005139func TestAddnoOverride64GlobalCflags(t *testing.T) {
5140 t.Parallel()
5141 ctx := testCc(t, `
5142 cc_library_shared {
5143 name: "libclient",
5144 srcs: ["foo.c"],
5145 shared_libs: ["libfoo#1"],
5146 }
5147
5148 cc_library_shared {
5149 name: "libfoo",
5150 srcs: ["foo.c"],
5151 shared_libs: ["libbar"],
5152 export_shared_lib_headers: ["libbar"],
5153 stubs: {
5154 symbol_file: "foo.map.txt",
5155 versions: ["1", "2", "3"],
5156 },
5157 }
5158
5159 cc_library_shared {
5160 name: "libbar",
5161 export_include_dirs: ["include/libbar"],
5162 srcs: ["foo.c"],
5163 }`)
5164
5165 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5166
5167 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5168 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5169 }
5170}
5171
Alixb5f6d9e2022-04-20 23:00:58 +00005172func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005173 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005174 tests := []struct {
5175 name string
5176 clang bool
5177 BuildBrokenClangProperty bool
5178 err string
5179 }{
5180 {
5181 name: "error when clang is set to false",
5182 clang: false,
5183 err: "is no longer supported",
5184 },
5185 {
5186 name: "error when clang is set to true",
5187 clang: true,
5188 err: "property is deprecated, see Changes.md",
5189 },
5190 {
5191 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5192 clang: true,
5193 BuildBrokenClangProperty: true,
5194 },
5195 }
5196
5197 for _, test := range tests {
5198 t.Run(test.name, func(t *testing.T) {
5199 bp := fmt.Sprintf(`
5200 cc_library {
5201 name: "foo",
5202 clang: %t,
5203 }`, test.clang)
5204
5205 if test.err == "" {
5206 android.GroupFixturePreparers(
5207 prepareForCcTest,
5208 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5209 if test.BuildBrokenClangProperty {
5210 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5211 }
5212 }),
5213 ).RunTestWithBp(t, bp)
5214 } else {
5215 prepareForCcTest.
5216 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5217 RunTestWithBp(t, bp)
5218 }
5219 })
5220 }
5221}
Alix Espinoef47e542022-09-14 19:10:51 +00005222
5223func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005224 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005225 tests := []struct {
5226 name string
5227 clangAsFlags []string
5228 BuildBrokenClangAsFlags bool
5229 err string
5230 }{
5231 {
5232 name: "error when clang_asflags is set",
5233 clangAsFlags: []string{"-a", "-b"},
5234 err: "clang_asflags: property is deprecated",
5235 },
5236 {
5237 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5238 clangAsFlags: []string{"-a", "-b"},
5239 BuildBrokenClangAsFlags: true,
5240 },
5241 }
5242
5243 for _, test := range tests {
5244 t.Run(test.name, func(t *testing.T) {
5245 bp := fmt.Sprintf(`
5246 cc_library {
5247 name: "foo",
5248 clang_asflags: %s,
5249 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5250
5251 if test.err == "" {
5252 android.GroupFixturePreparers(
5253 prepareForCcTest,
5254 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5255 if test.BuildBrokenClangAsFlags {
5256 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5257 }
5258 }),
5259 ).RunTestWithBp(t, bp)
5260 } else {
5261 prepareForCcTest.
5262 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5263 RunTestWithBp(t, bp)
5264 }
5265 })
5266 }
5267}
5268
5269func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005270 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005271 tests := []struct {
5272 name string
5273 clangCFlags []string
5274 BuildBrokenClangCFlags bool
5275 err string
5276 }{
5277 {
5278 name: "error when clang_cflags is set",
5279 clangCFlags: []string{"-a", "-b"},
5280 err: "clang_cflags: property is deprecated",
5281 },
5282 {
5283 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5284 clangCFlags: []string{"-a", "-b"},
5285 BuildBrokenClangCFlags: true,
5286 },
5287 }
5288
5289 for _, test := range tests {
5290 t.Run(test.name, func(t *testing.T) {
5291 bp := fmt.Sprintf(`
5292 cc_library {
5293 name: "foo",
5294 clang_cflags: %s,
5295 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5296
5297 if test.err == "" {
5298 android.GroupFixturePreparers(
5299 prepareForCcTest,
5300 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5301 if test.BuildBrokenClangCFlags {
5302 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5303 }
5304 }),
5305 ).RunTestWithBp(t, bp)
5306 } else {
5307 prepareForCcTest.
5308 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5309 RunTestWithBp(t, bp)
5310 }
5311 })
5312 }
5313}
Yu Liue4312402023-01-18 09:15:31 -08005314
5315func TestDclaLibraryInApex(t *testing.T) {
5316 t.Parallel()
5317 bp := `
5318 cc_library_shared {
5319 name: "cc_lib_in_apex",
5320 srcs: ["foo.cc"],
5321 apex_available: ["myapex"],
5322 bazel_module: { label: "//foo/bar:bar" },
5323 }`
5324 label := "//foo/bar:bar"
5325 arch64 := "arm64_armv8-a"
5326 arch32 := "arm_armv7-a-neon"
5327 apexCfgKey := android.ApexConfigKey{
5328 WithinApex: true,
5329 ApexSdkVersion: "28",
5330 }
5331
5332 result := android.GroupFixturePreparers(
5333 prepareForCcTest,
5334 android.FixtureRegisterWithContext(registerTestMutators),
5335 android.FixtureModifyConfig(func(config android.Config) {
5336 config.BazelContext = android.MockBazelContext{
5337 OutputBaseDir: "outputbase",
5338 LabelToCcInfo: map[string]cquery.CcInfo{
5339 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5340 RootDynamicLibraries: []string{"foo.so"},
5341 },
5342 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5343 RootDynamicLibraries: []string{"foo.so"},
5344 },
5345 },
5346 BazelRequests: make(map[string]bool),
5347 }
5348 }),
5349 ).RunTestWithBp(t, bp)
5350 ctx := result.TestContext
5351
5352 // Test if the bazel request is queued correctly
5353 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5354 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5355 t.Errorf("Bazel request was not queued: %s", key)
5356 }
5357
5358 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5359 producer := sharedFoo.(android.OutputFileProducer)
5360 outputFiles, err := producer.OutputFiles("")
5361 if err != nil {
5362 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5363 }
5364 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5365 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5366}
Sam Delmericoef69d472023-04-18 17:32:43 -04005367
5368func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5369 t.Parallel()
5370 bp := `
5371 cc_library_static {
5372 name: "foo_ubsan_minimal",
5373 srcs: ["foo.cc"],
5374 bazel_module: { label: "//foo_ubsan_minimal" },
5375 sanitize: {
5376 all_undefined: true,
5377 integer_overflow: true,
5378 },
5379 }
5380 cc_library_static {
5381 name: "foo",
5382 srcs: ["foo.cc"],
5383 bazel_module: { label: "//foo" },
5384 sanitize: {
5385 address: true,
5386 hwaddress: true,
5387 fuzzer: true,
5388 integer_overflow: true,
5389 scs: true,
5390 },
5391 }
5392 cc_library_static {
5393 name: "foo_tsan",
5394 srcs: ["foo.cc"],
5395 bazel_module: { label: "//foo_tsan" },
5396 sanitize: {
5397 thread: true,
5398 },
5399 }
5400 cc_library_static {
5401 name: "foo_cfi",
5402 srcs: ["foo.cc"],
5403 bazel_module: { label: "//foo_cfi" },
5404 sanitize: {
5405 cfi: true,
5406 },
5407 }
5408 cc_library_static {
5409 name: "foo_memtag_stack",
5410 srcs: ["foo.cc"],
5411 bazel_module: { label: "//foo_memtag_stack" },
5412 sanitize: {
5413 memtag_stack: true,
5414 },
5415 }
5416 cc_library_static {
5417 name: "foo_memtag_heap",
5418 srcs: ["foo.cc"],
5419 bazel_module: { label: "//foo_memtag_heap" },
5420 sanitize: {
5421 memtag_heap: true,
5422 },
5423 }
5424 cc_library_static {
5425 name: "foo_safestack",
5426 srcs: ["foo.cc"],
5427 bazel_module: { label: "//foo_safestack" },
5428 sanitize: {
5429 safestack: true,
5430 },
5431 }
5432 cc_library_static {
5433 name: "foo_scudo",
5434 srcs: ["foo.cc"],
5435 bazel_module: { label: "//foo_scudo" },
5436 sanitize: {
5437 scudo: true,
5438 },
5439 }
5440 `
5441 testcases := []struct {
5442 name string
5443 variant string
5444 expectedOutputPaths []string
5445 }{
5446 {
5447 name: "foo_ubsan_minimal",
5448 variant: "android_arm64_armv8-a_static_apex28",
5449 expectedOutputPaths: []string{
5450 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5451 },
5452 },
5453 {
5454 name: "foo",
5455 variant: "android_arm64_armv8-a_static_apex28",
5456 expectedOutputPaths: []string{
5457 "outputbase/execroot/__main__/foo.a",
5458 },
5459 },
5460 {
5461 name: "foo",
5462 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5463 expectedOutputPaths: []string{
5464 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5465 },
5466 },
5467 {
5468 name: "foo",
5469 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5470 expectedOutputPaths: []string{
5471 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5472 },
5473 },
5474 {
5475 name: "foo",
5476 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5477 expectedOutputPaths: []string{
5478 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5479 },
5480 },
5481 {
5482 name: "foo",
5483 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5484 expectedOutputPaths: []string{
5485 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5486 },
5487 },
5488 {
5489 name: "foo",
5490 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5491 expectedOutputPaths: []string{
5492 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5493 },
5494 },
5495 {
5496 name: "foo",
5497 variant: "android_arm64_armv8-a_static_scs_apex28",
5498 expectedOutputPaths: []string{
5499 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5500 },
5501 },
5502 {
5503 name: "foo",
5504 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5505 expectedOutputPaths: []string{
5506 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5507 },
5508 },
5509 {
5510 name: "foo",
5511 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5512 expectedOutputPaths: []string{
5513 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5514 },
5515 },
5516 {
5517 name: "foo_tsan",
5518 variant: "android_arm64_armv8-a_static_apex28",
5519 expectedOutputPaths: []string{
5520 "outputbase/execroot/__main__/foo_tsan.a",
5521 },
5522 },
5523 {
5524 name: "foo_tsan",
5525 variant: "android_arm64_armv8-a_static_tsan_apex28",
5526 expectedOutputPaths: []string{
5527 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5528 },
5529 },
5530 {
5531 name: "foo_cfi",
5532 variant: "android_arm64_armv8-a_static_apex28",
5533 expectedOutputPaths: []string{
5534 "outputbase/execroot/__main__/foo_cfi.a",
5535 },
5536 },
5537 {
5538 name: "foo_cfi",
5539 variant: "android_arm64_armv8-a_static_cfi_apex28",
5540 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005541 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005542 },
5543 },
5544 {
5545 name: "foo_memtag_stack",
5546 variant: "android_arm64_armv8-a_static_apex28",
5547 expectedOutputPaths: []string{
5548 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5549 },
5550 },
5551 {
5552 name: "foo_memtag_heap",
5553 variant: "android_arm64_armv8-a_static_apex28",
5554 expectedOutputPaths: []string{
5555 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5556 },
5557 },
5558 {
5559 name: "foo_safestack",
5560 variant: "android_arm64_armv8-a_static_apex28",
5561 expectedOutputPaths: []string{
5562 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5563 },
5564 },
5565 {
5566 name: "foo_scudo",
5567 variant: "android_arm64_armv8-a_static_apex28",
5568 expectedOutputPaths: []string{
5569 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5570 },
5571 },
5572 }
5573
5574 ctx := android.GroupFixturePreparers(
5575 prepareForCcTest,
5576 prepareForAsanTest,
5577 android.FixtureRegisterWithContext(registerTestMutators),
5578 android.FixtureModifyConfig(func(config android.Config) {
5579 config.BazelContext = android.MockBazelContext{
5580 OutputBaseDir: "outputbase",
5581 LabelToCcInfo: map[string]cquery.CcInfo{
5582 "//foo_ubsan_minimal": {
5583 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5584 },
5585 "//foo": {
5586 RootStaticArchives: []string{"foo.a"},
5587 },
5588 "//foo_tsan": {
5589 RootStaticArchives: []string{"foo_tsan.a"},
5590 },
5591 "//foo_cfi": {
5592 RootStaticArchives: []string{"foo_cfi.a"},
5593 },
5594 "//foo_memtag_stack": {
5595 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5596 },
5597 "//foo_memtag_heap": {
5598 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5599 },
5600 "//foo_safestack": {
5601 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5602 },
5603 "//foo_scudo": {
5604 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5605 },
5606 },
5607 }
5608 }),
5609 ).RunTestWithBp(t, bp).TestContext
5610
5611 for _, tc := range testcases {
5612 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5613 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5614 if err != nil {
5615 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5616 }
5617 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5618 }
5619}