blob: cf618af58eec7625b5afa15adc7dbc814cc23cab [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
Vinh Tran367d89d2023-04-28 11:21:25 -040027 "android/soong/aidl_library"
Colin Crosse1bb5d02019-09-24 14:55:04 -070028 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050029 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070030)
31
Yu Liue4312402023-01-18 09:15:31 -080032func init() {
33 registerTestMutators(android.InitRegistrationContext)
34}
35
Jiyong Park6a43f042017-10-12 23:05:00 +090036func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000037 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090038}
39
Paul Duffin2e6f90e2021-03-22 23:20:25 +000040var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000041 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
43 variables.DeviceVndkVersion = StringPtr("current")
44 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090045 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000046 }),
47)
48
Yu Liue4312402023-01-18 09:15:31 -080049var ccLibInApex = "cc_lib_in_apex"
50var apexVariationName = "apex28"
51var apexVersion = "28"
52
53func registerTestMutators(ctx android.RegistrationContext) {
54 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
55 ctx.BottomUp("apex", testApexMutator).Parallel()
56 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
57 })
58}
59
60func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
61 if m := ctx.Module(); m.Enabled() {
62 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000063 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080064 mixedBuildMod.QueueBazelCall(ctx)
65 }
66 }
67 }
68}
69
70func testApexMutator(mctx android.BottomUpMutatorContext) {
71 modules := mctx.CreateVariations(apexVariationName)
72 apexInfo := android.ApexInfo{
73 ApexVariationName: apexVariationName,
74 MinSdkVersion: android.ApiLevelForTest(apexVersion),
75 }
76 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
77}
78
Paul Duffin8567f222021-03-23 00:02:06 +000079// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000080//
81// See testCc for an explanation as to how to stop using this deprecated method.
82//
83// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080084func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070085 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000086 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000087 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090088}
89
Paul Duffin8567f222021-03-23 00:02:06 +000090// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000091//
Paul Duffin8567f222021-03-23 00:02:06 +000092// Do not add any new usages of this, instead use the prepareForCcTest directly as it makes it much
Paul Duffin02a3d652021-02-24 18:51:54 +000093// easier to customize the test behavior.
94//
95// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin8567f222021-03-23 00:02:06 +000096// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000097// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
98// that it did not change the test behavior unexpectedly.
99//
100// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800101func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800102 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000103 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000104 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800105}
106
Paul Duffin8567f222021-03-23 00:02:06 +0000107// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000108//
109// See testCc for an explanation as to how to stop using this deprecated method.
110//
111// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800112func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800113 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000114 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900115 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +0800116
Colin Cross98be1bb2019-12-13 20:41:13 -0800117 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800118}
119
Paul Duffin8567f222021-03-23 00:02:06 +0000120// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900125func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
126 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000127 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +0900128 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900129 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +0900130
131 return testCcWithConfig(t, config)
132}
133
Paul Duffin8567f222021-03-23 00:02:06 +0000134// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000135//
136// See testCc for an explanation as to how to stop using this deprecated method.
137//
138// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900139func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800140 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800141
Paul Duffin8567f222021-03-23 00:02:06 +0000142 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000143 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
144 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800145}
146
Paul Duffin8567f222021-03-23 00:02:06 +0000147// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000148//
149// See testCc for an explanation as to how to stop using this deprecated method.
150//
151// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900152func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900153 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000154 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900156 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900157 testCcErrorWithConfig(t, pattern, config)
158 return
159}
160
Paul Duffin8567f222021-03-23 00:02:06 +0000161// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000162//
163// See testCc for an explanation as to how to stop using this deprecated method.
164//
165// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900166func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900167 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000168 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900169 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
170 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900171 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900172 testCcErrorWithConfig(t, pattern, config)
173 return
174}
175
Logan Chienf3511742017-10-31 18:04:35 +0800176const (
Colin Cross7113d202019-11-20 16:39:12 -0800177 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900178 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
179 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800180 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800181)
182
Paul Duffindb462dd2021-03-21 22:01:55 +0000183// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
184// running it in a fixture that requires all source files to exist.
185func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
186 android.GroupFixturePreparers(
187 PrepareForTestWithCcDefaultModules,
188 android.PrepareForTestDisallowNonExistentPaths,
189 ).RunTest(t)
190}
191
Jiyong Park6a43f042017-10-12 23:05:00 +0900192func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400193 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900194 ctx := testCc(t, `
195 cc_library {
196 name: "libTest",
197 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700198 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800199 nocrt: true,
200 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900201 vendor_available: true,
202 target: {
203 vendor: {
204 srcs: ["bar.c"],
205 },
206 },
207 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900208 `)
209
Logan Chienf3511742017-10-31 18:04:35 +0800210 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900211 var objs []string
212 for _, o := range ld.Inputs {
213 objs = append(objs, o.Base())
214 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800215 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900216 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
217 }
218}
219
Justin Yun7f99ec72021-04-12 13:19:28 +0900220func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
221 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
222 partitionDefined := false
223 checkPartition := func(specific bool, partition string) {
224 if specific {
225 if expected != partition && !partitionDefined {
226 // The variant is installed to the 'partition'
227 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
228 }
229 partitionDefined = true
230 } else {
231 // The variant is not installed to the 'partition'
232 if expected == partition {
233 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
234 }
235 }
236 }
237 socSpecific := func(m *Module) bool {
238 return m.SocSpecific() || m.socSpecificModuleContext()
239 }
240 deviceSpecific := func(m *Module) bool {
241 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
242 }
243 productSpecific := func(m *Module) bool {
244 return m.ProductSpecific() || m.productSpecificModuleContext()
245 }
246 systemExtSpecific := func(m *Module) bool {
247 return m.SystemExtSpecific()
248 }
249 checkPartition(socSpecific(mod), "vendor")
250 checkPartition(deviceSpecific(mod), "odm")
251 checkPartition(productSpecific(mod), "product")
252 checkPartition(systemExtSpecific(mod), "system_ext")
253 if !partitionDefined && expected != "system" {
254 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
255 " but installed to system partition", variant, name, expected)
256 }
257}
258
259func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400260 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900261 t.Helper()
262 ctx := prepareForCcTest.RunTestWithBp(t, `
263 cc_library {
264 name: "libsystem",
265 }
266 cc_library {
267 name: "libsystem_ext",
268 system_ext_specific: true,
269 }
270 cc_library {
271 name: "libproduct",
272 product_specific: true,
273 }
274 cc_library {
275 name: "libvendor",
276 vendor: true,
277 }
278 cc_library {
279 name: "libodm",
280 device_specific: true,
281 }
282 cc_library {
283 name: "liball_available",
284 vendor_available: true,
285 product_available: true,
286 }
287 cc_library {
288 name: "libsystem_ext_all_available",
289 system_ext_specific: true,
290 vendor_available: true,
291 product_available: true,
292 }
293 cc_library {
294 name: "liball_available_odm",
295 odm_available: true,
296 product_available: true,
297 }
298 cc_library {
299 name: "libproduct_vendoravailable",
300 product_specific: true,
301 vendor_available: true,
302 }
303 cc_library {
304 name: "libproduct_odmavailable",
305 product_specific: true,
306 odm_available: true,
307 }
308 `).TestContext
309
310 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
311 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
312 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
313 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
314 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
315
316 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
317 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
318 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
319
320 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
321 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
322 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
323
324 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
325 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
326 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
327
328 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
329 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
330
331 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
332 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
333}
334
Logan Chienf3511742017-10-31 18:04:35 +0800335func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900336 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800337
Logan Chiend3c59a22018-03-29 14:08:15 +0800338 t.Helper()
339
Justin Yun0ecf0b22020-02-28 15:07:59 +0900340 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800341
342 // Check library properties.
343 lib, ok := mod.compiler.(*libraryDecorator)
344 if !ok {
345 t.Errorf("%q must have libraryDecorator", name)
346 } else if lib.baseInstaller.subDir != subDir {
347 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
348 lib.baseInstaller.subDir)
349 }
350
351 // Check VNDK properties.
352 if mod.vndkdep == nil {
353 t.Fatalf("%q must have `vndkdep`", name)
354 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700355 if !mod.IsVndk() {
356 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800357 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400358 if mod.IsVndkSp() != isVndkSp {
359 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800360 }
361
362 // Check VNDK extension properties.
363 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500364 if mod.IsVndkExt() != isVndkExt {
365 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800366 }
367
368 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
369 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
370 }
371}
372
Jooyung Han2216fb12019-11-06 16:46:15 +0900373func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
374 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800375 content := android.ContentFromFileRuleForTests(t, params)
376 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900377 assertArrayString(t, actual, expected)
378}
379
Jooyung Han097087b2019-10-22 19:32:18 +0900380func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
381 t.Helper()
382 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900383 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
384}
385
386func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
387 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800388 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800389 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900390}
391
Logan Chienf3511742017-10-31 18:04:35 +0800392func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400393 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800394 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800395 cc_library {
396 name: "libvndk",
397 vendor_available: true,
398 vndk: {
399 enabled: true,
400 },
401 nocrt: true,
402 }
403
404 cc_library {
405 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900406 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800407 vndk: {
408 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900409 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 },
411 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900412 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800413 }
414
415 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900416 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800417 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900418 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800419 vndk: {
420 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900421 },
422 nocrt: true,
423 target: {
424 vendor: {
425 cflags: ["-DTEST"],
426 },
427 product: {
428 cflags: ["-DTEST"],
429 },
430 },
431 }
432
433 cc_library {
434 name: "libvndk_sp",
435 vendor_available: true,
436 vndk: {
437 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800438 support_system_process: true,
439 },
440 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900441 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800442 }
443
444 cc_library {
445 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900446 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800447 vndk: {
448 enabled: true,
449 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900450 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800451 },
452 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900453 target: {
454 vendor: {
455 suffix: "-x",
456 },
457 },
Logan Chienf3511742017-10-31 18:04:35 +0800458 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900459
460 cc_library {
461 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900462 vendor_available: true,
463 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900464 vndk: {
465 enabled: true,
466 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900467 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900468 },
469 nocrt: true,
470 target: {
471 vendor: {
472 suffix: "-x",
473 },
474 product: {
475 suffix: "-x",
476 },
477 },
478 }
479
Justin Yun450ae722021-04-16 19:58:18 +0900480 cc_library {
481 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700482 llndk: {
483 symbol_file: "libllndk.map.txt",
484 export_llndk_headers: ["libllndk_headers"],
485 }
Justin Yun450ae722021-04-16 19:58:18 +0900486 }
487
Justin Yun611e8862021-05-24 18:17:33 +0900488 cc_library {
489 name: "libclang_rt.hwasan-llndk",
490 llndk: {
491 symbol_file: "libclang_rt.hwasan.map.txt",
492 }
493 }
494
Colin Cross627280f2021-04-26 16:53:58 -0700495 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900496 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700497 llndk: {
498 llndk_headers: true,
499 },
Justin Yun450ae722021-04-16 19:58:18 +0900500 export_include_dirs: ["include"],
501 }
502
Colin Crosse4e44bc2020-12-28 13:50:21 -0800503 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900504 name: "llndk.libraries.txt",
505 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800506 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900507 name: "vndkcore.libraries.txt",
508 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800509 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900510 name: "vndksp.libraries.txt",
511 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800512 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900513 name: "vndkprivate.libraries.txt",
514 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800515 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900516 name: "vndkproduct.libraries.txt",
517 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800518 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900519 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800520 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900521 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800522 `
523
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000524 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800525 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900526 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900527 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800528
529 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800530
Jooyung Han261e1582020-10-20 18:54:21 +0900531 // subdir == "" because VNDK libs are not supposed to be installed separately.
532 // They are installed as part of VNDK APEX instead.
533 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
534 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900535 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900536 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
537 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900538 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900539
Justin Yun6977e8a2020-10-29 18:24:11 +0900540 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
541 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900542
Inseob Kim1f086e22019-05-09 13:29:15 +0900543 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900544 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000545 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900546
547 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
548 "arm64", "armv8-a"))
549 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
550 "arm", "armv7-a-neon"))
551
552 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
553 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900554 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
555
Inseob Kim1f086e22019-05-09 13:29:15 +0900556 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
557 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900558 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900559
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900560 variant := "android_vendor.29_arm64_armv8-a_shared"
561 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900562
Inseob Kim7f283f42020-06-01 21:53:49 +0900563 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
564
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400565 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
566 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
567 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
568 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
569 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
570 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
571 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
572 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900573
Jooyung Han39edb6c2019-11-06 16:53:07 +0900574 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800575 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
576 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
577 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
578 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
579 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900580
Jooyung Han097087b2019-10-22 19:32:18 +0900581 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
582 "LLNDK: libc.so",
583 "LLNDK: libdl.so",
584 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900585 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900586 "LLNDK: libm.so",
587 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900588 "VNDK-SP: libvndk_sp-x.so",
589 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900590 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900591 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900592 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900593 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900594 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900595 "VNDK-private: libvndk-private.so",
596 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900597 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900598 "VNDK-product: libc++.so",
599 "VNDK-product: libvndk_product.so",
600 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900601 })
Justin Yun611e8862021-05-24 18:17:33 +0900602 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libclang_rt.hwasan-llndk.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900603 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
604 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
605 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt", []string{"libft2.so", "libvndk-private.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
Justin Yun8a2600c2020-12-07 12:44:03 +0900606 checkVndkLibrariesOutput(t, ctx, "vndkproduct.libraries.txt", []string{"libc++.so", "libvndk_product.so", "libvndk_sp_product_private-x.so"})
Jooyung Han2216fb12019-11-06 16:46:15 +0900607 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
608}
609
Yo Chiangbba545e2020-06-09 16:15:37 +0800610func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400611 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800612 ctx := testCc(t, `
613 cc_library {
614 name: "libvndk_host_supported",
615 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900616 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800617 vndk: {
618 enabled: true,
619 },
620 host_supported: true,
621 }
622
623 cc_library {
624 name: "libvndk_host_supported_but_disabled_on_device",
625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900626 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800627 vndk: {
628 enabled: true,
629 },
630 host_supported: true,
631 enabled: false,
632 target: {
633 host: {
634 enabled: true,
635 }
636 }
637 }
638
Colin Crosse4e44bc2020-12-28 13:50:21 -0800639 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800640 name: "vndkcore.libraries.txt",
641 }
642 `)
643
644 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
645}
646
Jooyung Han2216fb12019-11-06 16:46:15 +0900647func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400648 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800649 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800650 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900651 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800652 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800653 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000654 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800655 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900656 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900657 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800658 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900659
Colin Cross45bce852021-11-11 22:47:54 -0800660 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700661 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900662 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900663}
664
665func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400666 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800667 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900668 cc_library {
669 name: "libvndk",
670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900671 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900672 vndk: {
673 enabled: true,
674 },
675 nocrt: true,
676 }
677
678 cc_library {
679 name: "libvndk_sp",
680 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900681 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900682 vndk: {
683 enabled: true,
684 support_system_process: true,
685 },
686 nocrt: true,
687 }
688
689 cc_library {
690 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900691 vendor_available: true,
692 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900693 vndk: {
694 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900695 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900696 },
697 nocrt: true,
698 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900699
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900701 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800702 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900703 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800704 `
705
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000706 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800707 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900708 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800709 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900710 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800711
712 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
713
714 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900715
Jooyung Han2216fb12019-11-06 16:46:15 +0900716 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900717}
718
Chris Parsons79d66a52020-06-05 17:26:16 -0400719func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400720 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400721 bp := `
722 cc_test_library {
723 name: "test_lib",
724 srcs: ["test_lib.cpp"],
725 gtest: false,
726 }
727
728 cc_test {
729 name: "main_test",
730 data_libs: ["test_lib"],
731 gtest: false,
732 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400733 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400734
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000735 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400736 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900737 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400738 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
739
740 ctx := testCcWithConfig(t, config)
741 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
742 testBinary := module.(*Module).linker.(*testBinary)
743 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
744 if err != nil {
745 t.Errorf("Expected cc_test to produce output files, error: %s", err)
746 return
747 }
748 if len(outputFiles) != 1 {
749 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
750 return
751 }
752 if len(testBinary.dataPaths()) != 1 {
753 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
754 return
755 }
756
757 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400758 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400759
760 if !strings.HasSuffix(outputPath, "/main_test") {
761 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
762 return
763 }
764 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
765 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
766 return
767 }
768}
769
Chris Parsons216e10a2020-07-09 17:12:52 -0400770func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400771 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400772 bp := `
773 cc_test_library {
774 name: "test_lib",
775 srcs: ["test_lib.cpp"],
776 relative_install_path: "foo/bar/baz",
777 gtest: false,
778 }
779
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400780 cc_binary {
781 name: "test_bin",
782 relative_install_path: "foo/bar/baz",
783 compile_multilib: "both",
784 }
785
Chris Parsons216e10a2020-07-09 17:12:52 -0400786 cc_test {
787 name: "main_test",
788 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400789 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400790 gtest: false,
791 }
792 `
793
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000794 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400795 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900796 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400797 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
798
799 ctx := testCcWithConfig(t, config)
800 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
801 testBinary := module.(*Module).linker.(*testBinary)
802 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
803 if err != nil {
804 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
805 }
806 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400807 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400808 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400809 if len(testBinary.dataPaths()) != 2 {
810 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400811 }
812
813 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400814
815 if !strings.HasSuffix(outputPath, "/main_test") {
816 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
817 }
Colin Crossaa255532020-07-03 13:18:24 -0700818 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400819 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
820 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400821 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400822 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400823 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
824 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
825 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
826 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400827}
828
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000829func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400830 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000831 bp := `
832 cc_test {
833 name: "main_test",
834 srcs: ["main_test.cpp"],
835 test_suites: [
836 "suite_1",
837 "suite_2",
838 ],
839 gtest: false,
840 }
841 `
842
843 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
844 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
845
846 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
847 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
848 if len(compatEntries) != 2 {
849 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
850 }
851 if compatEntries[0] != "suite_1" {
852 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
853 " but was '%s'", compatEntries[0])
854 }
855 if compatEntries[1] != "suite_2" {
856 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
857 " but was '%s'", compatEntries[1])
858 }
859}
860
861func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400862 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000863 bp := `
864 cc_test_library {
865 name: "main_test_lib",
866 srcs: ["main_test_lib.cpp"],
867 test_suites: [
868 "suite_1",
869 "suite_2",
870 ],
871 gtest: false,
872 }
873 `
874
875 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
876 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
877
878 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
879 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
880 if len(compatEntries) != 2 {
881 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
882 }
883 if compatEntries[0] != "suite_1" {
884 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
885 " but was '%s'", compatEntries[0])
886 }
887 if compatEntries[1] != "suite_2" {
888 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
889 " but was '%s'", compatEntries[1])
890 }
891}
892
Jooyung Han0302a842019-10-30 18:43:49 +0900893func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400894 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900895 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900896 cc_library {
897 name: "libvndk",
898 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900899 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900900 vndk: {
901 enabled: true,
902 },
903 nocrt: true,
904 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900905 cc_library {
906 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900907 vendor_available: true,
908 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900909 vndk: {
910 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900911 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900912 },
913 nocrt: true,
914 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800915
916 cc_library {
917 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700918 llndk: {
919 symbol_file: "libllndk.map.txt",
920 export_llndk_headers: ["libllndk_headers"],
921 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800922 }
923
Colin Cross627280f2021-04-26 16:53:58 -0700924 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800925 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700926 llndk: {
927 symbol_file: "libllndk.map.txt",
928 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800929 export_include_dirs: ["include"],
930 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900931 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900932
933 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
934 "LLNDK: libc.so",
935 "LLNDK: libdl.so",
936 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800937 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900938 "LLNDK: libm.so",
939 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900940 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900941 "VNDK-core: libvndk.so",
942 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900943 "VNDK-private: libvndk-private.so",
944 "VNDK-product: libc++.so",
945 "VNDK-product: libvndk-private.so",
946 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900947 })
Logan Chienf3511742017-10-31 18:04:35 +0800948}
949
Justin Yun63e9ec72020-10-29 16:49:43 +0900950func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400951 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900952 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900953 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900954 cc_library {
955 name: "libvndk",
956 vndk: {
957 enabled: true,
958 },
959 nocrt: true,
960 }
961 `)
962
Justin Yunc0d8c492021-01-07 17:45:31 +0900963 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900964 cc_library {
965 name: "libvndk",
966 product_available: true,
967 vndk: {
968 enabled: true,
969 },
970 nocrt: true,
971 }
972 `)
973
Justin Yun6977e8a2020-10-29 18:24:11 +0900974 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
975 cc_library {
976 name: "libvndkprop",
977 vendor_available: true,
978 product_available: true,
979 vndk: {
980 enabled: true,
981 },
982 nocrt: true,
983 target: {
984 vendor: {
985 cflags: ["-DTEST",],
986 },
987 },
988 }
989 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900990}
991
Logan Chiend3c59a22018-03-29 14:08:15 +0800992func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400993 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800994 // Check whether an error is emitted when a VNDK lib depends on a system lib.
995 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
996 cc_library {
997 name: "libvndk",
998 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900999 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001000 vndk: {
1001 enabled: true,
1002 },
1003 shared_libs: ["libfwk"], // Cause error
1004 nocrt: true,
1005 }
1006
1007 cc_library {
1008 name: "libfwk",
1009 nocrt: true,
1010 }
1011 `)
1012
1013 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
1014 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1015 cc_library {
1016 name: "libvndk",
1017 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001018 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001019 vndk: {
1020 enabled: true,
1021 },
1022 shared_libs: ["libvendor"], // Cause error
1023 nocrt: true,
1024 }
1025
1026 cc_library {
1027 name: "libvendor",
1028 vendor: true,
1029 nocrt: true,
1030 }
1031 `)
1032
1033 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1034 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1035 cc_library {
1036 name: "libvndk_sp",
1037 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001038 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001039 vndk: {
1040 enabled: true,
1041 support_system_process: true,
1042 },
1043 shared_libs: ["libfwk"], // Cause error
1044 nocrt: true,
1045 }
1046
1047 cc_library {
1048 name: "libfwk",
1049 nocrt: true,
1050 }
1051 `)
1052
1053 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1054 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1055 cc_library {
1056 name: "libvndk_sp",
1057 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001058 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001059 vndk: {
1060 enabled: true,
1061 support_system_process: true,
1062 },
1063 shared_libs: ["libvendor"], // Cause error
1064 nocrt: true,
1065 }
1066
1067 cc_library {
1068 name: "libvendor",
1069 vendor: true,
1070 nocrt: true,
1071 }
1072 `)
1073
1074 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1075 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1076 cc_library {
1077 name: "libvndk_sp",
1078 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001079 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001080 vndk: {
1081 enabled: true,
1082 support_system_process: true,
1083 },
1084 shared_libs: ["libvndk"], // Cause error
1085 nocrt: true,
1086 }
1087
1088 cc_library {
1089 name: "libvndk",
1090 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001091 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001092 vndk: {
1093 enabled: true,
1094 },
1095 nocrt: true,
1096 }
1097 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001098
1099 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1100 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1101 cc_library {
1102 name: "libvndk",
1103 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001104 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001105 vndk: {
1106 enabled: true,
1107 },
1108 shared_libs: ["libnonvndk"],
1109 nocrt: true,
1110 }
1111
1112 cc_library {
1113 name: "libnonvndk",
1114 vendor_available: true,
1115 nocrt: true,
1116 }
1117 `)
1118
1119 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1120 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1121 cc_library {
1122 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001123 vendor_available: true,
1124 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001125 vndk: {
1126 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001127 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001128 },
1129 shared_libs: ["libnonvndk"],
1130 nocrt: true,
1131 }
1132
1133 cc_library {
1134 name: "libnonvndk",
1135 vendor_available: true,
1136 nocrt: true,
1137 }
1138 `)
1139
1140 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1141 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1142 cc_library {
1143 name: "libvndksp",
1144 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001145 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001146 vndk: {
1147 enabled: true,
1148 support_system_process: true,
1149 },
1150 shared_libs: ["libnonvndk"],
1151 nocrt: true,
1152 }
1153
1154 cc_library {
1155 name: "libnonvndk",
1156 vendor_available: true,
1157 nocrt: true,
1158 }
1159 `)
1160
1161 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1162 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1163 cc_library {
1164 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001165 vendor_available: true,
1166 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001167 vndk: {
1168 enabled: true,
1169 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001170 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001171 },
1172 shared_libs: ["libnonvndk"],
1173 nocrt: true,
1174 }
1175
1176 cc_library {
1177 name: "libnonvndk",
1178 vendor_available: true,
1179 nocrt: true,
1180 }
1181 `)
1182}
1183
1184func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001185 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001186 // okay to link : LLNDK -> double_loadable VNDK
1187 testCc(t, `
1188 cc_library {
1189 name: "libllndk",
1190 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001191 llndk: {
1192 symbol_file: "libllndk.map.txt",
1193 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001194 }
1195
1196 cc_library {
1197 name: "libdoubleloadable",
1198 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001199 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001200 vndk: {
1201 enabled: true,
1202 },
1203 double_loadable: true,
1204 }
1205 `)
1206 // okay to link : LLNDK -> VNDK-SP
1207 testCc(t, `
1208 cc_library {
1209 name: "libllndk",
1210 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001211 llndk: {
1212 symbol_file: "libllndk.map.txt",
1213 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001214 }
1215
1216 cc_library {
1217 name: "libvndksp",
1218 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001219 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001220 vndk: {
1221 enabled: true,
1222 support_system_process: true,
1223 },
1224 }
1225 `)
1226 // okay to link : double_loadable -> double_loadable
1227 testCc(t, `
1228 cc_library {
1229 name: "libdoubleloadable1",
1230 shared_libs: ["libdoubleloadable2"],
1231 vendor_available: true,
1232 double_loadable: true,
1233 }
1234
1235 cc_library {
1236 name: "libdoubleloadable2",
1237 vendor_available: true,
1238 double_loadable: true,
1239 }
1240 `)
1241 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1242 testCc(t, `
1243 cc_library {
1244 name: "libdoubleloadable",
1245 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001246 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001247 vndk: {
1248 enabled: true,
1249 },
1250 double_loadable: true,
1251 shared_libs: ["libnondoubleloadable"],
1252 }
1253
1254 cc_library {
1255 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001256 vendor_available: true,
1257 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001258 vndk: {
1259 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001260 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001261 },
1262 double_loadable: true,
1263 }
1264 `)
1265 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1266 testCc(t, `
1267 cc_library {
1268 name: "libllndk",
1269 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001270 llndk: {
1271 symbol_file: "libllndk.map.txt",
1272 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001273 }
1274
1275 cc_library {
1276 name: "libcoreonly",
1277 shared_libs: ["libvendoravailable"],
1278 }
1279
1280 // indirect dependency of LLNDK
1281 cc_library {
1282 name: "libvendoravailable",
1283 vendor_available: true,
1284 double_loadable: true,
1285 }
1286 `)
1287}
1288
1289func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001290 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001291 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1292 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1293 cc_library {
1294 name: "libllndk",
1295 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001296 llndk: {
1297 symbol_file: "libllndk.map.txt",
1298 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001299 }
1300
1301 cc_library {
1302 name: "libnondoubleloadable",
1303 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001304 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001305 vndk: {
1306 enabled: true,
1307 },
1308 }
1309 `)
1310
1311 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1312 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1313 cc_library {
1314 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001315 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001316 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001317 llndk: {
1318 symbol_file: "libllndk.map.txt",
1319 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001320 }
1321
1322 cc_library {
1323 name: "libnondoubleloadable",
1324 vendor_available: true,
1325 }
1326 `)
1327
Jooyung Hana70f0672019-01-18 15:20:43 +09001328 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1329 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1330 cc_library {
1331 name: "libllndk",
1332 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001333 llndk: {
1334 symbol_file: "libllndk.map.txt",
1335 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001336 }
1337
1338 cc_library {
1339 name: "libcoreonly",
1340 shared_libs: ["libvendoravailable"],
1341 }
1342
1343 // indirect dependency of LLNDK
1344 cc_library {
1345 name: "libvendoravailable",
1346 vendor_available: true,
1347 }
1348 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001349
1350 // The error is not from 'client' but from 'libllndk'
1351 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1352 cc_library {
1353 name: "client",
1354 vendor_available: true,
1355 double_loadable: true,
1356 shared_libs: ["libllndk"],
1357 }
1358 cc_library {
1359 name: "libllndk",
1360 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001361 llndk: {
1362 symbol_file: "libllndk.map.txt",
1363 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001364 }
1365 cc_library {
1366 name: "libnondoubleloadable",
1367 vendor_available: true,
1368 }
1369 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001370}
1371
Jooyung Han479ca172020-10-19 18:51:07 +09001372func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001373 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001374 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1375 cc_library {
1376 name: "libvndksp",
1377 shared_libs: ["libanothervndksp"],
1378 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001379 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001380 vndk: {
1381 enabled: true,
1382 support_system_process: true,
1383 }
1384 }
1385
1386 cc_library {
1387 name: "libllndk",
1388 shared_libs: ["libanothervndksp"],
1389 }
1390
Jooyung Han479ca172020-10-19 18:51:07 +09001391 cc_library {
1392 name: "libanothervndksp",
1393 vendor_available: true,
1394 }
1395 `)
1396}
1397
Logan Chienf3511742017-10-31 18:04:35 +08001398func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001399 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001400 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001401 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001402 cc_library {
1403 name: "libvndk",
1404 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001405 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001406 vndk: {
1407 enabled: true,
1408 },
1409 nocrt: true,
1410 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001411 cc_library {
1412 name: "libvndk2",
1413 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001414 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001415 vndk: {
1416 enabled: true,
1417 },
1418 target: {
1419 vendor: {
1420 suffix: "-suffix",
1421 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001422 product: {
1423 suffix: "-suffix",
1424 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001425 },
1426 nocrt: true,
1427 }
Logan Chienf3511742017-10-31 18:04:35 +08001428
1429 cc_library {
1430 name: "libvndk_ext",
1431 vendor: true,
1432 vndk: {
1433 enabled: true,
1434 extends: "libvndk",
1435 },
1436 nocrt: true,
1437 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001438
1439 cc_library {
1440 name: "libvndk2_ext",
1441 vendor: true,
1442 vndk: {
1443 enabled: true,
1444 extends: "libvndk2",
1445 },
1446 nocrt: true,
1447 }
Logan Chienf3511742017-10-31 18:04:35 +08001448
Justin Yun0ecf0b22020-02-28 15:07:59 +09001449 cc_library {
1450 name: "libvndk_ext_product",
1451 product_specific: true,
1452 vndk: {
1453 enabled: true,
1454 extends: "libvndk",
1455 },
1456 nocrt: true,
1457 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001458
Justin Yun0ecf0b22020-02-28 15:07:59 +09001459 cc_library {
1460 name: "libvndk2_ext_product",
1461 product_specific: true,
1462 vndk: {
1463 enabled: true,
1464 extends: "libvndk2",
1465 },
1466 nocrt: true,
1467 }
1468 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001469 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001470 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1471 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001472 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001473
1474 ctx := testCcWithConfig(t, config)
1475
1476 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1477 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1478
1479 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1480 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1481
1482 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1483 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001484}
1485
Logan Chiend3c59a22018-03-29 14:08:15 +08001486func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001487 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001488 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1489 ctx := testCcNoVndk(t, `
1490 cc_library {
1491 name: "libvndk",
1492 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001493 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001494 vndk: {
1495 enabled: true,
1496 },
1497 nocrt: true,
1498 }
1499
1500 cc_library {
1501 name: "libvndk_ext",
1502 vendor: true,
1503 vndk: {
1504 enabled: true,
1505 extends: "libvndk",
1506 },
1507 nocrt: true,
1508 }
1509 `)
1510
1511 // Ensures that the core variant of "libvndk_ext" can be found.
1512 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1513 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1514 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1515 }
1516}
1517
Justin Yun0ecf0b22020-02-28 15:07:59 +09001518func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001519 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001520 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001521 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001522 cc_library {
1523 name: "libvndk",
1524 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001525 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001526 vndk: {
1527 enabled: true,
1528 },
1529 nocrt: true,
1530 }
1531
1532 cc_library {
1533 name: "libvndk_ext_product",
1534 product_specific: true,
1535 vndk: {
1536 enabled: true,
1537 extends: "libvndk",
1538 },
1539 nocrt: true,
1540 }
1541 `)
1542
1543 // Ensures that the core variant of "libvndk_ext_product" can be found.
1544 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1545 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1546 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1547 }
1548}
1549
Logan Chienf3511742017-10-31 18:04:35 +08001550func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001551 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001552 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001553 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001554 cc_library {
1555 name: "libvndk",
1556 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001557 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001558 vndk: {
1559 enabled: true,
1560 },
1561 nocrt: true,
1562 }
1563
1564 cc_library {
1565 name: "libvndk_ext",
1566 vndk: {
1567 enabled: true,
1568 extends: "libvndk",
1569 },
1570 nocrt: true,
1571 }
1572 `)
1573
1574 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1575 cc_library {
1576 name: "libvndk",
1577 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001578 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001579 vndk: {
1580 enabled: true,
1581 },
1582 nocrt: true,
1583 }
1584
1585 cc_library {
1586 name: "libvndk_ext",
1587 vendor: true,
1588 vndk: {
1589 enabled: true,
1590 },
1591 nocrt: true,
1592 }
1593 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001594
1595 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1596 cc_library {
1597 name: "libvndk",
1598 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001599 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001600 vndk: {
1601 enabled: true,
1602 },
1603 nocrt: true,
1604 }
1605
1606 cc_library {
1607 name: "libvndk_ext_product",
1608 product_specific: true,
1609 vndk: {
1610 enabled: true,
1611 },
1612 nocrt: true,
1613 }
1614 `)
1615
1616 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1617 cc_library {
1618 name: "libvndk",
1619 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001620 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001621 vndk: {
1622 enabled: true,
1623 },
1624 nocrt: true,
1625 }
1626
1627 cc_library {
1628 name: "libvndk_ext_product",
1629 product_specific: true,
1630 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001631 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001632 vndk: {
1633 enabled: true,
1634 extends: "libvndk",
1635 },
1636 nocrt: true,
1637 }
1638 `)
Logan Chienf3511742017-10-31 18:04:35 +08001639}
1640
1641func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001642 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001643 // This test ensures an error is emitted for inconsistent support_system_process.
1644 testCcError(t, "module \".*\" with mismatched support_system_process", `
1645 cc_library {
1646 name: "libvndk",
1647 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001648 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001649 vndk: {
1650 enabled: true,
1651 },
1652 nocrt: true,
1653 }
1654
1655 cc_library {
1656 name: "libvndk_sp_ext",
1657 vendor: true,
1658 vndk: {
1659 enabled: true,
1660 extends: "libvndk",
1661 support_system_process: true,
1662 },
1663 nocrt: true,
1664 }
1665 `)
1666
1667 testCcError(t, "module \".*\" with mismatched support_system_process", `
1668 cc_library {
1669 name: "libvndk_sp",
1670 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001671 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001672 vndk: {
1673 enabled: true,
1674 support_system_process: true,
1675 },
1676 nocrt: true,
1677 }
1678
1679 cc_library {
1680 name: "libvndk_ext",
1681 vendor: true,
1682 vndk: {
1683 enabled: true,
1684 extends: "libvndk_sp",
1685 },
1686 nocrt: true,
1687 }
1688 `)
1689}
1690
1691func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001692 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001693 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001694 // with `private: true`.
1695 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001696 cc_library {
1697 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001698 vendor_available: true,
1699 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001700 vndk: {
1701 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001702 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001703 },
1704 nocrt: true,
1705 }
1706
1707 cc_library {
1708 name: "libvndk_ext",
1709 vendor: true,
1710 vndk: {
1711 enabled: true,
1712 extends: "libvndk",
1713 },
1714 nocrt: true,
1715 }
1716 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001717
Justin Yunfd9e8042020-12-23 18:23:14 +09001718 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001719 cc_library {
1720 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001721 vendor_available: true,
1722 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001723 vndk: {
1724 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001725 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001726 },
1727 nocrt: true,
1728 }
1729
1730 cc_library {
1731 name: "libvndk_ext_product",
1732 product_specific: true,
1733 vndk: {
1734 enabled: true,
1735 extends: "libvndk",
1736 },
1737 nocrt: true,
1738 }
1739 `)
Logan Chienf3511742017-10-31 18:04:35 +08001740}
1741
Logan Chiend3c59a22018-03-29 14:08:15 +08001742func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001743 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001744 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001745 testCc(t, `
1746 cc_library {
1747 name: "libvndk",
1748 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001749 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001750 vndk: {
1751 enabled: true,
1752 },
1753 nocrt: true,
1754 }
1755
1756 cc_library {
1757 name: "libvndk_ext",
1758 vendor: true,
1759 vndk: {
1760 enabled: true,
1761 extends: "libvndk",
1762 },
1763 nocrt: true,
1764 }
1765
1766 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001767 name: "libvndk_sp",
1768 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001769 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001770 vndk: {
1771 enabled: true,
1772 support_system_process: true,
1773 },
1774 nocrt: true,
1775 }
1776
1777 cc_library {
1778 name: "libvndk_sp_ext",
1779 vendor: true,
1780 vndk: {
1781 enabled: true,
1782 extends: "libvndk_sp",
1783 support_system_process: true,
1784 },
1785 nocrt: true,
1786 }
1787
1788 cc_library {
1789 name: "libvendor",
1790 vendor: true,
1791 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1792 nocrt: true,
1793 }
1794 `)
1795}
1796
Logan Chiend3c59a22018-03-29 14:08:15 +08001797func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001798 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001799 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001800 testCc(t, `
1801 cc_library {
1802 name: "libvndk",
1803 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001804 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001805 vndk: {
1806 enabled: true,
1807 },
1808 nocrt: true,
1809 }
1810
1811 cc_library {
1812 name: "libvndk_ext",
1813 vendor: true,
1814 vndk: {
1815 enabled: true,
1816 extends: "libvndk",
1817 },
1818 shared_libs: ["libvendor"],
1819 nocrt: true,
1820 }
1821
1822 cc_library {
1823 name: "libvendor",
1824 vendor: true,
1825 nocrt: true,
1826 }
1827 `)
Logan Chienf3511742017-10-31 18:04:35 +08001828
Logan Chiend3c59a22018-03-29 14:08:15 +08001829 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1830 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001831 cc_library {
1832 name: "libvndk_sp",
1833 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001834 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001835 vndk: {
1836 enabled: true,
1837 support_system_process: true,
1838 },
1839 nocrt: true,
1840 }
1841
1842 cc_library {
1843 name: "libvndk_sp_ext",
1844 vendor: true,
1845 vndk: {
1846 enabled: true,
1847 extends: "libvndk_sp",
1848 support_system_process: true,
1849 },
1850 shared_libs: ["libvendor"], // Cause an error
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvendor",
1856 vendor: true,
1857 nocrt: true,
1858 }
1859 `)
1860}
1861
Justin Yun0ecf0b22020-02-28 15:07:59 +09001862func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001863 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001864 bp := `
1865 cc_library {
1866 name: "libvndk",
1867 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001868 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001869 vndk: {
1870 enabled: true,
1871 },
1872 nocrt: true,
1873 }
1874
1875 cc_library {
1876 name: "libvndk_ext_product",
1877 product_specific: true,
1878 vndk: {
1879 enabled: true,
1880 extends: "libvndk",
1881 },
1882 shared_libs: ["libproduct_for_vndklibs"],
1883 nocrt: true,
1884 }
1885
1886 cc_library {
1887 name: "libvndk_sp",
1888 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001889 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001890 vndk: {
1891 enabled: true,
1892 support_system_process: true,
1893 },
1894 nocrt: true,
1895 }
1896
1897 cc_library {
1898 name: "libvndk_sp_ext_product",
1899 product_specific: true,
1900 vndk: {
1901 enabled: true,
1902 extends: "libvndk_sp",
1903 support_system_process: true,
1904 },
1905 shared_libs: ["libproduct_for_vndklibs"],
1906 nocrt: true,
1907 }
1908
1909 cc_library {
1910 name: "libproduct",
1911 product_specific: true,
1912 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1913 nocrt: true,
1914 }
1915
1916 cc_library {
1917 name: "libproduct_for_vndklibs",
1918 product_specific: true,
1919 nocrt: true,
1920 }
1921 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001922 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001923 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1924 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001925 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001926
1927 testCcWithConfig(t, config)
1928}
1929
Logan Chiend3c59a22018-03-29 14:08:15 +08001930func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001931 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001932 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1933 // library.
1934 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1935 cc_library {
1936 name: "libvndk",
1937 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001938 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001939 vndk: {
1940 enabled: true,
1941 },
1942 nocrt: true,
1943 }
1944
1945 cc_library {
1946 name: "libvndk_sp",
1947 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001948 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001949 vndk: {
1950 enabled: true,
1951 support_system_process: true,
1952 },
1953 nocrt: true,
1954 }
1955
1956 cc_library {
1957 name: "libvndk_sp_ext",
1958 vendor: true,
1959 vndk: {
1960 enabled: true,
1961 extends: "libvndk_sp",
1962 support_system_process: true,
1963 },
1964 shared_libs: ["libvndk"], // Cause an error
1965 nocrt: true,
1966 }
1967 `)
1968
1969 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1970 // library.
1971 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1972 cc_library {
1973 name: "libvndk",
1974 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001975 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001976 vndk: {
1977 enabled: true,
1978 },
1979 nocrt: true,
1980 }
1981
1982 cc_library {
1983 name: "libvndk_ext",
1984 vendor: true,
1985 vndk: {
1986 enabled: true,
1987 extends: "libvndk",
1988 },
1989 nocrt: true,
1990 }
1991
1992 cc_library {
1993 name: "libvndk_sp",
1994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001995 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001996 vndk: {
1997 enabled: true,
1998 support_system_process: true,
1999 },
2000 nocrt: true,
2001 }
2002
2003 cc_library {
2004 name: "libvndk_sp_ext",
2005 vendor: true,
2006 vndk: {
2007 enabled: true,
2008 extends: "libvndk_sp",
2009 support_system_process: true,
2010 },
2011 shared_libs: ["libvndk_ext"], // Cause an error
2012 nocrt: true,
2013 }
2014 `)
2015}
2016
2017func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002018 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08002019 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2020 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002021 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2022 cc_library {
2023 name: "libvndk",
2024 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002025 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002026 vndk: {
2027 enabled: true,
2028 },
2029 nocrt: true,
2030 }
2031
2032 cc_library {
2033 name: "libvndk_ext",
2034 vendor: true,
2035 vndk: {
2036 enabled: true,
2037 extends: "libvndk",
2038 },
2039 nocrt: true,
2040 }
2041
2042 cc_library {
2043 name: "libvndk2",
2044 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002045 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002046 vndk: {
2047 enabled: true,
2048 },
2049 shared_libs: ["libvndk_ext"],
2050 nocrt: true,
2051 }
2052 `)
2053
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002054 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002055 cc_library {
2056 name: "libvndk",
2057 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002058 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002059 vndk: {
2060 enabled: true,
2061 },
2062 nocrt: true,
2063 }
2064
2065 cc_library {
2066 name: "libvndk_ext",
2067 vendor: true,
2068 vndk: {
2069 enabled: true,
2070 extends: "libvndk",
2071 },
2072 nocrt: true,
2073 }
2074
2075 cc_library {
2076 name: "libvndk2",
2077 vendor_available: true,
2078 vndk: {
2079 enabled: true,
2080 },
2081 target: {
2082 vendor: {
2083 shared_libs: ["libvndk_ext"],
2084 },
2085 },
2086 nocrt: true,
2087 }
2088 `)
2089
2090 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2091 cc_library {
2092 name: "libvndk_sp",
2093 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002094 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002095 vndk: {
2096 enabled: true,
2097 support_system_process: true,
2098 },
2099 nocrt: true,
2100 }
2101
2102 cc_library {
2103 name: "libvndk_sp_ext",
2104 vendor: true,
2105 vndk: {
2106 enabled: true,
2107 extends: "libvndk_sp",
2108 support_system_process: true,
2109 },
2110 nocrt: true,
2111 }
2112
2113 cc_library {
2114 name: "libvndk_sp_2",
2115 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002116 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002117 vndk: {
2118 enabled: true,
2119 support_system_process: true,
2120 },
2121 shared_libs: ["libvndk_sp_ext"],
2122 nocrt: true,
2123 }
2124 `)
2125
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002126 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002127 cc_library {
2128 name: "libvndk_sp",
2129 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002130 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002131 vndk: {
2132 enabled: true,
2133 },
2134 nocrt: true,
2135 }
2136
2137 cc_library {
2138 name: "libvndk_sp_ext",
2139 vendor: true,
2140 vndk: {
2141 enabled: true,
2142 extends: "libvndk_sp",
2143 },
2144 nocrt: true,
2145 }
2146
2147 cc_library {
2148 name: "libvndk_sp2",
2149 vendor_available: true,
2150 vndk: {
2151 enabled: true,
2152 },
2153 target: {
2154 vendor: {
2155 shared_libs: ["libvndk_sp_ext"],
2156 },
2157 },
2158 nocrt: true,
2159 }
2160 `)
2161}
2162
Justin Yun5f7f7e82019-11-18 19:52:14 +09002163func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002164 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002165 bp := `
2166 cc_library {
2167 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002168 llndk: {
2169 symbol_file: "libllndk.map.txt",
2170 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002171 }
2172 cc_library {
2173 name: "libvndk",
2174 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002175 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002176 vndk: {
2177 enabled: true,
2178 },
2179 nocrt: true,
2180 }
2181 cc_library {
2182 name: "libvndk_sp",
2183 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002184 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002185 vndk: {
2186 enabled: true,
2187 support_system_process: true,
2188 },
2189 nocrt: true,
2190 }
2191 cc_library {
2192 name: "libva",
2193 vendor_available: true,
2194 nocrt: true,
2195 }
2196 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002197 name: "libpa",
2198 product_available: true,
2199 nocrt: true,
2200 }
2201 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002202 name: "libboth_available",
2203 vendor_available: true,
2204 product_available: true,
2205 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002206 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002207 target: {
2208 vendor: {
2209 suffix: "-vendor",
2210 },
2211 product: {
2212 suffix: "-product",
2213 },
2214 }
2215 }
2216 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002217 name: "libproduct_va",
2218 product_specific: true,
2219 vendor_available: true,
2220 nocrt: true,
2221 }
2222 cc_library {
2223 name: "libprod",
2224 product_specific: true,
2225 shared_libs: [
2226 "libllndk",
2227 "libvndk",
2228 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002229 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002230 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002231 "libproduct_va",
2232 ],
2233 nocrt: true,
2234 }
2235 cc_library {
2236 name: "libvendor",
2237 vendor: true,
2238 shared_libs: [
2239 "libllndk",
2240 "libvndk",
2241 "libvndk_sp",
2242 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002243 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002244 "libproduct_va",
2245 ],
2246 nocrt: true,
2247 }
2248 `
2249
Paul Duffin8567f222021-03-23 00:02:06 +00002250 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002251
Jooyung Han261e1582020-10-20 18:54:21 +09002252 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2253 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002254
2255 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2256 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2257
2258 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2259 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002260
2261 ensureStringContains := func(t *testing.T, str string, substr string) {
2262 t.Helper()
2263 if !strings.Contains(str, substr) {
2264 t.Errorf("%q is not found in %v", substr, str)
2265 }
2266 }
2267 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2268 t.Helper()
2269 if strings.Contains(str, substr) {
2270 t.Errorf("%q is found in %v", substr, str)
2271 }
2272 }
2273
2274 // _static variant is used since _shared reuses *.o from the static variant
2275 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2276 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2277
2278 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2279 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2280 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2281 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2282
2283 product_cflags := product_static.Rule("cc").Args["cFlags"]
2284 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2285 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2286 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002287}
2288
2289func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002290 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002291 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002292 cc_library {
2293 name: "libprod",
2294 product_specific: true,
2295 shared_libs: [
2296 "libvendor",
2297 ],
2298 nocrt: true,
2299 }
2300 cc_library {
2301 name: "libvendor",
2302 vendor: true,
2303 nocrt: true,
2304 }
2305 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002306 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002307 cc_library {
2308 name: "libprod",
2309 product_specific: true,
2310 shared_libs: [
2311 "libsystem",
2312 ],
2313 nocrt: true,
2314 }
2315 cc_library {
2316 name: "libsystem",
2317 nocrt: true,
2318 }
2319 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002320 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002321 cc_library {
2322 name: "libprod",
2323 product_specific: true,
2324 shared_libs: [
2325 "libva",
2326 ],
2327 nocrt: true,
2328 }
2329 cc_library {
2330 name: "libva",
2331 vendor_available: true,
2332 nocrt: true,
2333 }
2334 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002335 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002336 cc_library {
2337 name: "libprod",
2338 product_specific: true,
2339 shared_libs: [
2340 "libvndk_private",
2341 ],
2342 nocrt: true,
2343 }
2344 cc_library {
2345 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002346 vendor_available: true,
2347 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002348 vndk: {
2349 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002350 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002351 },
2352 nocrt: true,
2353 }
2354 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002355 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002356 cc_library {
2357 name: "libprod",
2358 product_specific: true,
2359 shared_libs: [
2360 "libsystem_ext",
2361 ],
2362 nocrt: true,
2363 }
2364 cc_library {
2365 name: "libsystem_ext",
2366 system_ext_specific: true,
2367 nocrt: true,
2368 }
2369 `)
2370 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2371 cc_library {
2372 name: "libsystem",
2373 shared_libs: [
2374 "libproduct_va",
2375 ],
2376 nocrt: true,
2377 }
2378 cc_library {
2379 name: "libproduct_va",
2380 product_specific: true,
2381 vendor_available: true,
2382 nocrt: true,
2383 }
2384 `)
2385}
2386
Jooyung Han38002912019-05-16 04:01:54 +09002387func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002388 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002389 bp := `
2390 cc_library {
2391 name: "libvndk",
2392 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002393 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002394 vndk: {
2395 enabled: true,
2396 },
2397 }
2398 cc_library {
2399 name: "libvndksp",
2400 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002401 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002402 vndk: {
2403 enabled: true,
2404 support_system_process: true,
2405 },
2406 }
2407 cc_library {
2408 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002409 vendor_available: true,
2410 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002411 vndk: {
2412 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002413 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002414 },
2415 }
2416 cc_library {
2417 name: "libvendor",
2418 vendor: true,
2419 }
2420 cc_library {
2421 name: "libvndkext",
2422 vendor: true,
2423 vndk: {
2424 enabled: true,
2425 extends: "libvndk",
2426 },
2427 }
2428 vndk_prebuilt_shared {
2429 name: "prevndk",
2430 version: "27",
2431 target_arch: "arm",
2432 binder32bit: true,
2433 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002434 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002435 vndk: {
2436 enabled: true,
2437 },
2438 arch: {
2439 arm: {
2440 srcs: ["liba.so"],
2441 },
2442 },
2443 }
2444 cc_library {
2445 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002446 llndk: {
2447 symbol_file: "libllndk.map.txt",
2448 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002449 }
2450 cc_library {
2451 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002452 llndk: {
2453 symbol_file: "libllndkprivate.map.txt",
2454 private: true,
2455 }
Colin Cross78212242021-01-06 14:51:30 -08002456 }
2457
2458 llndk_libraries_txt {
2459 name: "llndk.libraries.txt",
2460 }
2461 vndkcore_libraries_txt {
2462 name: "vndkcore.libraries.txt",
2463 }
2464 vndksp_libraries_txt {
2465 name: "vndksp.libraries.txt",
2466 }
2467 vndkprivate_libraries_txt {
2468 name: "vndkprivate.libraries.txt",
2469 }
2470 vndkcorevariant_libraries_txt {
2471 name: "vndkcorevariant.libraries.txt",
2472 insert_vndk_version: false,
2473 }
2474 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002475
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002476 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002477 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002478 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002479 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002480 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002481
Colin Cross78212242021-01-06 14:51:30 -08002482 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2483 []string{"libvndk.so", "libvndkprivate.so"})
2484 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2485 []string{"libc++.so", "libvndksp.so"})
2486 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2487 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2488 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2489 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002490
Colin Crossfb0c16e2019-11-20 17:12:35 -08002491 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002492
Jooyung Han38002912019-05-16 04:01:54 +09002493 tests := []struct {
2494 variant string
2495 name string
2496 expected string
2497 }{
2498 {vendorVariant, "libvndk", "native:vndk"},
2499 {vendorVariant, "libvndksp", "native:vndk"},
2500 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2501 {vendorVariant, "libvendor", "native:vendor"},
2502 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002503 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002504 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002505 {coreVariant, "libvndk", "native:platform"},
2506 {coreVariant, "libvndkprivate", "native:platform"},
2507 {coreVariant, "libllndk", "native:platform"},
2508 }
2509 for _, test := range tests {
2510 t.Run(test.name, func(t *testing.T) {
2511 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2512 assertString(t, module.makeLinkType, test.expected)
2513 })
2514 }
2515}
2516
Jeff Gaston294356f2017-09-27 17:05:30 -07002517var staticLinkDepOrderTestCases = []struct {
2518 // This is a string representation of a map[moduleName][]moduleDependency .
2519 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002520 inStatic string
2521
2522 // This is a string representation of a map[moduleName][]moduleDependency .
2523 // It models the dependencies declared in an Android.bp file.
2524 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002525
2526 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2527 // The keys of allOrdered specify which modules we would like to check.
2528 // The values of allOrdered specify the expected result (of the transitive closure of all
2529 // dependencies) for each module to test
2530 allOrdered string
2531
2532 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2533 // The keys of outOrdered specify which modules we would like to check.
2534 // The values of outOrdered specify the expected result (of the ordered linker command line)
2535 // for each module to test.
2536 outOrdered string
2537}{
2538 // Simple tests
2539 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002540 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002541 outOrdered: "",
2542 },
2543 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002544 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002545 outOrdered: "a:",
2546 },
2547 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002548 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002549 outOrdered: "a:b; b:",
2550 },
2551 // Tests of reordering
2552 {
2553 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002554 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002555 outOrdered: "a:b,c,d; b:d; c:d; d:",
2556 },
2557 {
2558 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002559 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002560 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2561 },
2562 {
2563 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002564 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002565 outOrdered: "a:d,b,e,c; d:b; e:c",
2566 },
2567 {
2568 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002569 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002570 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2571 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2572 },
2573 {
2574 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002575 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 -07002576 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2577 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2578 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002579 // shared dependencies
2580 {
2581 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2582 // So, we don't actually have to check that a shared dependency of c will change the order
2583 // of a library that depends statically on b and on c. We only need to check that if c has
2584 // a shared dependency on b, that that shows up in allOrdered.
2585 inShared: "c:b",
2586 allOrdered: "c:b",
2587 outOrdered: "c:",
2588 },
2589 {
2590 // This test doesn't actually include any shared dependencies but it's a reminder of what
2591 // the second phase of the above test would look like
2592 inStatic: "a:b,c; c:b",
2593 allOrdered: "a:c,b; c:b",
2594 outOrdered: "a:c,b; c:b",
2595 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002596 // tiebreakers for when two modules specifying different orderings and there is no dependency
2597 // to dictate an order
2598 {
2599 // 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 -08002600 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002601 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2602 },
2603 {
2604 // 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 -08002605 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 -07002606 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2607 },
2608 // Tests involving duplicate dependencies
2609 {
2610 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002611 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002612 outOrdered: "a:c,b",
2613 },
2614 {
2615 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002616 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002617 outOrdered: "a:d,c,b",
2618 },
2619 // Tests to confirm the nonexistence of infinite loops.
2620 // These cases should never happen, so as long as the test terminates and the
2621 // result is deterministic then that should be fine.
2622 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002623 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002624 outOrdered: "a:a",
2625 },
2626 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002627 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002628 allOrdered: "a:b,c; b:c,a; c:a,b",
2629 outOrdered: "a:b; b:c; c:a",
2630 },
2631 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002632 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002633 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2634 outOrdered: "a:c,b; b:a,c; c:b,a",
2635 },
2636}
2637
2638// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2639func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2640 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2641 strippedText := strings.Replace(text, " ", "", -1)
2642 if len(strippedText) < 1 {
2643 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2644 }
2645 allDeps = make(map[android.Path][]android.Path, 0)
2646
2647 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2648 moduleTexts := strings.Split(strippedText, ";")
2649
2650 outputForModuleName := func(moduleName string) android.Path {
2651 return android.PathForTesting(moduleName)
2652 }
2653
2654 for _, moduleText := range moduleTexts {
2655 // convert from "a:b,c" to ["a", "b,c"]
2656 components := strings.Split(moduleText, ":")
2657 if len(components) != 2 {
2658 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2659 }
2660 moduleName := components[0]
2661 moduleOutput := outputForModuleName(moduleName)
2662 modulesInOrder = append(modulesInOrder, moduleOutput)
2663
2664 depString := components[1]
2665 // convert from "b,c" to ["b", "c"]
2666 depNames := strings.Split(depString, ",")
2667 if len(depString) < 1 {
2668 depNames = []string{}
2669 }
2670 var deps []android.Path
2671 for _, depName := range depNames {
2672 deps = append(deps, outputForModuleName(depName))
2673 }
2674 allDeps[moduleOutput] = deps
2675 }
2676 return modulesInOrder, allDeps
2677}
2678
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002679func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002680 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002681 ctx := testCc(t, `
2682 cc_library {
2683 name: "a",
2684 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002685 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002686 }
2687 cc_library {
2688 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002689 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002690 }
2691 cc_library {
2692 name: "c",
2693 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002694 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002695 }
2696 cc_library {
2697 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002698 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002699 }
2700
2701 `)
2702
Colin Cross7113d202019-11-20 16:39:12 -08002703 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002704 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002705 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2706 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002707 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002708
2709 if !reflect.DeepEqual(actual, expected) {
2710 t.Errorf("staticDeps orderings were not propagated correctly"+
2711 "\nactual: %v"+
2712 "\nexpected: %v",
2713 actual,
2714 expected,
2715 )
2716 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002717}
Jeff Gaston294356f2017-09-27 17:05:30 -07002718
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002719func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002720 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002721 ctx := testCc(t, `
2722 cc_library {
2723 name: "a",
2724 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002725 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002726 }
2727 cc_library {
2728 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002729 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002730 }
2731 cc_library {
2732 name: "c",
2733 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002734 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002735 }
2736
2737 `)
2738
Colin Cross7113d202019-11-20 16:39:12 -08002739 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002740 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002741 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2742 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002743 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002744
2745 if !reflect.DeepEqual(actual, expected) {
2746 t.Errorf("staticDeps orderings did not account for shared libs"+
2747 "\nactual: %v"+
2748 "\nexpected: %v",
2749 actual,
2750 expected,
2751 )
2752 }
2753}
2754
Jooyung Hanb04a4992020-03-13 18:57:35 +09002755func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002756 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002757 if !reflect.DeepEqual(actual, expected) {
2758 t.Errorf(message+
2759 "\nactual: %v"+
2760 "\nexpected: %v",
2761 actual,
2762 expected,
2763 )
2764 }
2765}
2766
Jooyung Han61b66e92020-03-21 14:21:46 +00002767func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002768 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002769 result := prepareForCcTest.RunTestWithBp(t, `
2770 cc_library {
2771 name: "libllndk",
2772 stubs: { versions: ["1", "2"] },
2773 llndk: {
2774 symbol_file: "libllndk.map.txt",
2775 },
2776 export_include_dirs: ["include"],
2777 }
2778
2779 cc_prebuilt_library_shared {
2780 name: "libllndkprebuilt",
2781 stubs: { versions: ["1", "2"] },
2782 llndk: {
2783 symbol_file: "libllndkprebuilt.map.txt",
2784 },
2785 }
2786
2787 cc_library {
2788 name: "libllndk_with_external_headers",
2789 stubs: { versions: ["1", "2"] },
2790 llndk: {
2791 symbol_file: "libllndk.map.txt",
2792 export_llndk_headers: ["libexternal_llndk_headers"],
2793 },
2794 header_libs: ["libexternal_headers"],
2795 export_header_lib_headers: ["libexternal_headers"],
2796 }
2797 cc_library_headers {
2798 name: "libexternal_headers",
2799 export_include_dirs: ["include"],
2800 vendor_available: true,
2801 }
2802 cc_library_headers {
2803 name: "libexternal_llndk_headers",
2804 export_include_dirs: ["include_llndk"],
2805 llndk: {
2806 symbol_file: "libllndk.map.txt",
2807 },
2808 vendor_available: true,
2809 }
2810
2811 cc_library {
2812 name: "libllndk_with_override_headers",
2813 stubs: { versions: ["1", "2"] },
2814 llndk: {
2815 symbol_file: "libllndk.map.txt",
2816 override_export_include_dirs: ["include_llndk"],
2817 },
2818 export_include_dirs: ["include"],
2819 }
2820 `)
2821 actual := result.ModuleVariantsForTests("libllndk")
2822 for i := 0; i < len(actual); i++ {
2823 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2824 actual = append(actual[:i], actual[i+1:]...)
2825 i--
2826 }
2827 }
2828 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002829 "android_vendor.29_arm64_armv8-a_shared_current",
2830 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002831 "android_vendor.29_arm_armv7-a-neon_shared_current",
2832 "android_vendor.29_arm_armv7-a-neon_shared",
2833 }
2834 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2835
2836 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2837 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2838
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002839 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2840 t.Helper()
2841 m := result.ModuleForTests(module, variant).Module()
2842 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2843 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2844 expectedDirs, f.IncludeDirs)
2845 }
2846
2847 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2848 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2849 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2850 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2851 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2852 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2853}
2854
Jiyong Parka46a4d52017-12-14 19:54:34 +09002855func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002856 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002857 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002858 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002859 name: "libllndk_headers",
2860 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002861 llndk: {
2862 llndk_headers: true,
2863 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002864 }
2865 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002866 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002867 llndk: {
2868 symbol_file: "libllndk.map.txt",
2869 export_llndk_headers: ["libllndk_headers"],
2870 }
Colin Cross0477b422020-10-13 18:43:54 -07002871 }
2872
2873 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002874 name: "libvendor",
2875 shared_libs: ["libllndk"],
2876 vendor: true,
2877 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002878 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002879 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002880 }
2881 `)
2882
2883 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002884 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002885 cflags := cc.Args["cFlags"]
2886 if !strings.Contains(cflags, "-Imy_include") {
2887 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2888 }
2889}
2890
Logan Chien43d34c32017-12-20 01:17:32 +08002891func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2892 actual := module.Properties.AndroidMkRuntimeLibs
2893 if !reflect.DeepEqual(actual, expected) {
2894 t.Errorf("incorrect runtime_libs for shared libs"+
2895 "\nactual: %v"+
2896 "\nexpected: %v",
2897 actual,
2898 expected,
2899 )
2900 }
2901}
2902
2903const runtimeLibAndroidBp = `
2904 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002905 name: "liball_available",
2906 vendor_available: true,
2907 product_available: true,
2908 no_libcrt : true,
2909 nocrt : true,
2910 system_shared_libs : [],
2911 }
2912 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002913 name: "libvendor_available1",
2914 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002915 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002916 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002917 nocrt : true,
2918 system_shared_libs : [],
2919 }
2920 cc_library {
2921 name: "libvendor_available2",
2922 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002923 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002924 target: {
2925 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002926 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002927 }
2928 },
Yi Konge7fe9912019-06-02 00:53:50 -07002929 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002930 nocrt : true,
2931 system_shared_libs : [],
2932 }
2933 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002934 name: "libproduct_vendor",
2935 product_specific: true,
2936 vendor_available: true,
2937 no_libcrt : true,
2938 nocrt : true,
2939 system_shared_libs : [],
2940 }
2941 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002942 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002943 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002944 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002945 nocrt : true,
2946 system_shared_libs : [],
2947 }
2948 cc_library {
2949 name: "libvendor1",
2950 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002951 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002952 nocrt : true,
2953 system_shared_libs : [],
2954 }
2955 cc_library {
2956 name: "libvendor2",
2957 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002958 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002959 no_libcrt : true,
2960 nocrt : true,
2961 system_shared_libs : [],
2962 }
2963 cc_library {
2964 name: "libproduct_available1",
2965 product_available: true,
2966 runtime_libs: ["liball_available"],
2967 no_libcrt : true,
2968 nocrt : true,
2969 system_shared_libs : [],
2970 }
2971 cc_library {
2972 name: "libproduct1",
2973 product_specific: true,
2974 no_libcrt : true,
2975 nocrt : true,
2976 system_shared_libs : [],
2977 }
2978 cc_library {
2979 name: "libproduct2",
2980 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002981 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002982 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002983 nocrt : true,
2984 system_shared_libs : [],
2985 }
2986`
2987
2988func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002989 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002990 ctx := testCc(t, runtimeLibAndroidBp)
2991
2992 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002993 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002994
Justin Yun8a2600c2020-12-07 12:44:03 +09002995 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2996 checkRuntimeLibs(t, []string{"liball_available"}, module)
2997
2998 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2999 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003000
3001 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003002 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003003
3004 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3005 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003006 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003007
Justin Yun8a2600c2020-12-07 12:44:03 +09003008 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3009 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003010
3011 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003012 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003013
3014 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3015 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003016 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003017
3018 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3019 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3020
3021 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003022 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003023}
3024
3025func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003026 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003027 ctx := testCc(t, runtimeLibAndroidBp)
3028
Colin Cross7113d202019-11-20 16:39:12 -08003029 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003030 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3031 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003032
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003033 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003034 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003035 checkRuntimeLibs(t, nil, module)
3036}
3037
3038func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003039 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003040 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3041
3042 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3043
Colin Cross7113d202019-11-20 16:39:12 -08003044 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003045
Justin Yun8a2600c2020-12-07 12:44:03 +09003046 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3047 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003048
3049 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003050 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003051
3052 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003053 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003054}
3055
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003056func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003057 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003058 actual := module.Properties.AndroidMkStaticLibs
3059 if !reflect.DeepEqual(actual, expected) {
3060 t.Errorf("incorrect static_libs"+
3061 "\nactual: %v"+
3062 "\nexpected: %v",
3063 actual,
3064 expected,
3065 )
3066 }
3067}
3068
3069const staticLibAndroidBp = `
3070 cc_library {
3071 name: "lib1",
3072 }
3073 cc_library {
3074 name: "lib2",
3075 static_libs: ["lib1"],
3076 }
3077`
3078
3079func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003080 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003081 ctx := testCc(t, staticLibAndroidBp)
3082
3083 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003084 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003085 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003086 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003087
3088 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003089 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003090 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3091 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003092 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003093}
3094
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003095func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3096 bp := `
3097 cc_library {
3098 name: "static_dep",
3099 }
3100 cc_library {
3101 name: "whole_static_dep",
3102 }
3103 cc_library {
3104 name: "shared_dep",
3105 }
3106 cc_library {
3107 name: "lib",
3108 bazel_module: { label: "//:lib" },
3109 static_libs: ["static_dep"],
3110 whole_static_libs: ["whole_static_dep"],
3111 shared_libs: ["shared_dep"],
3112 }
3113 cc_test {
3114 name: "test",
3115 bazel_module: { label: "//:test" },
3116 static_libs: ["static_dep"],
3117 whole_static_libs: ["whole_static_dep"],
3118 shared_libs: ["shared_dep"],
3119 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04003120 sanitize: {
3121 // cc_test modules default to memtag_heap: true,
3122 // but this adds extra dependencies that we don't care about
3123 never: true,
3124 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003125 }
3126 cc_binary {
3127 name: "binary",
3128 bazel_module: { label: "//:binary" },
3129 static_libs: ["static_dep"],
3130 whole_static_libs: ["whole_static_dep"],
3131 shared_libs: ["shared_dep"],
3132 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003133 cc_library_headers {
3134 name: "lib_headers",
3135 bazel_module: { label: "//:lib_headers" },
3136 static_libs: ["static_dep"],
3137 whole_static_libs: ["whole_static_dep"],
3138 shared_libs: ["shared_dep"],
3139 }
3140 cc_prebuilt_library {
3141 name: "lib_prebuilt",
3142 bazel_module: { label: "//:lib_prebuilt" },
3143 static_libs: ["static_dep"],
3144 whole_static_libs: ["whole_static_dep"],
3145 shared_libs: ["shared_dep"],
3146 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003147 `
3148
3149 testCases := []struct {
3150 name string
3151 moduleName string
3152 variant string
3153 androidMkInfo cquery.CcAndroidMkInfo
3154 }{
3155 {
3156 name: "shared lib",
3157 moduleName: "lib",
3158 variant: "android_arm64_armv8-a_shared",
3159 androidMkInfo: cquery.CcAndroidMkInfo{
3160 LocalStaticLibs: []string{"static_dep"},
3161 LocalWholeStaticLibs: []string{"whole_static_dep"},
3162 LocalSharedLibs: []string{"shared_dep"},
3163 },
3164 },
3165 {
3166 name: "static lib",
3167 moduleName: "lib",
3168 variant: "android_arm64_armv8-a_static",
3169 androidMkInfo: cquery.CcAndroidMkInfo{
3170 LocalStaticLibs: []string{"static_dep"},
3171 LocalWholeStaticLibs: []string{"whole_static_dep"},
3172 LocalSharedLibs: []string{"shared_dep"},
3173 },
3174 },
3175 {
3176 name: "cc_test arm64",
3177 moduleName: "test",
3178 variant: "android_arm64_armv8-a",
3179 androidMkInfo: cquery.CcAndroidMkInfo{
3180 LocalStaticLibs: []string{"static_dep"},
3181 LocalWholeStaticLibs: []string{"whole_static_dep"},
3182 LocalSharedLibs: []string{"shared_dep"},
3183 },
3184 },
3185 {
3186 name: "cc_test arm",
3187 moduleName: "test",
3188 variant: "android_arm_armv7-a-neon",
3189 androidMkInfo: cquery.CcAndroidMkInfo{
3190 LocalStaticLibs: []string{"static_dep"},
3191 LocalWholeStaticLibs: []string{"whole_static_dep"},
3192 LocalSharedLibs: []string{"shared_dep"},
3193 },
3194 },
3195 {
3196 name: "cc_binary",
3197 moduleName: "binary",
3198 variant: "android_arm64_armv8-a",
3199 androidMkInfo: cquery.CcAndroidMkInfo{
3200 LocalStaticLibs: []string{"static_dep"},
3201 LocalWholeStaticLibs: []string{"whole_static_dep"},
3202 LocalSharedLibs: []string{"shared_dep"},
3203 },
3204 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003205 {
3206 name: "cc_library_headers",
3207 moduleName: "lib_headers",
3208 variant: "android_arm64_armv8-a",
3209 androidMkInfo: cquery.CcAndroidMkInfo{
3210 LocalStaticLibs: []string{"static_dep"},
3211 LocalWholeStaticLibs: []string{"whole_static_dep"},
3212 LocalSharedLibs: []string{"shared_dep"},
3213 },
3214 },
3215 {
3216 name: "prebuilt lib static",
3217 moduleName: "lib_prebuilt",
3218 variant: "android_arm64_armv8-a_static",
3219 androidMkInfo: cquery.CcAndroidMkInfo{
3220 LocalStaticLibs: []string{"static_dep"},
3221 LocalWholeStaticLibs: []string{"whole_static_dep"},
3222 LocalSharedLibs: []string{"shared_dep"},
3223 },
3224 },
3225 {
3226 name: "prebuilt lib shared",
3227 moduleName: "lib_prebuilt",
3228 variant: "android_arm64_armv8-a_shared",
3229 androidMkInfo: cquery.CcAndroidMkInfo{
3230 LocalStaticLibs: []string{"static_dep"},
3231 LocalWholeStaticLibs: []string{"whole_static_dep"},
3232 LocalSharedLibs: []string{"shared_dep"},
3233 },
3234 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003235 }
3236
3237 outputBaseDir := "out/bazel"
3238 for _, tc := range testCases {
3239 t.Run(tc.name, func(t *testing.T) {
3240 result := android.GroupFixturePreparers(
3241 prepareForCcTest,
3242 android.FixtureModifyConfig(func(config android.Config) {
3243 config.BazelContext = android.MockBazelContext{
3244 OutputBaseDir: outputBaseDir,
3245 LabelToCcInfo: map[string]cquery.CcInfo{
3246 "//:lib": cquery.CcInfo{
3247 CcAndroidMkInfo: tc.androidMkInfo,
3248 RootDynamicLibraries: []string{""},
3249 },
3250 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3251 CcAndroidMkInfo: tc.androidMkInfo,
3252 RootStaticArchives: []string{""},
3253 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003254 "//:lib_headers": cquery.CcInfo{
3255 CcAndroidMkInfo: tc.androidMkInfo,
3256 OutputFiles: []string{""},
3257 },
3258 "//:lib_prebuilt": cquery.CcInfo{
3259 CcAndroidMkInfo: tc.androidMkInfo,
3260 },
3261 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3262 CcAndroidMkInfo: tc.androidMkInfo,
3263 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003264 },
3265 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003266 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003267 CcAndroidMkInfo: tc.androidMkInfo,
3268 },
3269 "//:binary": cquery.CcUnstrippedInfo{
3270 CcAndroidMkInfo: tc.androidMkInfo,
3271 },
3272 },
3273 }
3274 }),
3275 ).RunTestWithBp(t, bp)
3276 ctx := result.TestContext
3277
3278 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3279 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003280 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3281 t.Errorf("incorrect static_libs"+
3282 "\nactual: %v"+
3283 "\nexpected: %v",
3284 module.Properties.AndroidMkStaticLibs,
3285 tc.androidMkInfo.LocalStaticLibs,
3286 )
3287 }
3288 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3289 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3290 tc.androidMkInfo.LocalStaticLibs,
3291 )
3292 if staticDepsDiffer {
3293 t.Errorf(
3294 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3295 tc.androidMkInfo.LocalStaticLibs,
3296 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3297 missingStaticDeps,
3298 additionalStaticDeps,
3299 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003300 }
3301
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003302 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3303 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3304 tc.androidMkInfo.LocalWholeStaticLibs,
3305 module.Properties.AndroidMkWholeStaticLibs,
3306 )
3307 }
3308 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3309 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3310 tc.androidMkInfo.LocalWholeStaticLibs,
3311 )
3312 if wholeStaticDepsDiffer {
3313 t.Errorf(
3314 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3315 tc.androidMkInfo.LocalWholeStaticLibs,
3316 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3317 missingWholeStaticDeps,
3318 additionalWholeStaticDeps,
3319 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003320 }
3321
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003322 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3323 t.Errorf("incorrect shared_libs"+
3324 "\nactual: %v"+
3325 "\nexpected: %v",
3326 module.Properties.AndroidMkSharedLibs,
3327 tc.androidMkInfo.LocalSharedLibs,
3328 )
3329 }
3330 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3331 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3332 tc.androidMkInfo.LocalSharedLibs,
3333 )
3334 if sharedDepsDiffer {
3335 t.Errorf(
3336 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3337 tc.androidMkInfo.LocalSharedLibs,
3338 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3339 missingSharedDeps,
3340 additionalSharedDeps,
3341 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003342 }
3343 })
3344 }
3345}
3346
Jiyong Parkd08b6972017-09-26 10:50:54 +09003347var compilerFlagsTestCases = []struct {
3348 in string
3349 out bool
3350}{
3351 {
3352 in: "a",
3353 out: false,
3354 },
3355 {
3356 in: "-a",
3357 out: true,
3358 },
3359 {
3360 in: "-Ipath/to/something",
3361 out: false,
3362 },
3363 {
3364 in: "-isystempath/to/something",
3365 out: false,
3366 },
3367 {
3368 in: "--coverage",
3369 out: false,
3370 },
3371 {
3372 in: "-include a/b",
3373 out: true,
3374 },
3375 {
3376 in: "-include a/b c/d",
3377 out: false,
3378 },
3379 {
3380 in: "-DMACRO",
3381 out: true,
3382 },
3383 {
3384 in: "-DMAC RO",
3385 out: false,
3386 },
3387 {
3388 in: "-a -b",
3389 out: false,
3390 },
3391 {
3392 in: "-DMACRO=definition",
3393 out: true,
3394 },
3395 {
3396 in: "-DMACRO=defi nition",
3397 out: true, // TODO(jiyong): this should be false
3398 },
3399 {
3400 in: "-DMACRO(x)=x + 1",
3401 out: true,
3402 },
3403 {
3404 in: "-DMACRO=\"defi nition\"",
3405 out: true,
3406 },
3407}
3408
3409type mockContext struct {
3410 BaseModuleContext
3411 result bool
3412}
3413
3414func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3415 // CheckBadCompilerFlags calls this function when the flag should be rejected
3416 ctx.result = false
3417}
3418
3419func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003420 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003421 for _, testCase := range compilerFlagsTestCases {
3422 ctx := &mockContext{result: true}
3423 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3424 if ctx.result != testCase.out {
3425 t.Errorf("incorrect output:")
3426 t.Errorf(" input: %#v", testCase.in)
3427 t.Errorf(" expected: %#v", testCase.out)
3428 t.Errorf(" got: %#v", ctx.result)
3429 }
3430 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003431}
Jiyong Park374510b2018-03-19 18:23:01 +09003432
Jiyong Park37b25202018-07-11 10:49:27 +09003433func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003434 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003435 ctx := testCc(t, `
3436 cc_library_shared {
3437 name: "librecovery",
3438 recovery: true,
3439 }
3440 cc_library_shared {
3441 name: "librecovery32",
3442 recovery: true,
3443 compile_multilib:"32",
3444 }
Jiyong Park5baac542018-08-28 09:55:37 +09003445 cc_library_shared {
3446 name: "libHalInRecovery",
3447 recovery_available: true,
3448 vendor: true,
3449 }
Jiyong Park37b25202018-07-11 10:49:27 +09003450 `)
3451
3452 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003453 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003454 if len(variants) != 1 || !android.InList(arm64, variants) {
3455 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3456 }
3457
3458 variants = ctx.ModuleVariantsForTests("librecovery32")
3459 if android.InList(arm64, variants) {
3460 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3461 }
Jiyong Park5baac542018-08-28 09:55:37 +09003462
3463 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3464 if !recoveryModule.Platform() {
3465 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3466 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003467}
Jiyong Park5baac542018-08-28 09:55:37 +09003468
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003469func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003470 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003471 bp := `
3472 cc_prebuilt_test_library_shared {
3473 name: "test_lib",
3474 relative_install_path: "foo/bar/baz",
3475 srcs: ["srcpath/dontusethispath/baz.so"],
3476 }
3477
3478 cc_test {
3479 name: "main_test",
3480 data_libs: ["test_lib"],
3481 gtest: false,
3482 }
3483 `
3484
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003485 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003486 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003487 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003488 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3489
3490 ctx := testCcWithConfig(t, config)
3491 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3492 testBinary := module.(*Module).linker.(*testBinary)
3493 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3494 if err != nil {
3495 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3496 }
3497 if len(outputFiles) != 1 {
3498 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3499 }
3500 if len(testBinary.dataPaths()) != 1 {
3501 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3502 }
3503
3504 outputPath := outputFiles[0].String()
3505
3506 if !strings.HasSuffix(outputPath, "/main_test") {
3507 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3508 }
Colin Crossaa255532020-07-03 13:18:24 -07003509 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003510 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3511 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3512 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3513 }
3514}
3515
Jiyong Park7ed9de32018-10-15 22:25:07 +09003516func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003517 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003518 ctx := testCc(t, `
3519 cc_library_shared {
3520 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003521 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003522 stubs: {
3523 symbol_file: "foo.map.txt",
3524 versions: ["1", "2", "3"],
3525 },
3526 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003527
Jiyong Park7ed9de32018-10-15 22:25:07 +09003528 cc_library_shared {
3529 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003530 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003531 shared_libs: ["libFoo#1"],
3532 }`)
3533
3534 variants := ctx.ModuleVariantsForTests("libFoo")
3535 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003536 "android_arm64_armv8-a_shared",
3537 "android_arm64_armv8-a_shared_1",
3538 "android_arm64_armv8-a_shared_2",
3539 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003540 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003541 "android_arm_armv7-a-neon_shared",
3542 "android_arm_armv7-a-neon_shared_1",
3543 "android_arm_armv7-a-neon_shared_2",
3544 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003545 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003546 }
3547 variantsMismatch := false
3548 if len(variants) != len(expectedVariants) {
3549 variantsMismatch = true
3550 } else {
3551 for _, v := range expectedVariants {
3552 if !inList(v, variants) {
3553 variantsMismatch = false
3554 }
3555 }
3556 }
3557 if variantsMismatch {
3558 t.Errorf("variants of libFoo expected:\n")
3559 for _, v := range expectedVariants {
3560 t.Errorf("%q\n", v)
3561 }
3562 t.Errorf(", but got:\n")
3563 for _, v := range variants {
3564 t.Errorf("%q\n", v)
3565 }
3566 }
3567
Colin Cross7113d202019-11-20 16:39:12 -08003568 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003569 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003570 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003571 if !strings.Contains(libFlags, libFoo1StubPath) {
3572 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3573 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003574
Colin Cross7113d202019-11-20 16:39:12 -08003575 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003576 cFlags := libBarCompileRule.Args["cFlags"]
3577 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3578 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3579 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3580 }
Jiyong Park37b25202018-07-11 10:49:27 +09003581}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003582
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003583func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3584 t.Parallel()
3585 ctx := testCc(t, `
3586 cc_library_shared {
3587 name: "libFoo",
3588 srcs: ["foo.c"],
3589 stubs: {
3590 symbol_file: "foo.map.txt",
3591 versions: ["current"],
3592 },
3593 apex_available: ["bar", "a1"],
3594 }
3595
3596 cc_library_shared {
3597 name: "libBar",
3598 srcs: ["bar.c"],
3599 shared_libs: ["libFoo"],
3600 apex_available: ["a1"],
3601 }
3602
3603 cc_library_shared {
3604 name: "libA1",
3605 srcs: ["a1.c"],
3606 shared_libs: ["libFoo"],
3607 apex_available: ["a1"],
3608 }
3609
3610 cc_library_shared {
3611 name: "libBarA1",
3612 srcs: ["bara1.c"],
3613 shared_libs: ["libFoo"],
3614 apex_available: ["bar", "a1"],
3615 }
3616
3617 cc_library_shared {
3618 name: "libAnyApex",
3619 srcs: ["anyApex.c"],
3620 shared_libs: ["libFoo"],
3621 apex_available: ["//apex_available:anyapex"],
3622 }
3623
3624 cc_library_shared {
3625 name: "libBaz",
3626 srcs: ["baz.c"],
3627 shared_libs: ["libFoo"],
3628 apex_available: ["baz"],
3629 }
3630
3631 cc_library_shared {
3632 name: "libQux",
3633 srcs: ["qux.c"],
3634 shared_libs: ["libFoo"],
3635 apex_available: ["qux", "bar"],
3636 }`)
3637
3638 variants := ctx.ModuleVariantsForTests("libFoo")
3639 expectedVariants := []string{
3640 "android_arm64_armv8-a_shared",
3641 "android_arm64_armv8-a_shared_current",
3642 "android_arm_armv7-a-neon_shared",
3643 "android_arm_armv7-a-neon_shared_current",
3644 }
3645 variantsMismatch := false
3646 if len(variants) != len(expectedVariants) {
3647 variantsMismatch = true
3648 } else {
3649 for _, v := range expectedVariants {
3650 if !inList(v, variants) {
3651 variantsMismatch = false
3652 }
3653 }
3654 }
3655 if variantsMismatch {
3656 t.Errorf("variants of libFoo expected:\n")
3657 for _, v := range expectedVariants {
3658 t.Errorf("%q\n", v)
3659 }
3660 t.Errorf(", but got:\n")
3661 for _, v := range variants {
3662 t.Errorf("%q\n", v)
3663 }
3664 }
3665
3666 linkAgainstFoo := []string{"libBarA1"}
3667 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3668
3669 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3670 for _, lib := range linkAgainstFoo {
3671 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3672 libFlags := libLinkRule.Args["libFlags"]
3673 if !strings.Contains(libFlags, libFooPath) {
3674 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3675 }
3676 }
3677
3678 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3679 for _, lib := range linkAgainstFooStubs {
3680 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3681 libFlags := libLinkRule.Args["libFlags"]
3682 if !strings.Contains(libFlags, libFooStubPath) {
3683 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3684 }
3685 }
3686}
3687
Sam Delmerico75dbca22023-04-20 13:13:25 +00003688func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003689 t.Parallel()
3690 bp := `
3691 cc_library_shared {
3692 name: "libFoo",
3693 bazel_module: { label: "//:libFoo" },
3694 srcs: ["foo.c"],
3695 stubs: {
3696 symbol_file: "foo.map.txt",
3697 versions: ["current"],
3698 },
3699 apex_available: ["bar", "a1"],
3700 }
3701
3702 cc_library_shared {
3703 name: "libBar",
3704 srcs: ["bar.c"],
3705 shared_libs: ["libFoo"],
3706 apex_available: ["a1"],
3707 }
3708
3709 cc_library_shared {
3710 name: "libA1",
3711 srcs: ["a1.c"],
3712 shared_libs: ["libFoo"],
3713 apex_available: ["a1"],
3714 }
3715
3716 cc_library_shared {
3717 name: "libBarA1",
3718 srcs: ["bara1.c"],
3719 shared_libs: ["libFoo"],
3720 apex_available: ["bar", "a1"],
3721 }
3722
3723 cc_library_shared {
3724 name: "libAnyApex",
3725 srcs: ["anyApex.c"],
3726 shared_libs: ["libFoo"],
3727 apex_available: ["//apex_available:anyapex"],
3728 }
3729
3730 cc_library_shared {
3731 name: "libBaz",
3732 srcs: ["baz.c"],
3733 shared_libs: ["libFoo"],
3734 apex_available: ["baz"],
3735 }
3736
3737 cc_library_shared {
3738 name: "libQux",
3739 srcs: ["qux.c"],
3740 shared_libs: ["libFoo"],
3741 apex_available: ["qux", "bar"],
3742 }`
3743
3744 result := android.GroupFixturePreparers(
3745 prepareForCcTest,
3746 android.FixtureModifyConfig(func(config android.Config) {
3747 config.BazelContext = android.MockBazelContext{
3748 OutputBaseDir: "out/bazel",
3749 LabelToCcInfo: map[string]cquery.CcInfo{
3750 "//:libFoo": {
3751 RootDynamicLibraries: []string{"libFoo.so"},
3752 },
3753 "//:libFoo_stub_libs-current": {
3754 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3755 },
3756 },
3757 }
3758 }),
3759 ).RunTestWithBp(t, bp)
3760 ctx := result.TestContext
3761
3762 variants := ctx.ModuleVariantsForTests("libFoo")
3763 expectedVariants := []string{
3764 "android_arm64_armv8-a_shared",
3765 "android_arm64_armv8-a_shared_current",
3766 "android_arm_armv7-a-neon_shared",
3767 "android_arm_armv7-a-neon_shared_current",
3768 }
3769 variantsMismatch := false
3770 if len(variants) != len(expectedVariants) {
3771 variantsMismatch = true
3772 } else {
3773 for _, v := range expectedVariants {
3774 if !inList(v, variants) {
3775 variantsMismatch = false
3776 }
3777 }
3778 }
3779 if variantsMismatch {
3780 t.Errorf("variants of libFoo expected:\n")
3781 for _, v := range expectedVariants {
3782 t.Errorf("%q\n", v)
3783 }
3784 t.Errorf(", but got:\n")
3785 for _, v := range variants {
3786 t.Errorf("%q\n", v)
3787 }
3788 }
3789
3790 linkAgainstFoo := []string{"libBarA1"}
3791 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3792
3793 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3794 for _, lib := range linkAgainstFoo {
3795 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3796 libFlags := libLinkRule.Args["libFlags"]
3797 if !strings.Contains(libFlags, libFooPath) {
3798 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3799 }
3800 }
3801
3802 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3803 for _, lib := range linkAgainstFooStubs {
3804 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3805 libFlags := libLinkRule.Args["libFlags"]
3806 if !strings.Contains(libFlags, libFooStubPath) {
3807 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3808 }
3809 }
3810}
3811
Jooyung Hanb04a4992020-03-13 18:57:35 +09003812func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003813 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003814 for _, tc := range []struct{ moduleName, expected string }{
3815 {"libc", "__LIBC_API__"},
3816 {"libfoo", "__LIBFOO_API__"},
3817 {"libfoo@1", "__LIBFOO_1_API__"},
3818 {"libfoo-v1", "__LIBFOO_V1_API__"},
3819 {"libfoo.v1", "__LIBFOO_V1_API__"},
3820 } {
3821 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3822 }
3823}
3824
Liz Kammer83cf81b2022-09-22 08:24:20 -04003825func pathsToBase(paths android.Paths) []string {
3826 var ret []string
3827 for _, p := range paths {
3828 ret = append(ret, p.Base())
3829 }
3830 return ret
3831}
3832
3833func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003834 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003835 ctx := testCc(t, `
3836 cc_library_static {
3837 name: "foo",
3838 srcs: ["foo.c"],
3839 }
3840
3841 cc_library_static {
3842 name: "bar",
3843 srcs: ["bar.c"],
3844 }
3845
3846 cc_library_shared {
3847 name: "qux",
3848 srcs: ["qux.c"],
3849 }
3850
3851 cc_library_static {
3852 name: "baz",
3853 srcs: ["baz.c"],
3854 static_libs: ["foo"],
3855 shared_libs: ["qux"],
3856 whole_static_libs: ["bar"],
3857 }`)
3858
3859 variant := "android_arm64_armv8-a_static"
3860 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3861
3862 // For static libraries, the object files of a whole static dep are included in the archive
3863 // directly
3864 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3865 t.Errorf("Expected input objects %q, got %q", w, g)
3866 }
3867
3868 // non whole static dependencies are not linked into the archive
3869 if len(arRule.Implicits) > 0 {
3870 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3871 }
3872}
3873
3874func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003875 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003876 ctx := testCc(t, `
3877 cc_library_static {
3878 name: "foo",
3879 srcs: ["foo.c"],
3880 }
3881
3882 cc_library_static {
3883 name: "bar",
3884 srcs: ["bar.c"],
3885 }
3886
3887 cc_library_shared {
3888 name: "qux",
3889 srcs: ["qux.c"],
3890 }
3891
3892 cc_library_shared {
3893 name: "baz",
3894 srcs: ["baz.c"],
3895 static_libs: ["foo"],
3896 shared_libs: ["qux"],
3897 whole_static_libs: ["bar"],
3898 }`)
3899
3900 variant := "android_arm64_armv8-a_shared"
3901 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3902 libFlags := linkRule.Args["libFlags"]
3903 // When dynamically linking, we expect static dependencies to be found on the command line
3904 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3905 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3906 }
3907 // When dynamically linking, we expect whole static dependencies to be found on the command line
3908 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3909 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3910 }
3911
3912 // When dynamically linking, we expect shared dependencies to be found on the command line
3913 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3914 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3915 }
3916
3917 // We should only have the objects from the shared library srcs, not the whole static dependencies
3918 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3919 t.Errorf("Expected input objects %q, got %q", w, g)
3920 }
3921}
3922
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003923func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003924 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003925 ctx := testCc(t, `
3926 cc_binary {
3927 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003928 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003929 static_executable: true,
3930 }`)
3931
Colin Cross7113d202019-11-20 16:39:12 -08003932 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003933 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3934 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003935 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003936 for _, lib := range systemStaticLibs {
3937 if !strings.Contains(libFlags, lib) {
3938 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3939 }
3940 }
3941 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3942 for _, lib := range systemSharedLibs {
3943 if strings.Contains(libFlags, lib) {
3944 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3945 }
3946 }
3947}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003948
3949func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003950 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003951 ctx := testCc(t, `
3952 cc_binary {
3953 name: "mybin",
3954 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003955 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003956 static_executable: true,
3957 stl: "none",
3958 }
3959
3960 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003961 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003962 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003963 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003964 stl: "none",
3965 }
3966
3967 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003968 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003969 srcs: ["foo.c"],
3970 stl: "none",
3971 stubs: {
3972 versions: ["1"],
3973 },
3974 }`)
3975
Colin Cross0de8a1e2020-09-18 14:15:30 -07003976 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3977 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003978 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003979
3980 if !reflect.DeepEqual(actual, expected) {
3981 t.Errorf("staticDeps orderings were not propagated correctly"+
3982 "\nactual: %v"+
3983 "\nexpected: %v",
3984 actual,
3985 expected,
3986 )
3987 }
3988}
Jooyung Han38002912019-05-16 04:01:54 +09003989
Jooyung Hand48f3c32019-08-23 11:18:57 +09003990func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003991 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003992 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3993 cc_library {
3994 name: "libA",
3995 srcs: ["foo.c"],
3996 shared_libs: ["libB"],
3997 stl: "none",
3998 }
3999
4000 cc_library {
4001 name: "libB",
4002 srcs: ["foo.c"],
4003 enabled: false,
4004 stl: "none",
4005 }
4006 `)
4007}
4008
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004009func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
4010 bp := `
4011 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00004012 name: "test_afl_fuzz_target",
4013 srcs: ["foo.c"],
4014 host_supported: true,
4015 static_libs: [
4016 "afl_fuzz_static_lib",
4017 ],
4018 shared_libs: [
4019 "afl_fuzz_shared_lib",
4020 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004021 fuzzing_frameworks: {
4022 afl: true,
4023 libfuzzer: false,
4024 },
Cory Barkera1da26f2022-06-07 20:12:06 +00004025 }
4026 cc_library {
4027 name: "afl_fuzz_static_lib",
4028 host_supported: true,
4029 srcs: ["static_file.c"],
4030 }
4031 cc_library {
4032 name: "libfuzzer_only_static_lib",
4033 host_supported: true,
4034 srcs: ["static_file.c"],
4035 }
4036 cc_library {
4037 name: "afl_fuzz_shared_lib",
4038 host_supported: true,
4039 srcs: ["shared_file.c"],
4040 static_libs: [
4041 "second_static_lib",
4042 ],
4043 }
4044 cc_library_headers {
4045 name: "libafl_headers",
4046 vendor_available: true,
4047 host_supported: true,
4048 export_include_dirs: [
4049 "include",
4050 "instrumentation",
4051 ],
4052 }
4053 cc_object {
4054 name: "afl-compiler-rt",
4055 vendor_available: true,
4056 host_supported: true,
4057 cflags: [
4058 "-fPIC",
4059 ],
4060 srcs: [
4061 "instrumentation/afl-compiler-rt.o.c",
4062 ],
4063 }
4064 cc_library {
4065 name: "second_static_lib",
4066 host_supported: true,
4067 srcs: ["second_file.c"],
4068 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004069 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00004070 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004071 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00004072 srcs: [
4073 "aflpp_driver.c",
4074 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004075 }`
4076
4077 testEnv := map[string]string{
4078 "FUZZ_FRAMEWORK": "AFL",
4079 }
4080
4081 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00004082
4083 checkPcGuardFlag := func(
4084 modName string, variantName string, shouldHave bool) {
4085 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
4086
4087 cFlags, ok := cc.Args["cFlags"]
4088 if !ok {
4089 t.Errorf("Could not find cFlags for module %s and variant %s",
4090 modName, variantName)
4091 }
4092
4093 if strings.Contains(
4094 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
4095 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
4096 "Test failed for module %s and variant %s",
4097 !shouldHave, shouldHave, modName, variantName)
4098 }
4099 }
4100
Cory Barkera1da26f2022-06-07 20:12:06 +00004101 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004102 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004103
4104 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004105 checkPcGuardFlag(moduleName, variant+"_static", false)
4106 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004107
4108 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004109 checkPcGuardFlag(moduleName, variant+"_static", false)
4110 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004111
4112 ctx.ModuleForTests("afl_fuzz_shared_lib",
4113 "android_arm64_armv8-a_shared").Rule("cc")
4114 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004115 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
4116}
4117
4118func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004119 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004120 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
4121}
4122
4123func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004124 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004125 if runtime.GOOS != "linux" {
4126 t.Skip("requires linux")
4127 }
4128
4129 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00004130}
4131
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004132// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4133// correctly.
4134func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004135 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004136 ctx := testCc(t, `
4137 cc_fuzz {
4138 name: "fuzz_smoke_test",
4139 srcs: ["foo.c"],
4140 }`)
4141
Paul Duffin075c4172019-12-19 19:06:13 +00004142 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004143 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4144}
4145
Jooyung Han38002912019-05-16 04:01:54 +09004146func assertString(t *testing.T, got, expected string) {
4147 t.Helper()
4148 if got != expected {
4149 t.Errorf("expected %q got %q", expected, got)
4150 }
4151}
4152
4153func assertArrayString(t *testing.T, got, expected []string) {
4154 t.Helper()
4155 if len(got) != len(expected) {
4156 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4157 return
4158 }
4159 for i := range got {
4160 if got[i] != expected[i] {
4161 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4162 i, expected[i], expected, got[i], got)
4163 return
4164 }
4165 }
4166}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004167
Jooyung Han0302a842019-10-30 18:43:49 +09004168func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4169 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004170 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004171}
4172
Colin Crosse1bb5d02019-09-24 14:55:04 -07004173func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004174 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004175 ctx := testCc(t, `
4176 cc_defaults {
4177 name: "defaults",
4178 srcs: ["foo.c"],
4179 static: {
4180 srcs: ["bar.c"],
4181 },
4182 shared: {
4183 srcs: ["baz.c"],
4184 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004185 bazel_module: {
4186 bp2build_available: true,
4187 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004188 }
4189
4190 cc_library_static {
4191 name: "libstatic",
4192 defaults: ["defaults"],
4193 }
4194
4195 cc_library_shared {
4196 name: "libshared",
4197 defaults: ["defaults"],
4198 }
4199
4200 cc_library {
4201 name: "libboth",
4202 defaults: ["defaults"],
4203 }
4204
4205 cc_binary {
4206 name: "binary",
4207 defaults: ["defaults"],
4208 }`)
4209
Colin Cross7113d202019-11-20 16:39:12 -08004210 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004211 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4212 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4213 }
Colin Cross7113d202019-11-20 16:39:12 -08004214 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004215 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4216 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4217 }
Colin Cross7113d202019-11-20 16:39:12 -08004218 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004219 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4220 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4221 }
4222
Colin Cross7113d202019-11-20 16:39:12 -08004223 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004224 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4225 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4226 }
Colin Cross7113d202019-11-20 16:39:12 -08004227 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004228 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4229 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4230 }
4231}
Colin Crosseabaedd2020-02-06 17:01:55 -08004232
4233func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004234 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004235 bp := `
4236 cc_defaults {
4237 name: "libfoo_defaults",
4238 srcs: ["foo.c"],
4239 cppflags: ["-DFOO"],
4240 product_variables: {
4241 debuggable: {
4242 cppflags: ["-DBAR"],
4243 },
4244 },
4245 }
4246
4247 cc_library {
4248 name: "libfoo",
4249 defaults: ["libfoo_defaults"],
4250 }
4251 `
4252
Paul Duffin8567f222021-03-23 00:02:06 +00004253 result := android.GroupFixturePreparers(
4254 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004255 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004256
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004257 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4258 variables.Debuggable = BoolPtr(true)
4259 }),
4260 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004261
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004262 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004263 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004264}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004265
4266func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4267 t.Parallel()
4268 bp := `
4269 cc_library_static {
4270 name: "libfoo",
4271 srcs: ["foo.c"],
4272 whole_static_libs: ["libbar"],
4273 }
4274
4275 cc_library_static {
4276 name: "libbar",
4277 whole_static_libs: ["libmissing"],
4278 }
4279 `
4280
Paul Duffin8567f222021-03-23 00:02:06 +00004281 result := android.GroupFixturePreparers(
4282 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004283 android.PrepareForTestWithAllowMissingDependencies,
4284 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004285
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004286 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004287 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004288
Paul Duffine84b1332021-03-12 11:59:43 +00004289 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004290
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004291 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004292 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004293}
Colin Crosse9fe2942020-11-10 18:12:15 -08004294
4295func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004296 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004297 bp := `
4298 cc_binary {
4299 name: "bin",
4300 host_supported: true,
4301 shared_libs: ["libshared"],
4302 runtime_libs: ["libruntime"],
4303 srcs: [":gen"],
4304 }
4305
4306 cc_library_shared {
4307 name: "libshared",
4308 host_supported: true,
4309 shared_libs: ["libtransitive"],
4310 }
4311
4312 cc_library_shared {
4313 name: "libtransitive",
4314 host_supported: true,
4315 }
4316
4317 cc_library_shared {
4318 name: "libruntime",
4319 host_supported: true,
4320 }
4321
4322 cc_binary_host {
4323 name: "tool",
4324 srcs: ["foo.cpp"],
4325 }
4326
4327 genrule {
4328 name: "gen",
4329 tools: ["tool"],
4330 out: ["gen.cpp"],
4331 cmd: "$(location tool) $(out)",
4332 }
4333 `
4334
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004335 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004336 ctx := testCcWithConfig(t, config)
4337
4338 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4339 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4340 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4341 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4342 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4343
4344 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4345 t.Errorf("expected host bin dependency %q, got %q", w, g)
4346 }
4347
4348 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4349 t.Errorf("expected host bin dependency %q, got %q", w, g)
4350 }
4351
4352 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4353 t.Errorf("expected host bin dependency %q, got %q", w, g)
4354 }
4355
4356 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4357 t.Errorf("expected host bin dependency %q, got %q", w, g)
4358 }
4359
4360 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4361 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4362 }
4363
4364 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4365 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4366 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4367 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4368
4369 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4370 t.Errorf("expected device bin dependency %q, got %q", w, g)
4371 }
4372
4373 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4374 t.Errorf("expected device bin dependency %q, got %q", w, g)
4375 }
4376
4377 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4378 t.Errorf("expected device bin dependency %q, got %q", w, g)
4379 }
4380
4381 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4382 t.Errorf("expected device bin dependency %q, got %q", w, g)
4383 }
4384
4385 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4386 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4387 }
4388
4389}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004390
4391func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004392 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004393 ctx := testCc(t, `
4394 cc_library_shared {
4395 name: "libclient",
4396 srcs: ["foo.c"],
4397 shared_libs: ["libfoo#1"],
4398 }
4399
4400 cc_library_shared {
4401 name: "libfoo",
4402 srcs: ["foo.c"],
4403 shared_libs: ["libbar"],
4404 export_shared_lib_headers: ["libbar"],
4405 stubs: {
4406 symbol_file: "foo.map.txt",
4407 versions: ["1", "2", "3"],
4408 },
4409 }
4410
4411 cc_library_shared {
4412 name: "libbar",
4413 export_include_dirs: ["include/libbar"],
4414 srcs: ["foo.c"],
4415 }`)
4416
4417 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4418
4419 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4420 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4421 }
4422}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004423
Vinh Tran09581952023-05-16 16:03:20 -04004424func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004425 t.Parallel()
4426 ctx := android.GroupFixturePreparers(
4427 prepareForCcTest,
4428 aidl_library.PrepareForTestWithAidlLibrary,
4429 android.MockFS{
4430 "package_bar/Android.bp": []byte(`
4431 aidl_library {
4432 name: "bar",
4433 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004434 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004435 strip_import_prefix: "x",
4436 }
4437 `)}.AddToFixture(),
4438 android.MockFS{
4439 "package_foo/Android.bp": []byte(`
4440 aidl_library {
4441 name: "foo",
4442 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004443 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004444 strip_import_prefix: "a",
4445 deps: ["bar"],
4446 }
4447 cc_library {
4448 name: "libfoo",
4449 aidl: {
4450 libs: ["foo"],
4451 }
4452 }
4453 `),
4454 }.AddToFixture(),
4455 ).RunTest(t).TestContext
4456
4457 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004458
4459 android.AssertPathsRelativeToTopEquals(
4460 t,
4461 "aidl headers",
4462 []string{
4463 "package_bar/x/HeaderBar.aidl",
4464 "package_foo/a/HeaderFoo.aidl",
4465 "package_foo/a/b/Foo.aidl",
4466 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4467 },
4468 libfoo.Rule("aidl_library").Implicits,
4469 )
4470
4471 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004472 aidlCommand := manifest.Commands[0].GetCommand()
4473
4474 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4475 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4476 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4477 }
4478
4479 outputs := strings.Join(libfoo.AllOutputs(), " ")
4480
Vinh Tran09581952023-05-16 16:03:20 -04004481 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4482 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4483 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004484 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4485 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004486 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4487 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4488 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004489 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4490}
4491
Jooyung Hane197d8b2021-01-05 10:33:16 +09004492func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004493 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004494 ctx := android.GroupFixturePreparers(
4495 prepareForCcTest,
4496 aidl_library.PrepareForTestWithAidlLibrary,
4497 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004498 cc_library {
4499 name: "libfoo",
4500 srcs: ["a/Foo.aidl"],
4501 aidl: { flags: ["-Werror"], },
4502 }
4503 `)
4504
4505 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4506 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4507 aidlCommand := manifest.Commands[0].GetCommand()
4508 expectedAidlFlag := "-Werror"
4509 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4510 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4511 }
4512}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004513
Jooyung Han07f70c02021-11-06 07:08:45 +09004514func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004515 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004516 for _, tc := range []struct {
4517 name string
4518 sdkVersion string
4519 variant string
4520 expected string
4521 }{
4522 {
4523 name: "default is current",
4524 sdkVersion: "",
4525 variant: "android_arm64_armv8-a_static",
4526 expected: "platform_apis",
4527 },
4528 {
4529 name: "use sdk_version",
4530 sdkVersion: `sdk_version: "29"`,
4531 variant: "android_arm64_armv8-a_static",
4532 expected: "platform_apis",
4533 },
4534 {
4535 name: "use sdk_version(sdk variant)",
4536 sdkVersion: `sdk_version: "29"`,
4537 variant: "android_arm64_armv8-a_sdk_static",
4538 expected: "29",
4539 },
4540 {
4541 name: "use min_sdk_version",
4542 sdkVersion: `min_sdk_version: "29"`,
4543 variant: "android_arm64_armv8-a_static",
4544 expected: "29",
4545 },
4546 } {
4547 t.Run(tc.name, func(t *testing.T) {
4548 ctx := testCc(t, `
4549 cc_library {
4550 name: "libfoo",
4551 stl: "none",
4552 srcs: ["a/Foo.aidl"],
4553 `+tc.sdkVersion+`
4554 }
4555 `)
4556 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4557 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4558 aidlCommand := manifest.Commands[0].GetCommand()
4559 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4560 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4561 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4562 }
4563 })
4564 }
4565}
4566
Vinh Tran09581952023-05-16 16:03:20 -04004567func TestInvalidAidlProp(t *testing.T) {
4568 t.Parallel()
4569
4570 testCases := []struct {
4571 description string
4572 bp string
4573 }{
4574 {
4575 description: "Invalid use of aidl.libs and aidl.include_dirs",
4576 bp: `
4577 cc_library {
4578 name: "foo",
4579 aidl: {
4580 libs: ["foo_aidl"],
4581 include_dirs: ["bar/include"],
4582 }
4583 }
4584 `,
4585 },
4586 {
4587 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4588 bp: `
4589 cc_library {
4590 name: "foo",
4591 aidl: {
4592 libs: ["foo_aidl"],
4593 local_include_dirs: ["include"],
4594 }
4595 }
4596 `,
4597 },
4598 }
4599
4600 for _, testCase := range testCases {
4601 t.Run(testCase.description, func(t *testing.T) {
4602 bp := `
4603 aidl_library {
4604 name: "foo_aidl",
4605 srcs: ["Foo.aidl"],
4606 } ` + testCase.bp
4607 android.GroupFixturePreparers(
4608 prepareForCcTest,
4609 aidl_library.PrepareForTestWithAidlLibrary.
4610 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4611 ).RunTestWithBp(t, bp)
4612 })
4613 }
4614}
4615
Jiyong Parka008fb02021-03-16 17:15:53 +09004616func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004617 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004618 ctx := testCc(t, `
4619 cc_library_shared {
4620 name: "libfoo",
4621 srcs: ["foo.c"],
4622 min_sdk_version: "29",
4623 }`)
4624
4625 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4626 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4627}
4628
Vinh Tranf1924742022-06-24 16:40:11 -04004629func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004630 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004631 bp := `
4632 cc_library_shared {
4633 name: "libfoo",
4634 srcs: ["foo.c"],
4635 min_sdk_version: "S",
4636 }
4637 `
4638 result := android.GroupFixturePreparers(
4639 prepareForCcTest,
4640 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4641 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4642 }),
4643 ).RunTestWithBp(t, bp)
4644 ctx := result.TestContext
4645 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4646 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4647}
4648
Paul Duffin3cb603e2021-02-19 13:57:10 +00004649func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004650 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004651
4652 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4653 // embedded newline characters alone.
4654 trimIndentingSpaces := func(s string) string {
4655 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4656 }
4657
4658 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4659 t.Helper()
4660 expected = trimIndentingSpaces(expected)
4661 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4662 if expected != actual {
4663 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4664 }
4665 }
4666
4667 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4668
4669 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4670 t.Helper()
4671 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4672 name := module.Name()
4673
4674 for _, checker := range checkers {
4675 checker(t, name, exported)
4676 }
4677 }
4678
4679 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4680 return func(t *testing.T, name string, exported FlagExporterInfo) {
4681 t.Helper()
4682 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4683 }
4684 }
4685
4686 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4687 return func(t *testing.T, name string, exported FlagExporterInfo) {
4688 t.Helper()
4689 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4690 }
4691 }
4692
4693 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4694 return func(t *testing.T, name string, exported FlagExporterInfo) {
4695 t.Helper()
4696 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4697 }
4698 }
4699
4700 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4701 return func(t *testing.T, name string, exported FlagExporterInfo) {
4702 t.Helper()
4703 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4704 }
4705 }
4706
4707 genRuleModules := `
4708 genrule {
4709 name: "genrule_foo",
4710 cmd: "generate-foo",
4711 out: [
4712 "generated_headers/foo/generated_header.h",
4713 ],
4714 export_include_dirs: [
4715 "generated_headers",
4716 ],
4717 }
4718
4719 genrule {
4720 name: "genrule_bar",
4721 cmd: "generate-bar",
4722 out: [
4723 "generated_headers/bar/generated_header.h",
4724 ],
4725 export_include_dirs: [
4726 "generated_headers",
4727 ],
4728 }
4729 `
4730
4731 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4732 ctx := testCc(t, genRuleModules+`
4733 cc_library {
4734 name: "libfoo",
4735 srcs: ["foo.c"],
4736 export_include_dirs: ["foo/standard"],
4737 export_system_include_dirs: ["foo/system"],
4738 generated_headers: ["genrule_foo"],
4739 export_generated_headers: ["genrule_foo"],
4740 }
4741
4742 cc_library {
4743 name: "libbar",
4744 srcs: ["bar.c"],
4745 shared_libs: ["libfoo"],
4746 export_include_dirs: ["bar/standard"],
4747 export_system_include_dirs: ["bar/system"],
4748 generated_headers: ["genrule_bar"],
4749 export_generated_headers: ["genrule_bar"],
4750 }
4751 `)
4752 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4753 checkIncludeDirs(t, ctx, foo,
4754 expectedIncludeDirs(`
4755 foo/standard
4756 .intermediates/genrule_foo/gen/generated_headers
4757 `),
4758 expectedSystemIncludeDirs(`foo/system`),
4759 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4760 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4761 )
4762
4763 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4764 checkIncludeDirs(t, ctx, bar,
4765 expectedIncludeDirs(`
4766 bar/standard
4767 .intermediates/genrule_bar/gen/generated_headers
4768 `),
4769 expectedSystemIncludeDirs(`bar/system`),
4770 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4771 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4772 )
4773 })
4774
4775 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4776 ctx := testCc(t, genRuleModules+`
4777 cc_library {
4778 name: "libfoo",
4779 srcs: ["foo.c"],
4780 export_include_dirs: ["foo/standard"],
4781 export_system_include_dirs: ["foo/system"],
4782 generated_headers: ["genrule_foo"],
4783 export_generated_headers: ["genrule_foo"],
4784 }
4785
4786 cc_library {
4787 name: "libbar",
4788 srcs: ["bar.c"],
4789 whole_static_libs: ["libfoo"],
4790 export_include_dirs: ["bar/standard"],
4791 export_system_include_dirs: ["bar/system"],
4792 generated_headers: ["genrule_bar"],
4793 export_generated_headers: ["genrule_bar"],
4794 }
4795 `)
4796 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4797 checkIncludeDirs(t, ctx, foo,
4798 expectedIncludeDirs(`
4799 foo/standard
4800 .intermediates/genrule_foo/gen/generated_headers
4801 `),
4802 expectedSystemIncludeDirs(`foo/system`),
4803 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4804 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4805 )
4806
4807 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4808 checkIncludeDirs(t, ctx, bar,
4809 expectedIncludeDirs(`
4810 bar/standard
4811 foo/standard
4812 .intermediates/genrule_foo/gen/generated_headers
4813 .intermediates/genrule_bar/gen/generated_headers
4814 `),
4815 expectedSystemIncludeDirs(`
4816 bar/system
4817 foo/system
4818 `),
4819 expectedGeneratedHeaders(`
4820 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4821 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4822 `),
4823 expectedOrderOnlyDeps(`
4824 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4825 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4826 `),
4827 )
4828 })
4829
Paul Duffin3cb603e2021-02-19 13:57:10 +00004830 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004831 ctx := android.GroupFixturePreparers(
4832 prepareForCcTest,
4833 aidl_library.PrepareForTestWithAidlLibrary,
4834 ).RunTestWithBp(t, `
4835 aidl_library {
4836 name: "libfoo_aidl",
4837 srcs: ["x/y/Bar.aidl"],
4838 strip_import_prefix: "x",
4839 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004840 cc_library_shared {
4841 name: "libfoo",
4842 srcs: [
4843 "foo.c",
4844 "b.aidl",
4845 "a.proto",
4846 ],
4847 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004848 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004849 export_aidl_headers: true,
4850 }
4851 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004852 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004853 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4854 checkIncludeDirs(t, ctx, foo,
4855 expectedIncludeDirs(`
4856 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004857 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004858 `),
4859 expectedSystemIncludeDirs(``),
4860 expectedGeneratedHeaders(`
4861 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4862 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4863 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004864 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4865 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4866 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004867 `),
4868 expectedOrderOnlyDeps(`
4869 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4870 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4871 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004872 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4873 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4874 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004875 `),
4876 )
4877 })
4878
Paul Duffin3cb603e2021-02-19 13:57:10 +00004879 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4880 ctx := testCc(t, genRuleModules+`
4881 cc_library_shared {
4882 name: "libfoo",
4883 srcs: [
4884 "foo.c",
4885 "b.aidl",
4886 "a.proto",
4887 ],
4888 proto: {
4889 export_proto_headers: true,
4890 }
4891 }
4892 `)
4893 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4894 checkIncludeDirs(t, ctx, foo,
4895 expectedIncludeDirs(`
4896 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4897 `),
4898 expectedSystemIncludeDirs(``),
4899 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004900 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4901 `),
4902 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004903 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4904 `),
4905 )
4906 })
4907
Paul Duffin33056e82021-02-19 13:49:08 +00004908 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004909 ctx := testCc(t, genRuleModules+`
4910 cc_library_shared {
4911 name: "libfoo",
4912 srcs: [
4913 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004914 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004915 "b.aidl",
4916 "a.proto",
4917 ],
4918 }
4919 `)
4920 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4921 checkIncludeDirs(t, ctx, foo,
4922 expectedIncludeDirs(`
4923 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4924 `),
4925 expectedSystemIncludeDirs(``),
4926 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004927 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004928 `),
4929 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004930 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4931 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004932 `),
4933 )
4934 })
4935}
Colin Crossae628182021-06-14 16:52:28 -07004936
4937func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004938 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004939 baseExpectedFlags := []string{
4940 "${config.ArmThumbCflags}",
4941 "${config.ArmCflags}",
4942 "${config.CommonGlobalCflags}",
4943 "${config.DeviceGlobalCflags}",
4944 "${config.ExternalCflags}",
4945 "${config.ArmToolchainCflags}",
4946 "${config.ArmArmv7ANeonCflags}",
4947 "${config.ArmGenericCflags}",
4948 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004949 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004950 }
4951
4952 expectedIncludes := []string{
4953 "external/foo/android_arm_export_include_dirs",
4954 "external/foo/lib32_export_include_dirs",
4955 "external/foo/arm_export_include_dirs",
4956 "external/foo/android_export_include_dirs",
4957 "external/foo/linux_export_include_dirs",
4958 "external/foo/export_include_dirs",
4959 "external/foo/android_arm_local_include_dirs",
4960 "external/foo/lib32_local_include_dirs",
4961 "external/foo/arm_local_include_dirs",
4962 "external/foo/android_local_include_dirs",
4963 "external/foo/linux_local_include_dirs",
4964 "external/foo/local_include_dirs",
4965 "external/foo",
4966 "external/foo/libheader1",
4967 "external/foo/libheader2",
4968 "external/foo/libwhole1",
4969 "external/foo/libwhole2",
4970 "external/foo/libstatic1",
4971 "external/foo/libstatic2",
4972 "external/foo/libshared1",
4973 "external/foo/libshared2",
4974 "external/foo/liblinux",
4975 "external/foo/libandroid",
4976 "external/foo/libarm",
4977 "external/foo/lib32",
4978 "external/foo/libandroid_arm",
4979 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004980 }
4981
4982 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4983 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4984
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004985 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004986 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004987 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004988
4989 lastIncludes := []string{
4990 "out/soong/ndk/sysroot/usr/include",
4991 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4992 }
4993
4994 combineSlices := func(slices ...[]string) []string {
4995 var ret []string
4996 for _, s := range slices {
4997 ret = append(ret, s...)
4998 }
4999 return ret
5000 }
5001
5002 testCases := []struct {
5003 name string
5004 src string
5005 expected []string
5006 }{
5007 {
5008 name: "c",
5009 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09005010 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005011 },
5012 {
5013 name: "cc",
5014 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09005015 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04005016 },
5017 {
5018 name: "assemble",
5019 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09005020 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04005021 },
5022 }
5023
5024 for _, tc := range testCases {
5025 t.Run(tc.name, func(t *testing.T) {
5026 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005027 cc_library {
5028 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04005029 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05005030 cflags: ["-std=candcpp"],
5031 conlyflags: ["-std=conly"],
5032 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07005033 local_include_dirs: ["local_include_dirs"],
5034 export_include_dirs: ["export_include_dirs"],
5035 export_system_include_dirs: ["export_system_include_dirs"],
5036 static_libs: ["libstatic1", "libstatic2"],
5037 whole_static_libs: ["libwhole1", "libwhole2"],
5038 shared_libs: ["libshared1", "libshared2"],
5039 header_libs: ["libheader1", "libheader2"],
5040 target: {
5041 android: {
5042 shared_libs: ["libandroid"],
5043 local_include_dirs: ["android_local_include_dirs"],
5044 export_include_dirs: ["android_export_include_dirs"],
5045 },
5046 android_arm: {
5047 shared_libs: ["libandroid_arm"],
5048 local_include_dirs: ["android_arm_local_include_dirs"],
5049 export_include_dirs: ["android_arm_export_include_dirs"],
5050 },
5051 linux: {
5052 shared_libs: ["liblinux"],
5053 local_include_dirs: ["linux_local_include_dirs"],
5054 export_include_dirs: ["linux_export_include_dirs"],
5055 },
5056 },
5057 multilib: {
5058 lib32: {
5059 shared_libs: ["lib32"],
5060 local_include_dirs: ["lib32_local_include_dirs"],
5061 export_include_dirs: ["lib32_export_include_dirs"],
5062 },
5063 },
5064 arch: {
5065 arm: {
5066 shared_libs: ["libarm"],
5067 local_include_dirs: ["arm_local_include_dirs"],
5068 export_include_dirs: ["arm_export_include_dirs"],
5069 },
5070 },
5071 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005072 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005073 }
5074
5075 cc_library_headers {
5076 name: "libheader1",
5077 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005078 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005079 stl: "none",
5080 }
5081
5082 cc_library_headers {
5083 name: "libheader2",
5084 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005085 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005086 stl: "none",
5087 }
Liz Kammer08572c62021-09-30 10:11:04 -04005088 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07005089
Liz Kammer08572c62021-09-30 10:11:04 -04005090 libs := []string{
5091 "libstatic1",
5092 "libstatic2",
5093 "libwhole1",
5094 "libwhole2",
5095 "libshared1",
5096 "libshared2",
5097 "libandroid",
5098 "libandroid_arm",
5099 "liblinux",
5100 "lib32",
5101 "libarm",
5102 }
Colin Crossae628182021-06-14 16:52:28 -07005103
Liz Kammer08572c62021-09-30 10:11:04 -04005104 for _, lib := range libs {
5105 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07005106 cc_library {
5107 name: "%s",
5108 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00005109 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07005110 stl: "none",
5111 }
5112 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04005113 }
5114
5115 ctx := android.GroupFixturePreparers(
5116 PrepareForIntegrationTestWithCc,
5117 android.FixtureAddTextFile("external/foo/Android.bp", bp),
5118 ).RunTest(t)
5119 // Use the arm variant instead of the arm64 variant so that it gets headers from
5120 // ndk_libandroid_support to test LateStaticLibs.
5121 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
5122
5123 var includes []string
5124 flags := strings.Split(cflags, " ")
5125 for _, flag := range flags {
5126 if strings.HasPrefix(flag, "-I") {
5127 includes = append(includes, strings.TrimPrefix(flag, "-I"))
5128 } else if flag == "-isystem" {
5129 // skip isystem, include next
5130 } else if len(flag) > 0 {
5131 includes = append(includes, flag)
5132 }
5133 }
5134
5135 android.AssertArrayString(t, "includes", tc.expected, includes)
5136 })
Colin Crossae628182021-06-14 16:52:28 -07005137 }
5138
Colin Crossae628182021-06-14 16:52:28 -07005139}
Alixb5f6d9e2022-04-20 23:00:58 +00005140
zijunzhao933e3802023-01-12 07:26:20 +00005141func TestAddnoOverride64GlobalCflags(t *testing.T) {
5142 t.Parallel()
5143 ctx := testCc(t, `
5144 cc_library_shared {
5145 name: "libclient",
5146 srcs: ["foo.c"],
5147 shared_libs: ["libfoo#1"],
5148 }
5149
5150 cc_library_shared {
5151 name: "libfoo",
5152 srcs: ["foo.c"],
5153 shared_libs: ["libbar"],
5154 export_shared_lib_headers: ["libbar"],
5155 stubs: {
5156 symbol_file: "foo.map.txt",
5157 versions: ["1", "2", "3"],
5158 },
5159 }
5160
5161 cc_library_shared {
5162 name: "libbar",
5163 export_include_dirs: ["include/libbar"],
5164 srcs: ["foo.c"],
5165 }`)
5166
5167 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5168
5169 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5170 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5171 }
5172}
5173
Alixb5f6d9e2022-04-20 23:00:58 +00005174func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005175 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005176 tests := []struct {
5177 name string
5178 clang bool
5179 BuildBrokenClangProperty bool
5180 err string
5181 }{
5182 {
5183 name: "error when clang is set to false",
5184 clang: false,
5185 err: "is no longer supported",
5186 },
5187 {
5188 name: "error when clang is set to true",
5189 clang: true,
5190 err: "property is deprecated, see Changes.md",
5191 },
5192 {
5193 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5194 clang: true,
5195 BuildBrokenClangProperty: true,
5196 },
5197 }
5198
5199 for _, test := range tests {
5200 t.Run(test.name, func(t *testing.T) {
5201 bp := fmt.Sprintf(`
5202 cc_library {
5203 name: "foo",
5204 clang: %t,
5205 }`, test.clang)
5206
5207 if test.err == "" {
5208 android.GroupFixturePreparers(
5209 prepareForCcTest,
5210 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5211 if test.BuildBrokenClangProperty {
5212 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5213 }
5214 }),
5215 ).RunTestWithBp(t, bp)
5216 } else {
5217 prepareForCcTest.
5218 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5219 RunTestWithBp(t, bp)
5220 }
5221 })
5222 }
5223}
Alix Espinoef47e542022-09-14 19:10:51 +00005224
5225func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005226 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005227 tests := []struct {
5228 name string
5229 clangAsFlags []string
5230 BuildBrokenClangAsFlags bool
5231 err string
5232 }{
5233 {
5234 name: "error when clang_asflags is set",
5235 clangAsFlags: []string{"-a", "-b"},
5236 err: "clang_asflags: property is deprecated",
5237 },
5238 {
5239 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5240 clangAsFlags: []string{"-a", "-b"},
5241 BuildBrokenClangAsFlags: true,
5242 },
5243 }
5244
5245 for _, test := range tests {
5246 t.Run(test.name, func(t *testing.T) {
5247 bp := fmt.Sprintf(`
5248 cc_library {
5249 name: "foo",
5250 clang_asflags: %s,
5251 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5252
5253 if test.err == "" {
5254 android.GroupFixturePreparers(
5255 prepareForCcTest,
5256 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5257 if test.BuildBrokenClangAsFlags {
5258 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5259 }
5260 }),
5261 ).RunTestWithBp(t, bp)
5262 } else {
5263 prepareForCcTest.
5264 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5265 RunTestWithBp(t, bp)
5266 }
5267 })
5268 }
5269}
5270
5271func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005272 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005273 tests := []struct {
5274 name string
5275 clangCFlags []string
5276 BuildBrokenClangCFlags bool
5277 err string
5278 }{
5279 {
5280 name: "error when clang_cflags is set",
5281 clangCFlags: []string{"-a", "-b"},
5282 err: "clang_cflags: property is deprecated",
5283 },
5284 {
5285 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5286 clangCFlags: []string{"-a", "-b"},
5287 BuildBrokenClangCFlags: true,
5288 },
5289 }
5290
5291 for _, test := range tests {
5292 t.Run(test.name, func(t *testing.T) {
5293 bp := fmt.Sprintf(`
5294 cc_library {
5295 name: "foo",
5296 clang_cflags: %s,
5297 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5298
5299 if test.err == "" {
5300 android.GroupFixturePreparers(
5301 prepareForCcTest,
5302 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5303 if test.BuildBrokenClangCFlags {
5304 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5305 }
5306 }),
5307 ).RunTestWithBp(t, bp)
5308 } else {
5309 prepareForCcTest.
5310 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5311 RunTestWithBp(t, bp)
5312 }
5313 })
5314 }
5315}
Yu Liue4312402023-01-18 09:15:31 -08005316
5317func TestDclaLibraryInApex(t *testing.T) {
5318 t.Parallel()
5319 bp := `
5320 cc_library_shared {
5321 name: "cc_lib_in_apex",
5322 srcs: ["foo.cc"],
5323 apex_available: ["myapex"],
5324 bazel_module: { label: "//foo/bar:bar" },
5325 }`
5326 label := "//foo/bar:bar"
5327 arch64 := "arm64_armv8-a"
5328 arch32 := "arm_armv7-a-neon"
5329 apexCfgKey := android.ApexConfigKey{
5330 WithinApex: true,
5331 ApexSdkVersion: "28",
5332 }
5333
5334 result := android.GroupFixturePreparers(
5335 prepareForCcTest,
5336 android.FixtureRegisterWithContext(registerTestMutators),
5337 android.FixtureModifyConfig(func(config android.Config) {
5338 config.BazelContext = android.MockBazelContext{
5339 OutputBaseDir: "outputbase",
5340 LabelToCcInfo: map[string]cquery.CcInfo{
5341 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5342 RootDynamicLibraries: []string{"foo.so"},
5343 },
5344 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5345 RootDynamicLibraries: []string{"foo.so"},
5346 },
5347 },
5348 BazelRequests: make(map[string]bool),
5349 }
5350 }),
5351 ).RunTestWithBp(t, bp)
5352 ctx := result.TestContext
5353
5354 // Test if the bazel request is queued correctly
5355 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5356 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5357 t.Errorf("Bazel request was not queued: %s", key)
5358 }
5359
5360 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5361 producer := sharedFoo.(android.OutputFileProducer)
5362 outputFiles, err := producer.OutputFiles("")
5363 if err != nil {
5364 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5365 }
5366 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5367 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5368}
Sam Delmericoef69d472023-04-18 17:32:43 -04005369
5370func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5371 t.Parallel()
5372 bp := `
5373 cc_library_static {
5374 name: "foo_ubsan_minimal",
5375 srcs: ["foo.cc"],
5376 bazel_module: { label: "//foo_ubsan_minimal" },
5377 sanitize: {
5378 all_undefined: true,
5379 integer_overflow: true,
5380 },
5381 }
5382 cc_library_static {
5383 name: "foo",
5384 srcs: ["foo.cc"],
5385 bazel_module: { label: "//foo" },
5386 sanitize: {
5387 address: true,
5388 hwaddress: true,
5389 fuzzer: true,
5390 integer_overflow: true,
5391 scs: true,
5392 },
5393 }
5394 cc_library_static {
5395 name: "foo_tsan",
5396 srcs: ["foo.cc"],
5397 bazel_module: { label: "//foo_tsan" },
5398 sanitize: {
5399 thread: true,
5400 },
5401 }
5402 cc_library_static {
5403 name: "foo_cfi",
5404 srcs: ["foo.cc"],
5405 bazel_module: { label: "//foo_cfi" },
5406 sanitize: {
5407 cfi: true,
5408 },
5409 }
5410 cc_library_static {
5411 name: "foo_memtag_stack",
5412 srcs: ["foo.cc"],
5413 bazel_module: { label: "//foo_memtag_stack" },
5414 sanitize: {
5415 memtag_stack: true,
5416 },
5417 }
5418 cc_library_static {
5419 name: "foo_memtag_heap",
5420 srcs: ["foo.cc"],
5421 bazel_module: { label: "//foo_memtag_heap" },
5422 sanitize: {
5423 memtag_heap: true,
5424 },
5425 }
5426 cc_library_static {
5427 name: "foo_safestack",
5428 srcs: ["foo.cc"],
5429 bazel_module: { label: "//foo_safestack" },
5430 sanitize: {
5431 safestack: true,
5432 },
5433 }
5434 cc_library_static {
5435 name: "foo_scudo",
5436 srcs: ["foo.cc"],
5437 bazel_module: { label: "//foo_scudo" },
5438 sanitize: {
5439 scudo: true,
5440 },
5441 }
5442 `
5443 testcases := []struct {
5444 name string
5445 variant string
5446 expectedOutputPaths []string
5447 }{
5448 {
5449 name: "foo_ubsan_minimal",
5450 variant: "android_arm64_armv8-a_static_apex28",
5451 expectedOutputPaths: []string{
5452 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5453 },
5454 },
5455 {
5456 name: "foo",
5457 variant: "android_arm64_armv8-a_static_apex28",
5458 expectedOutputPaths: []string{
5459 "outputbase/execroot/__main__/foo.a",
5460 },
5461 },
5462 {
5463 name: "foo",
5464 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5465 expectedOutputPaths: []string{
5466 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5467 },
5468 },
5469 {
5470 name: "foo",
5471 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5472 expectedOutputPaths: []string{
5473 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5474 },
5475 },
5476 {
5477 name: "foo",
5478 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5479 expectedOutputPaths: []string{
5480 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5481 },
5482 },
5483 {
5484 name: "foo",
5485 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5486 expectedOutputPaths: []string{
5487 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5488 },
5489 },
5490 {
5491 name: "foo",
5492 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5493 expectedOutputPaths: []string{
5494 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5495 },
5496 },
5497 {
5498 name: "foo",
5499 variant: "android_arm64_armv8-a_static_scs_apex28",
5500 expectedOutputPaths: []string{
5501 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5502 },
5503 },
5504 {
5505 name: "foo",
5506 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5507 expectedOutputPaths: []string{
5508 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5509 },
5510 },
5511 {
5512 name: "foo",
5513 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5514 expectedOutputPaths: []string{
5515 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5516 },
5517 },
5518 {
5519 name: "foo_tsan",
5520 variant: "android_arm64_armv8-a_static_apex28",
5521 expectedOutputPaths: []string{
5522 "outputbase/execroot/__main__/foo_tsan.a",
5523 },
5524 },
5525 {
5526 name: "foo_tsan",
5527 variant: "android_arm64_armv8-a_static_tsan_apex28",
5528 expectedOutputPaths: []string{
5529 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5530 },
5531 },
5532 {
5533 name: "foo_cfi",
5534 variant: "android_arm64_armv8-a_static_apex28",
5535 expectedOutputPaths: []string{
5536 "outputbase/execroot/__main__/foo_cfi.a",
5537 },
5538 },
5539 {
5540 name: "foo_cfi",
5541 variant: "android_arm64_armv8-a_static_cfi_apex28",
5542 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005543 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005544 },
5545 },
5546 {
5547 name: "foo_memtag_stack",
5548 variant: "android_arm64_armv8-a_static_apex28",
5549 expectedOutputPaths: []string{
5550 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5551 },
5552 },
5553 {
5554 name: "foo_memtag_heap",
5555 variant: "android_arm64_armv8-a_static_apex28",
5556 expectedOutputPaths: []string{
5557 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5558 },
5559 },
5560 {
5561 name: "foo_safestack",
5562 variant: "android_arm64_armv8-a_static_apex28",
5563 expectedOutputPaths: []string{
5564 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5565 },
5566 },
5567 {
5568 name: "foo_scudo",
5569 variant: "android_arm64_armv8-a_static_apex28",
5570 expectedOutputPaths: []string{
5571 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5572 },
5573 },
5574 }
5575
5576 ctx := android.GroupFixturePreparers(
5577 prepareForCcTest,
5578 prepareForAsanTest,
5579 android.FixtureRegisterWithContext(registerTestMutators),
5580 android.FixtureModifyConfig(func(config android.Config) {
5581 config.BazelContext = android.MockBazelContext{
5582 OutputBaseDir: "outputbase",
5583 LabelToCcInfo: map[string]cquery.CcInfo{
5584 "//foo_ubsan_minimal": {
5585 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5586 },
5587 "//foo": {
5588 RootStaticArchives: []string{"foo.a"},
5589 },
5590 "//foo_tsan": {
5591 RootStaticArchives: []string{"foo_tsan.a"},
5592 },
5593 "//foo_cfi": {
5594 RootStaticArchives: []string{"foo_cfi.a"},
5595 },
5596 "//foo_memtag_stack": {
5597 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5598 },
5599 "//foo_memtag_heap": {
5600 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5601 },
5602 "//foo_safestack": {
5603 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5604 },
5605 "//foo_scudo": {
5606 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5607 },
5608 },
5609 }
5610 }),
5611 ).RunTestWithBp(t, bp).TestContext
5612
5613 for _, tc := range testcases {
5614 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5615 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5616 if err != nil {
5617 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5618 }
5619 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5620 }
5621}