blob: a1d6bdce7121942bfeb4052aa47fd69c12fb073c [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
27 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050028 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070029)
30
Yu Liue4312402023-01-18 09:15:31 -080031func init() {
32 registerTestMutators(android.InitRegistrationContext)
33}
34
Jiyong Park6a43f042017-10-12 23:05:00 +090035func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000036 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090037}
38
Paul Duffin2e6f90e2021-03-22 23:20:25 +000039var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000040 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000041 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
42 variables.DeviceVndkVersion = StringPtr("current")
43 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090044 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000045 }),
46)
47
Yu Liue4312402023-01-18 09:15:31 -080048var ccLibInApex = "cc_lib_in_apex"
49var apexVariationName = "apex28"
50var apexVersion = "28"
51
52func registerTestMutators(ctx android.RegistrationContext) {
53 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
54 ctx.BottomUp("apex", testApexMutator).Parallel()
55 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
56 })
57}
58
59func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
60 if m := ctx.Module(); m.Enabled() {
61 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
62 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) {
63 mixedBuildMod.QueueBazelCall(ctx)
64 }
65 }
66 }
67}
68
69func testApexMutator(mctx android.BottomUpMutatorContext) {
70 modules := mctx.CreateVariations(apexVariationName)
71 apexInfo := android.ApexInfo{
72 ApexVariationName: apexVariationName,
73 MinSdkVersion: android.ApiLevelForTest(apexVersion),
74 }
75 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
76}
77
Paul Duffin8567f222021-03-23 00:02:06 +000078// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000079//
80// See testCc for an explanation as to how to stop using this deprecated method.
81//
82// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080083func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070084 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000085 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000086 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090087}
88
Paul Duffin8567f222021-03-23 00:02:06 +000089// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000090//
Paul Duffin8567f222021-03-23 00:02:06 +000091// 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 +000092// easier to customize the test behavior.
93//
94// 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 +000095// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000096// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
97// that it did not change the test behavior unexpectedly.
98//
99// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800100func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800101 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000102 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000103 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800104}
105
Paul Duffin8567f222021-03-23 00:02:06 +0000106// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000107//
108// See testCc for an explanation as to how to stop using this deprecated method.
109//
110// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800111func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800112 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000113 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900114 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +0800115
Colin Cross98be1bb2019-12-13 20:41:13 -0800116 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800117}
118
Paul Duffin8567f222021-03-23 00:02:06 +0000119// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000120//
121// See testCc for an explanation as to how to stop using this deprecated method.
122//
123// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900124func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
125 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000126 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +0900127 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900128 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +0900129
130 return testCcWithConfig(t, config)
131}
132
Paul Duffin8567f222021-03-23 00:02:06 +0000133// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000134//
135// See testCc for an explanation as to how to stop using this deprecated method.
136//
137// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900138func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800139 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800140
Paul Duffin8567f222021-03-23 00:02:06 +0000141 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000142 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
143 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800144}
145
Paul Duffin8567f222021-03-23 00:02:06 +0000146// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000147//
148// See testCc for an explanation as to how to stop using this deprecated method.
149//
150// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900151func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900152 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000153 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900154 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900155 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900156 testCcErrorWithConfig(t, pattern, config)
157 return
158}
159
Paul Duffin8567f222021-03-23 00:02:06 +0000160// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000161//
162// See testCc for an explanation as to how to stop using this deprecated method.
163//
164// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900165func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900166 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000167 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900168 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
169 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900170 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900171 testCcErrorWithConfig(t, pattern, config)
172 return
173}
174
Logan Chienf3511742017-10-31 18:04:35 +0800175const (
Colin Cross7113d202019-11-20 16:39:12 -0800176 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900177 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
178 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800179 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800180)
181
Paul Duffindb462dd2021-03-21 22:01:55 +0000182// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
183// running it in a fixture that requires all source files to exist.
184func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
185 android.GroupFixturePreparers(
186 PrepareForTestWithCcDefaultModules,
187 android.PrepareForTestDisallowNonExistentPaths,
188 ).RunTest(t)
189}
190
Jiyong Park6a43f042017-10-12 23:05:00 +0900191func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400192 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900193 ctx := testCc(t, `
194 cc_library {
195 name: "libTest",
196 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700197 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800198 nocrt: true,
199 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900200 vendor_available: true,
201 target: {
202 vendor: {
203 srcs: ["bar.c"],
204 },
205 },
206 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900207 `)
208
Logan Chienf3511742017-10-31 18:04:35 +0800209 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900210 var objs []string
211 for _, o := range ld.Inputs {
212 objs = append(objs, o.Base())
213 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800214 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900215 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
216 }
217}
218
Justin Yun7f99ec72021-04-12 13:19:28 +0900219func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
220 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
221 partitionDefined := false
222 checkPartition := func(specific bool, partition string) {
223 if specific {
224 if expected != partition && !partitionDefined {
225 // The variant is installed to the 'partition'
226 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
227 }
228 partitionDefined = true
229 } else {
230 // The variant is not installed to the 'partition'
231 if expected == partition {
232 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
233 }
234 }
235 }
236 socSpecific := func(m *Module) bool {
237 return m.SocSpecific() || m.socSpecificModuleContext()
238 }
239 deviceSpecific := func(m *Module) bool {
240 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
241 }
242 productSpecific := func(m *Module) bool {
243 return m.ProductSpecific() || m.productSpecificModuleContext()
244 }
245 systemExtSpecific := func(m *Module) bool {
246 return m.SystemExtSpecific()
247 }
248 checkPartition(socSpecific(mod), "vendor")
249 checkPartition(deviceSpecific(mod), "odm")
250 checkPartition(productSpecific(mod), "product")
251 checkPartition(systemExtSpecific(mod), "system_ext")
252 if !partitionDefined && expected != "system" {
253 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
254 " but installed to system partition", variant, name, expected)
255 }
256}
257
258func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400259 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900260 t.Helper()
261 ctx := prepareForCcTest.RunTestWithBp(t, `
262 cc_library {
263 name: "libsystem",
264 }
265 cc_library {
266 name: "libsystem_ext",
267 system_ext_specific: true,
268 }
269 cc_library {
270 name: "libproduct",
271 product_specific: true,
272 }
273 cc_library {
274 name: "libvendor",
275 vendor: true,
276 }
277 cc_library {
278 name: "libodm",
279 device_specific: true,
280 }
281 cc_library {
282 name: "liball_available",
283 vendor_available: true,
284 product_available: true,
285 }
286 cc_library {
287 name: "libsystem_ext_all_available",
288 system_ext_specific: true,
289 vendor_available: true,
290 product_available: true,
291 }
292 cc_library {
293 name: "liball_available_odm",
294 odm_available: true,
295 product_available: true,
296 }
297 cc_library {
298 name: "libproduct_vendoravailable",
299 product_specific: true,
300 vendor_available: true,
301 }
302 cc_library {
303 name: "libproduct_odmavailable",
304 product_specific: true,
305 odm_available: true,
306 }
307 `).TestContext
308
309 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
310 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
311 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
312 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
313 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
314
315 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
316 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
317 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
318
319 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
320 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
321 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
322
323 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
324 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
325 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
326
327 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
328 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
329
330 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
331 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
332}
333
Logan Chienf3511742017-10-31 18:04:35 +0800334func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900335 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800336
Logan Chiend3c59a22018-03-29 14:08:15 +0800337 t.Helper()
338
Justin Yun0ecf0b22020-02-28 15:07:59 +0900339 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800340
341 // Check library properties.
342 lib, ok := mod.compiler.(*libraryDecorator)
343 if !ok {
344 t.Errorf("%q must have libraryDecorator", name)
345 } else if lib.baseInstaller.subDir != subDir {
346 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
347 lib.baseInstaller.subDir)
348 }
349
350 // Check VNDK properties.
351 if mod.vndkdep == nil {
352 t.Fatalf("%q must have `vndkdep`", name)
353 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700354 if !mod.IsVndk() {
355 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800356 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400357 if mod.IsVndkSp() != isVndkSp {
358 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800359 }
360
361 // Check VNDK extension properties.
362 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500363 if mod.IsVndkExt() != isVndkExt {
364 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800365 }
366
367 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
368 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
369 }
370}
371
Jooyung Han2216fb12019-11-06 16:46:15 +0900372func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
373 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800374 content := android.ContentFromFileRuleForTests(t, params)
375 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900376 assertArrayString(t, actual, expected)
377}
378
Jooyung Han097087b2019-10-22 19:32:18 +0900379func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
380 t.Helper()
381 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900382 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
383}
384
385func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
386 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800387 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800388 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900389}
390
Logan Chienf3511742017-10-31 18:04:35 +0800391func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400392 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800393 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800394 cc_library {
395 name: "libvndk",
396 vendor_available: true,
397 vndk: {
398 enabled: true,
399 },
400 nocrt: true,
401 }
402
403 cc_library {
404 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900405 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800406 vndk: {
407 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900408 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800409 },
410 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900411 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800412 }
413
414 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900415 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800416 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900417 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800418 vndk: {
419 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900420 },
421 nocrt: true,
422 target: {
423 vendor: {
424 cflags: ["-DTEST"],
425 },
426 product: {
427 cflags: ["-DTEST"],
428 },
429 },
430 }
431
432 cc_library {
433 name: "libvndk_sp",
434 vendor_available: true,
435 vndk: {
436 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800437 support_system_process: true,
438 },
439 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900440 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800441 }
442
443 cc_library {
444 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900445 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800446 vndk: {
447 enabled: true,
448 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900449 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800450 },
451 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900452 target: {
453 vendor: {
454 suffix: "-x",
455 },
456 },
Logan Chienf3511742017-10-31 18:04:35 +0800457 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900458
459 cc_library {
460 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900461 vendor_available: true,
462 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900463 vndk: {
464 enabled: true,
465 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900466 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900467 },
468 nocrt: true,
469 target: {
470 vendor: {
471 suffix: "-x",
472 },
473 product: {
474 suffix: "-x",
475 },
476 },
477 }
478
Justin Yun450ae722021-04-16 19:58:18 +0900479 cc_library {
480 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700481 llndk: {
482 symbol_file: "libllndk.map.txt",
483 export_llndk_headers: ["libllndk_headers"],
484 }
Justin Yun450ae722021-04-16 19:58:18 +0900485 }
486
Justin Yun611e8862021-05-24 18:17:33 +0900487 cc_library {
488 name: "libclang_rt.hwasan-llndk",
489 llndk: {
490 symbol_file: "libclang_rt.hwasan.map.txt",
491 }
492 }
493
Colin Cross627280f2021-04-26 16:53:58 -0700494 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900495 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700496 llndk: {
497 llndk_headers: true,
498 },
Justin Yun450ae722021-04-16 19:58:18 +0900499 export_include_dirs: ["include"],
500 }
501
Colin Crosse4e44bc2020-12-28 13:50:21 -0800502 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900503 name: "llndk.libraries.txt",
504 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800505 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900506 name: "vndkcore.libraries.txt",
507 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800508 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900509 name: "vndksp.libraries.txt",
510 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800511 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900512 name: "vndkprivate.libraries.txt",
513 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800514 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900515 name: "vndkproduct.libraries.txt",
516 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800517 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900518 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800519 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900520 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800521 `
522
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000523 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800524 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900525 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900526 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800527
528 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800529
Jooyung Han261e1582020-10-20 18:54:21 +0900530 // subdir == "" because VNDK libs are not supposed to be installed separately.
531 // They are installed as part of VNDK APEX instead.
532 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
533 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900534 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900535 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
536 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900538
Justin Yun6977e8a2020-10-29 18:24:11 +0900539 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
540 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900541
Inseob Kim1f086e22019-05-09 13:29:15 +0900542 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900543 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000544 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900545
546 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
547 "arm64", "armv8-a"))
548 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
549 "arm", "armv7-a-neon"))
550
551 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
552 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900553 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
554
Inseob Kim1f086e22019-05-09 13:29:15 +0900555 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
556 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900557 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900558
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900559 variant := "android_vendor.29_arm64_armv8-a_shared"
560 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900561
Inseob Kim7f283f42020-06-01 21:53:49 +0900562 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
563
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400564 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
565 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
566 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
567 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
568 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
569 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
570 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
571 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900572
Jooyung Han39edb6c2019-11-06 16:53:07 +0900573 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800574 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
575 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
576 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
577 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
578 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900579
Jooyung Han097087b2019-10-22 19:32:18 +0900580 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
581 "LLNDK: libc.so",
582 "LLNDK: libdl.so",
583 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900584 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900585 "LLNDK: libm.so",
586 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900587 "VNDK-SP: libvndk_sp-x.so",
588 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900589 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900590 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900591 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900592 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900593 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900594 "VNDK-private: libvndk-private.so",
595 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900596 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900597 "VNDK-product: libc++.so",
598 "VNDK-product: libvndk_product.so",
599 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900600 })
Justin Yun611e8862021-05-24 18:17:33 +0900601 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 +0900602 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
603 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
604 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 +0900605 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 +0900606 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
607}
608
Yo Chiangbba545e2020-06-09 16:15:37 +0800609func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400610 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800611 ctx := testCc(t, `
612 cc_library {
613 name: "libvndk_host_supported",
614 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900615 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800616 vndk: {
617 enabled: true,
618 },
619 host_supported: true,
620 }
621
622 cc_library {
623 name: "libvndk_host_supported_but_disabled_on_device",
624 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900625 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800626 vndk: {
627 enabled: true,
628 },
629 host_supported: true,
630 enabled: false,
631 target: {
632 host: {
633 enabled: true,
634 }
635 }
636 }
637
Colin Crosse4e44bc2020-12-28 13:50:21 -0800638 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800639 name: "vndkcore.libraries.txt",
640 }
641 `)
642
643 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
644}
645
Jooyung Han2216fb12019-11-06 16:46:15 +0900646func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400647 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800648 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800649 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900650 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800651 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800652 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000653 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800654 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900655 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800656 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900657
Colin Cross45bce852021-11-11 22:47:54 -0800658 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700659 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900660 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900661}
662
663func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400664 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800665 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900666 cc_library {
667 name: "libvndk",
668 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900669 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900670 vndk: {
671 enabled: true,
672 },
673 nocrt: true,
674 }
675
676 cc_library {
677 name: "libvndk_sp",
678 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900679 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900680 vndk: {
681 enabled: true,
682 support_system_process: true,
683 },
684 nocrt: true,
685 }
686
687 cc_library {
688 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900689 vendor_available: true,
690 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900691 vndk: {
692 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900693 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900694 },
695 nocrt: true,
696 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900697
Colin Crosse4e44bc2020-12-28 13:50:21 -0800698 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900699 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800700 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900701 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800702 `
703
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000704 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800705 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900706 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800707 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
708
709 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
710
711 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900712
Jooyung Han2216fb12019-11-06 16:46:15 +0900713 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900714}
715
Chris Parsons79d66a52020-06-05 17:26:16 -0400716func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400717 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400718 bp := `
719 cc_test_library {
720 name: "test_lib",
721 srcs: ["test_lib.cpp"],
722 gtest: false,
723 }
724
725 cc_test {
726 name: "main_test",
727 data_libs: ["test_lib"],
728 gtest: false,
729 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400730 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400731
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000732 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400733 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900734 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400735 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
736
737 ctx := testCcWithConfig(t, config)
738 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
739 testBinary := module.(*Module).linker.(*testBinary)
740 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
741 if err != nil {
742 t.Errorf("Expected cc_test to produce output files, error: %s", err)
743 return
744 }
745 if len(outputFiles) != 1 {
746 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
747 return
748 }
749 if len(testBinary.dataPaths()) != 1 {
750 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
751 return
752 }
753
754 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400755 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400756
757 if !strings.HasSuffix(outputPath, "/main_test") {
758 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
759 return
760 }
761 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
762 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
763 return
764 }
765}
766
Chris Parsons216e10a2020-07-09 17:12:52 -0400767func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400768 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400769 bp := `
770 cc_test_library {
771 name: "test_lib",
772 srcs: ["test_lib.cpp"],
773 relative_install_path: "foo/bar/baz",
774 gtest: false,
775 }
776
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400777 cc_binary {
778 name: "test_bin",
779 relative_install_path: "foo/bar/baz",
780 compile_multilib: "both",
781 }
782
Chris Parsons216e10a2020-07-09 17:12:52 -0400783 cc_test {
784 name: "main_test",
785 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400786 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400787 gtest: false,
788 }
789 `
790
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000791 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400792 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900793 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400794 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
795
796 ctx := testCcWithConfig(t, config)
797 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
798 testBinary := module.(*Module).linker.(*testBinary)
799 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
800 if err != nil {
801 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
802 }
803 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400804 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400805 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400806 if len(testBinary.dataPaths()) != 2 {
807 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400808 }
809
810 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400811
812 if !strings.HasSuffix(outputPath, "/main_test") {
813 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
814 }
Colin Crossaa255532020-07-03 13:18:24 -0700815 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400816 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
817 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400818 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400819 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400820 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
821 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
822 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
823 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400824}
825
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000826func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400827 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000828 bp := `
829 cc_test {
830 name: "main_test",
831 srcs: ["main_test.cpp"],
832 test_suites: [
833 "suite_1",
834 "suite_2",
835 ],
836 gtest: false,
837 }
838 `
839
840 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
841 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
842
843 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
844 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
845 if len(compatEntries) != 2 {
846 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
847 }
848 if compatEntries[0] != "suite_1" {
849 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
850 " but was '%s'", compatEntries[0])
851 }
852 if compatEntries[1] != "suite_2" {
853 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
854 " but was '%s'", compatEntries[1])
855 }
856}
857
858func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400859 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000860 bp := `
861 cc_test_library {
862 name: "main_test_lib",
863 srcs: ["main_test_lib.cpp"],
864 test_suites: [
865 "suite_1",
866 "suite_2",
867 ],
868 gtest: false,
869 }
870 `
871
872 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
873 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
874
875 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
876 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
877 if len(compatEntries) != 2 {
878 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
879 }
880 if compatEntries[0] != "suite_1" {
881 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
882 " but was '%s'", compatEntries[0])
883 }
884 if compatEntries[1] != "suite_2" {
885 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
886 " but was '%s'", compatEntries[1])
887 }
888}
889
Jooyung Han0302a842019-10-30 18:43:49 +0900890func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400891 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900892 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900893 cc_library {
894 name: "libvndk",
895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900896 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900897 vndk: {
898 enabled: true,
899 },
900 nocrt: true,
901 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900902 cc_library {
903 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900904 vendor_available: true,
905 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900906 vndk: {
907 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900908 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900909 },
910 nocrt: true,
911 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800912
913 cc_library {
914 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700915 llndk: {
916 symbol_file: "libllndk.map.txt",
917 export_llndk_headers: ["libllndk_headers"],
918 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800919 }
920
Colin Cross627280f2021-04-26 16:53:58 -0700921 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800922 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700923 llndk: {
924 symbol_file: "libllndk.map.txt",
925 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800926 export_include_dirs: ["include"],
927 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900928 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900929
930 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
931 "LLNDK: libc.so",
932 "LLNDK: libdl.so",
933 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800934 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900935 "LLNDK: libm.so",
936 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900937 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900938 "VNDK-core: libvndk.so",
939 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900940 "VNDK-private: libvndk-private.so",
941 "VNDK-product: libc++.so",
942 "VNDK-product: libvndk-private.so",
943 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900944 })
Logan Chienf3511742017-10-31 18:04:35 +0800945}
946
Justin Yun63e9ec72020-10-29 16:49:43 +0900947func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400948 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900949 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900950 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900951 cc_library {
952 name: "libvndk",
953 vndk: {
954 enabled: true,
955 },
956 nocrt: true,
957 }
958 `)
959
Justin Yunc0d8c492021-01-07 17:45:31 +0900960 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900961 cc_library {
962 name: "libvndk",
963 product_available: true,
964 vndk: {
965 enabled: true,
966 },
967 nocrt: true,
968 }
969 `)
970
Justin Yun6977e8a2020-10-29 18:24:11 +0900971 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
972 cc_library {
973 name: "libvndkprop",
974 vendor_available: true,
975 product_available: true,
976 vndk: {
977 enabled: true,
978 },
979 nocrt: true,
980 target: {
981 vendor: {
982 cflags: ["-DTEST",],
983 },
984 },
985 }
986 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900987}
988
Logan Chiend3c59a22018-03-29 14:08:15 +0800989func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400990 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800991 // Check whether an error is emitted when a VNDK lib depends on a system lib.
992 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
993 cc_library {
994 name: "libvndk",
995 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900996 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800997 vndk: {
998 enabled: true,
999 },
1000 shared_libs: ["libfwk"], // Cause error
1001 nocrt: true,
1002 }
1003
1004 cc_library {
1005 name: "libfwk",
1006 nocrt: true,
1007 }
1008 `)
1009
1010 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
1011 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1012 cc_library {
1013 name: "libvndk",
1014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001015 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001016 vndk: {
1017 enabled: true,
1018 },
1019 shared_libs: ["libvendor"], // Cause error
1020 nocrt: true,
1021 }
1022
1023 cc_library {
1024 name: "libvendor",
1025 vendor: true,
1026 nocrt: true,
1027 }
1028 `)
1029
1030 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
1031 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1032 cc_library {
1033 name: "libvndk_sp",
1034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001035 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001036 vndk: {
1037 enabled: true,
1038 support_system_process: true,
1039 },
1040 shared_libs: ["libfwk"], // Cause error
1041 nocrt: true,
1042 }
1043
1044 cc_library {
1045 name: "libfwk",
1046 nocrt: true,
1047 }
1048 `)
1049
1050 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1051 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1052 cc_library {
1053 name: "libvndk_sp",
1054 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001055 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001056 vndk: {
1057 enabled: true,
1058 support_system_process: true,
1059 },
1060 shared_libs: ["libvendor"], // Cause error
1061 nocrt: true,
1062 }
1063
1064 cc_library {
1065 name: "libvendor",
1066 vendor: true,
1067 nocrt: true,
1068 }
1069 `)
1070
1071 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1072 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1073 cc_library {
1074 name: "libvndk_sp",
1075 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001076 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001077 vndk: {
1078 enabled: true,
1079 support_system_process: true,
1080 },
1081 shared_libs: ["libvndk"], // Cause error
1082 nocrt: true,
1083 }
1084
1085 cc_library {
1086 name: "libvndk",
1087 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001088 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001089 vndk: {
1090 enabled: true,
1091 },
1092 nocrt: true,
1093 }
1094 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001095
1096 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1097 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1098 cc_library {
1099 name: "libvndk",
1100 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001101 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 vndk: {
1103 enabled: true,
1104 },
1105 shared_libs: ["libnonvndk"],
1106 nocrt: true,
1107 }
1108
1109 cc_library {
1110 name: "libnonvndk",
1111 vendor_available: true,
1112 nocrt: true,
1113 }
1114 `)
1115
1116 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1117 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1118 cc_library {
1119 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001120 vendor_available: true,
1121 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001122 vndk: {
1123 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001124 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001125 },
1126 shared_libs: ["libnonvndk"],
1127 nocrt: true,
1128 }
1129
1130 cc_library {
1131 name: "libnonvndk",
1132 vendor_available: true,
1133 nocrt: true,
1134 }
1135 `)
1136
1137 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1138 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1139 cc_library {
1140 name: "libvndksp",
1141 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001142 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001143 vndk: {
1144 enabled: true,
1145 support_system_process: true,
1146 },
1147 shared_libs: ["libnonvndk"],
1148 nocrt: true,
1149 }
1150
1151 cc_library {
1152 name: "libnonvndk",
1153 vendor_available: true,
1154 nocrt: true,
1155 }
1156 `)
1157
1158 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1159 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1160 cc_library {
1161 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001162 vendor_available: true,
1163 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001164 vndk: {
1165 enabled: true,
1166 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001167 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001168 },
1169 shared_libs: ["libnonvndk"],
1170 nocrt: true,
1171 }
1172
1173 cc_library {
1174 name: "libnonvndk",
1175 vendor_available: true,
1176 nocrt: true,
1177 }
1178 `)
1179}
1180
1181func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001182 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001183 // okay to link : LLNDK -> double_loadable VNDK
1184 testCc(t, `
1185 cc_library {
1186 name: "libllndk",
1187 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001188 llndk: {
1189 symbol_file: "libllndk.map.txt",
1190 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 }
1192
1193 cc_library {
1194 name: "libdoubleloadable",
1195 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001196 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001197 vndk: {
1198 enabled: true,
1199 },
1200 double_loadable: true,
1201 }
1202 `)
1203 // okay to link : LLNDK -> VNDK-SP
1204 testCc(t, `
1205 cc_library {
1206 name: "libllndk",
1207 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001208 llndk: {
1209 symbol_file: "libllndk.map.txt",
1210 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001211 }
1212
1213 cc_library {
1214 name: "libvndksp",
1215 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001216 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001217 vndk: {
1218 enabled: true,
1219 support_system_process: true,
1220 },
1221 }
1222 `)
1223 // okay to link : double_loadable -> double_loadable
1224 testCc(t, `
1225 cc_library {
1226 name: "libdoubleloadable1",
1227 shared_libs: ["libdoubleloadable2"],
1228 vendor_available: true,
1229 double_loadable: true,
1230 }
1231
1232 cc_library {
1233 name: "libdoubleloadable2",
1234 vendor_available: true,
1235 double_loadable: true,
1236 }
1237 `)
1238 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1239 testCc(t, `
1240 cc_library {
1241 name: "libdoubleloadable",
1242 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001243 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001244 vndk: {
1245 enabled: true,
1246 },
1247 double_loadable: true,
1248 shared_libs: ["libnondoubleloadable"],
1249 }
1250
1251 cc_library {
1252 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001253 vendor_available: true,
1254 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001255 vndk: {
1256 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001257 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001258 },
1259 double_loadable: true,
1260 }
1261 `)
1262 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1263 testCc(t, `
1264 cc_library {
1265 name: "libllndk",
1266 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001267 llndk: {
1268 symbol_file: "libllndk.map.txt",
1269 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001270 }
1271
1272 cc_library {
1273 name: "libcoreonly",
1274 shared_libs: ["libvendoravailable"],
1275 }
1276
1277 // indirect dependency of LLNDK
1278 cc_library {
1279 name: "libvendoravailable",
1280 vendor_available: true,
1281 double_loadable: true,
1282 }
1283 `)
1284}
1285
1286func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001287 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001288 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1289 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1290 cc_library {
1291 name: "libllndk",
1292 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001293 llndk: {
1294 symbol_file: "libllndk.map.txt",
1295 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001296 }
1297
1298 cc_library {
1299 name: "libnondoubleloadable",
1300 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001301 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001302 vndk: {
1303 enabled: true,
1304 },
1305 }
1306 `)
1307
1308 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1309 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1310 cc_library {
1311 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001312 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001313 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001314 llndk: {
1315 symbol_file: "libllndk.map.txt",
1316 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001317 }
1318
1319 cc_library {
1320 name: "libnondoubleloadable",
1321 vendor_available: true,
1322 }
1323 `)
1324
Jooyung Hana70f0672019-01-18 15:20:43 +09001325 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1326 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1327 cc_library {
1328 name: "libllndk",
1329 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001330 llndk: {
1331 symbol_file: "libllndk.map.txt",
1332 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001333 }
1334
1335 cc_library {
1336 name: "libcoreonly",
1337 shared_libs: ["libvendoravailable"],
1338 }
1339
1340 // indirect dependency of LLNDK
1341 cc_library {
1342 name: "libvendoravailable",
1343 vendor_available: true,
1344 }
1345 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001346
1347 // The error is not from 'client' but from 'libllndk'
1348 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1349 cc_library {
1350 name: "client",
1351 vendor_available: true,
1352 double_loadable: true,
1353 shared_libs: ["libllndk"],
1354 }
1355 cc_library {
1356 name: "libllndk",
1357 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001358 llndk: {
1359 symbol_file: "libllndk.map.txt",
1360 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001361 }
1362 cc_library {
1363 name: "libnondoubleloadable",
1364 vendor_available: true,
1365 }
1366 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001367}
1368
Jooyung Han479ca172020-10-19 18:51:07 +09001369func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001370 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001371 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1372 cc_library {
1373 name: "libvndksp",
1374 shared_libs: ["libanothervndksp"],
1375 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001376 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001377 vndk: {
1378 enabled: true,
1379 support_system_process: true,
1380 }
1381 }
1382
1383 cc_library {
1384 name: "libllndk",
1385 shared_libs: ["libanothervndksp"],
1386 }
1387
Jooyung Han479ca172020-10-19 18:51:07 +09001388 cc_library {
1389 name: "libanothervndksp",
1390 vendor_available: true,
1391 }
1392 `)
1393}
1394
Logan Chienf3511742017-10-31 18:04:35 +08001395func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001396 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001397 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001398 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001399 cc_library {
1400 name: "libvndk",
1401 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001402 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001403 vndk: {
1404 enabled: true,
1405 },
1406 nocrt: true,
1407 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001408 cc_library {
1409 name: "libvndk2",
1410 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001411 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001412 vndk: {
1413 enabled: true,
1414 },
1415 target: {
1416 vendor: {
1417 suffix: "-suffix",
1418 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001419 product: {
1420 suffix: "-suffix",
1421 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001422 },
1423 nocrt: true,
1424 }
Logan Chienf3511742017-10-31 18:04:35 +08001425
1426 cc_library {
1427 name: "libvndk_ext",
1428 vendor: true,
1429 vndk: {
1430 enabled: true,
1431 extends: "libvndk",
1432 },
1433 nocrt: true,
1434 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001435
1436 cc_library {
1437 name: "libvndk2_ext",
1438 vendor: true,
1439 vndk: {
1440 enabled: true,
1441 extends: "libvndk2",
1442 },
1443 nocrt: true,
1444 }
Logan Chienf3511742017-10-31 18:04:35 +08001445
Justin Yun0ecf0b22020-02-28 15:07:59 +09001446 cc_library {
1447 name: "libvndk_ext_product",
1448 product_specific: true,
1449 vndk: {
1450 enabled: true,
1451 extends: "libvndk",
1452 },
1453 nocrt: true,
1454 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001455
Justin Yun0ecf0b22020-02-28 15:07:59 +09001456 cc_library {
1457 name: "libvndk2_ext_product",
1458 product_specific: true,
1459 vndk: {
1460 enabled: true,
1461 extends: "libvndk2",
1462 },
1463 nocrt: true,
1464 }
1465 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001466 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001467 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1468 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001469 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001470
1471 ctx := testCcWithConfig(t, config)
1472
1473 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1474 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1475
1476 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1477 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1478
1479 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1480 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001481}
1482
Logan Chiend3c59a22018-03-29 14:08:15 +08001483func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001484 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001485 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1486 ctx := testCcNoVndk(t, `
1487 cc_library {
1488 name: "libvndk",
1489 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001490 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001491 vndk: {
1492 enabled: true,
1493 },
1494 nocrt: true,
1495 }
1496
1497 cc_library {
1498 name: "libvndk_ext",
1499 vendor: true,
1500 vndk: {
1501 enabled: true,
1502 extends: "libvndk",
1503 },
1504 nocrt: true,
1505 }
1506 `)
1507
1508 // Ensures that the core variant of "libvndk_ext" can be found.
1509 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1510 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1511 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1512 }
1513}
1514
Justin Yun0ecf0b22020-02-28 15:07:59 +09001515func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001516 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001517 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001518 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001519 cc_library {
1520 name: "libvndk",
1521 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001522 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001523 vndk: {
1524 enabled: true,
1525 },
1526 nocrt: true,
1527 }
1528
1529 cc_library {
1530 name: "libvndk_ext_product",
1531 product_specific: true,
1532 vndk: {
1533 enabled: true,
1534 extends: "libvndk",
1535 },
1536 nocrt: true,
1537 }
1538 `)
1539
1540 // Ensures that the core variant of "libvndk_ext_product" can be found.
1541 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1542 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1543 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1544 }
1545}
1546
Logan Chienf3511742017-10-31 18:04:35 +08001547func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001548 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001549 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001550 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001551 cc_library {
1552 name: "libvndk",
1553 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001554 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001555 vndk: {
1556 enabled: true,
1557 },
1558 nocrt: true,
1559 }
1560
1561 cc_library {
1562 name: "libvndk_ext",
1563 vndk: {
1564 enabled: true,
1565 extends: "libvndk",
1566 },
1567 nocrt: true,
1568 }
1569 `)
1570
1571 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1572 cc_library {
1573 name: "libvndk",
1574 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001575 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001576 vndk: {
1577 enabled: true,
1578 },
1579 nocrt: true,
1580 }
1581
1582 cc_library {
1583 name: "libvndk_ext",
1584 vendor: true,
1585 vndk: {
1586 enabled: true,
1587 },
1588 nocrt: true,
1589 }
1590 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001591
1592 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1593 cc_library {
1594 name: "libvndk",
1595 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001596 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001597 vndk: {
1598 enabled: true,
1599 },
1600 nocrt: true,
1601 }
1602
1603 cc_library {
1604 name: "libvndk_ext_product",
1605 product_specific: true,
1606 vndk: {
1607 enabled: true,
1608 },
1609 nocrt: true,
1610 }
1611 `)
1612
1613 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1614 cc_library {
1615 name: "libvndk",
1616 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001617 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001618 vndk: {
1619 enabled: true,
1620 },
1621 nocrt: true,
1622 }
1623
1624 cc_library {
1625 name: "libvndk_ext_product",
1626 product_specific: true,
1627 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001628 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001629 vndk: {
1630 enabled: true,
1631 extends: "libvndk",
1632 },
1633 nocrt: true,
1634 }
1635 `)
Logan Chienf3511742017-10-31 18:04:35 +08001636}
1637
1638func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001639 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001640 // This test ensures an error is emitted for inconsistent support_system_process.
1641 testCcError(t, "module \".*\" with mismatched support_system_process", `
1642 cc_library {
1643 name: "libvndk",
1644 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001645 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001646 vndk: {
1647 enabled: true,
1648 },
1649 nocrt: true,
1650 }
1651
1652 cc_library {
1653 name: "libvndk_sp_ext",
1654 vendor: true,
1655 vndk: {
1656 enabled: true,
1657 extends: "libvndk",
1658 support_system_process: true,
1659 },
1660 nocrt: true,
1661 }
1662 `)
1663
1664 testCcError(t, "module \".*\" with mismatched support_system_process", `
1665 cc_library {
1666 name: "libvndk_sp",
1667 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001668 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001669 vndk: {
1670 enabled: true,
1671 support_system_process: true,
1672 },
1673 nocrt: true,
1674 }
1675
1676 cc_library {
1677 name: "libvndk_ext",
1678 vendor: true,
1679 vndk: {
1680 enabled: true,
1681 extends: "libvndk_sp",
1682 },
1683 nocrt: true,
1684 }
1685 `)
1686}
1687
1688func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001689 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001690 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001691 // with `private: true`.
1692 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001693 cc_library {
1694 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001695 vendor_available: true,
1696 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001697 vndk: {
1698 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001699 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001700 },
1701 nocrt: true,
1702 }
1703
1704 cc_library {
1705 name: "libvndk_ext",
1706 vendor: true,
1707 vndk: {
1708 enabled: true,
1709 extends: "libvndk",
1710 },
1711 nocrt: true,
1712 }
1713 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001714
Justin Yunfd9e8042020-12-23 18:23:14 +09001715 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716 cc_library {
1717 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001718 vendor_available: true,
1719 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001720 vndk: {
1721 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001722 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001723 },
1724 nocrt: true,
1725 }
1726
1727 cc_library {
1728 name: "libvndk_ext_product",
1729 product_specific: true,
1730 vndk: {
1731 enabled: true,
1732 extends: "libvndk",
1733 },
1734 nocrt: true,
1735 }
1736 `)
Logan Chienf3511742017-10-31 18:04:35 +08001737}
1738
Logan Chiend3c59a22018-03-29 14:08:15 +08001739func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001740 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001741 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001742 testCc(t, `
1743 cc_library {
1744 name: "libvndk",
1745 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001746 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001747 vndk: {
1748 enabled: true,
1749 },
1750 nocrt: true,
1751 }
1752
1753 cc_library {
1754 name: "libvndk_ext",
1755 vendor: true,
1756 vndk: {
1757 enabled: true,
1758 extends: "libvndk",
1759 },
1760 nocrt: true,
1761 }
1762
1763 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001764 name: "libvndk_sp",
1765 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001766 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001767 vndk: {
1768 enabled: true,
1769 support_system_process: true,
1770 },
1771 nocrt: true,
1772 }
1773
1774 cc_library {
1775 name: "libvndk_sp_ext",
1776 vendor: true,
1777 vndk: {
1778 enabled: true,
1779 extends: "libvndk_sp",
1780 support_system_process: true,
1781 },
1782 nocrt: true,
1783 }
1784
1785 cc_library {
1786 name: "libvendor",
1787 vendor: true,
1788 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1789 nocrt: true,
1790 }
1791 `)
1792}
1793
Logan Chiend3c59a22018-03-29 14:08:15 +08001794func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001795 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001796 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001797 testCc(t, `
1798 cc_library {
1799 name: "libvndk",
1800 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001801 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001802 vndk: {
1803 enabled: true,
1804 },
1805 nocrt: true,
1806 }
1807
1808 cc_library {
1809 name: "libvndk_ext",
1810 vendor: true,
1811 vndk: {
1812 enabled: true,
1813 extends: "libvndk",
1814 },
1815 shared_libs: ["libvendor"],
1816 nocrt: true,
1817 }
1818
1819 cc_library {
1820 name: "libvendor",
1821 vendor: true,
1822 nocrt: true,
1823 }
1824 `)
Logan Chienf3511742017-10-31 18:04:35 +08001825
Logan Chiend3c59a22018-03-29 14:08:15 +08001826 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1827 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001828 cc_library {
1829 name: "libvndk_sp",
1830 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001831 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001832 vndk: {
1833 enabled: true,
1834 support_system_process: true,
1835 },
1836 nocrt: true,
1837 }
1838
1839 cc_library {
1840 name: "libvndk_sp_ext",
1841 vendor: true,
1842 vndk: {
1843 enabled: true,
1844 extends: "libvndk_sp",
1845 support_system_process: true,
1846 },
1847 shared_libs: ["libvendor"], // Cause an error
1848 nocrt: true,
1849 }
1850
1851 cc_library {
1852 name: "libvendor",
1853 vendor: true,
1854 nocrt: true,
1855 }
1856 `)
1857}
1858
Justin Yun0ecf0b22020-02-28 15:07:59 +09001859func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001860 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001861 bp := `
1862 cc_library {
1863 name: "libvndk",
1864 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001865 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001866 vndk: {
1867 enabled: true,
1868 },
1869 nocrt: true,
1870 }
1871
1872 cc_library {
1873 name: "libvndk_ext_product",
1874 product_specific: true,
1875 vndk: {
1876 enabled: true,
1877 extends: "libvndk",
1878 },
1879 shared_libs: ["libproduct_for_vndklibs"],
1880 nocrt: true,
1881 }
1882
1883 cc_library {
1884 name: "libvndk_sp",
1885 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001886 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001887 vndk: {
1888 enabled: true,
1889 support_system_process: true,
1890 },
1891 nocrt: true,
1892 }
1893
1894 cc_library {
1895 name: "libvndk_sp_ext_product",
1896 product_specific: true,
1897 vndk: {
1898 enabled: true,
1899 extends: "libvndk_sp",
1900 support_system_process: true,
1901 },
1902 shared_libs: ["libproduct_for_vndklibs"],
1903 nocrt: true,
1904 }
1905
1906 cc_library {
1907 name: "libproduct",
1908 product_specific: true,
1909 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1910 nocrt: true,
1911 }
1912
1913 cc_library {
1914 name: "libproduct_for_vndklibs",
1915 product_specific: true,
1916 nocrt: true,
1917 }
1918 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001919 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001920 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1921 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001922 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001923
1924 testCcWithConfig(t, config)
1925}
1926
Logan Chiend3c59a22018-03-29 14:08:15 +08001927func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001928 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001929 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1930 // library.
1931 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1932 cc_library {
1933 name: "libvndk",
1934 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001935 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001936 vndk: {
1937 enabled: true,
1938 },
1939 nocrt: true,
1940 }
1941
1942 cc_library {
1943 name: "libvndk_sp",
1944 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001945 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001946 vndk: {
1947 enabled: true,
1948 support_system_process: true,
1949 },
1950 nocrt: true,
1951 }
1952
1953 cc_library {
1954 name: "libvndk_sp_ext",
1955 vendor: true,
1956 vndk: {
1957 enabled: true,
1958 extends: "libvndk_sp",
1959 support_system_process: true,
1960 },
1961 shared_libs: ["libvndk"], // Cause an error
1962 nocrt: true,
1963 }
1964 `)
1965
1966 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1967 // library.
1968 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1969 cc_library {
1970 name: "libvndk",
1971 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001972 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001973 vndk: {
1974 enabled: true,
1975 },
1976 nocrt: true,
1977 }
1978
1979 cc_library {
1980 name: "libvndk_ext",
1981 vendor: true,
1982 vndk: {
1983 enabled: true,
1984 extends: "libvndk",
1985 },
1986 nocrt: true,
1987 }
1988
1989 cc_library {
1990 name: "libvndk_sp",
1991 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001992 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001993 vndk: {
1994 enabled: true,
1995 support_system_process: true,
1996 },
1997 nocrt: true,
1998 }
1999
2000 cc_library {
2001 name: "libvndk_sp_ext",
2002 vendor: true,
2003 vndk: {
2004 enabled: true,
2005 extends: "libvndk_sp",
2006 support_system_process: true,
2007 },
2008 shared_libs: ["libvndk_ext"], // Cause an error
2009 nocrt: true,
2010 }
2011 `)
2012}
2013
2014func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002015 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08002016 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2017 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002018 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2019 cc_library {
2020 name: "libvndk",
2021 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002022 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002023 vndk: {
2024 enabled: true,
2025 },
2026 nocrt: true,
2027 }
2028
2029 cc_library {
2030 name: "libvndk_ext",
2031 vendor: true,
2032 vndk: {
2033 enabled: true,
2034 extends: "libvndk",
2035 },
2036 nocrt: true,
2037 }
2038
2039 cc_library {
2040 name: "libvndk2",
2041 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002042 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002043 vndk: {
2044 enabled: true,
2045 },
2046 shared_libs: ["libvndk_ext"],
2047 nocrt: true,
2048 }
2049 `)
2050
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002051 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002052 cc_library {
2053 name: "libvndk",
2054 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002055 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002056 vndk: {
2057 enabled: true,
2058 },
2059 nocrt: true,
2060 }
2061
2062 cc_library {
2063 name: "libvndk_ext",
2064 vendor: true,
2065 vndk: {
2066 enabled: true,
2067 extends: "libvndk",
2068 },
2069 nocrt: true,
2070 }
2071
2072 cc_library {
2073 name: "libvndk2",
2074 vendor_available: true,
2075 vndk: {
2076 enabled: true,
2077 },
2078 target: {
2079 vendor: {
2080 shared_libs: ["libvndk_ext"],
2081 },
2082 },
2083 nocrt: true,
2084 }
2085 `)
2086
2087 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2088 cc_library {
2089 name: "libvndk_sp",
2090 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002091 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002092 vndk: {
2093 enabled: true,
2094 support_system_process: true,
2095 },
2096 nocrt: true,
2097 }
2098
2099 cc_library {
2100 name: "libvndk_sp_ext",
2101 vendor: true,
2102 vndk: {
2103 enabled: true,
2104 extends: "libvndk_sp",
2105 support_system_process: true,
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_sp_2",
2112 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002113 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002114 vndk: {
2115 enabled: true,
2116 support_system_process: true,
2117 },
2118 shared_libs: ["libvndk_sp_ext"],
2119 nocrt: true,
2120 }
2121 `)
2122
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002123 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002124 cc_library {
2125 name: "libvndk_sp",
2126 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002127 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002128 vndk: {
2129 enabled: true,
2130 },
2131 nocrt: true,
2132 }
2133
2134 cc_library {
2135 name: "libvndk_sp_ext",
2136 vendor: true,
2137 vndk: {
2138 enabled: true,
2139 extends: "libvndk_sp",
2140 },
2141 nocrt: true,
2142 }
2143
2144 cc_library {
2145 name: "libvndk_sp2",
2146 vendor_available: true,
2147 vndk: {
2148 enabled: true,
2149 },
2150 target: {
2151 vendor: {
2152 shared_libs: ["libvndk_sp_ext"],
2153 },
2154 },
2155 nocrt: true,
2156 }
2157 `)
2158}
2159
Justin Yun5f7f7e82019-11-18 19:52:14 +09002160func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002161 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002162 bp := `
2163 cc_library {
2164 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002165 llndk: {
2166 symbol_file: "libllndk.map.txt",
2167 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002168 }
2169 cc_library {
2170 name: "libvndk",
2171 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002172 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002173 vndk: {
2174 enabled: true,
2175 },
2176 nocrt: true,
2177 }
2178 cc_library {
2179 name: "libvndk_sp",
2180 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002181 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002182 vndk: {
2183 enabled: true,
2184 support_system_process: true,
2185 },
2186 nocrt: true,
2187 }
2188 cc_library {
2189 name: "libva",
2190 vendor_available: true,
2191 nocrt: true,
2192 }
2193 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002194 name: "libpa",
2195 product_available: true,
2196 nocrt: true,
2197 }
2198 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002199 name: "libboth_available",
2200 vendor_available: true,
2201 product_available: true,
2202 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002203 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002204 target: {
2205 vendor: {
2206 suffix: "-vendor",
2207 },
2208 product: {
2209 suffix: "-product",
2210 },
2211 }
2212 }
2213 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002214 name: "libproduct_va",
2215 product_specific: true,
2216 vendor_available: true,
2217 nocrt: true,
2218 }
2219 cc_library {
2220 name: "libprod",
2221 product_specific: true,
2222 shared_libs: [
2223 "libllndk",
2224 "libvndk",
2225 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002226 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002227 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002228 "libproduct_va",
2229 ],
2230 nocrt: true,
2231 }
2232 cc_library {
2233 name: "libvendor",
2234 vendor: true,
2235 shared_libs: [
2236 "libllndk",
2237 "libvndk",
2238 "libvndk_sp",
2239 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002240 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002241 "libproduct_va",
2242 ],
2243 nocrt: true,
2244 }
2245 `
2246
Paul Duffin8567f222021-03-23 00:02:06 +00002247 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002248
Jooyung Han261e1582020-10-20 18:54:21 +09002249 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2250 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002251
2252 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2253 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2254
2255 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2256 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002257
2258 ensureStringContains := func(t *testing.T, str string, substr string) {
2259 t.Helper()
2260 if !strings.Contains(str, substr) {
2261 t.Errorf("%q is not found in %v", substr, str)
2262 }
2263 }
2264 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2265 t.Helper()
2266 if strings.Contains(str, substr) {
2267 t.Errorf("%q is found in %v", substr, str)
2268 }
2269 }
2270
2271 // _static variant is used since _shared reuses *.o from the static variant
2272 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2273 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2274
2275 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2276 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2277 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2278 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2279
2280 product_cflags := product_static.Rule("cc").Args["cFlags"]
2281 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2282 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2283 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002284}
2285
2286func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002287 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002288 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002289 cc_library {
2290 name: "libprod",
2291 product_specific: true,
2292 shared_libs: [
2293 "libvendor",
2294 ],
2295 nocrt: true,
2296 }
2297 cc_library {
2298 name: "libvendor",
2299 vendor: true,
2300 nocrt: true,
2301 }
2302 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002303 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002304 cc_library {
2305 name: "libprod",
2306 product_specific: true,
2307 shared_libs: [
2308 "libsystem",
2309 ],
2310 nocrt: true,
2311 }
2312 cc_library {
2313 name: "libsystem",
2314 nocrt: true,
2315 }
2316 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002317 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002318 cc_library {
2319 name: "libprod",
2320 product_specific: true,
2321 shared_libs: [
2322 "libva",
2323 ],
2324 nocrt: true,
2325 }
2326 cc_library {
2327 name: "libva",
2328 vendor_available: true,
2329 nocrt: true,
2330 }
2331 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002332 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002333 cc_library {
2334 name: "libprod",
2335 product_specific: true,
2336 shared_libs: [
2337 "libvndk_private",
2338 ],
2339 nocrt: true,
2340 }
2341 cc_library {
2342 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002343 vendor_available: true,
2344 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002345 vndk: {
2346 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002347 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002348 },
2349 nocrt: true,
2350 }
2351 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002352 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002353 cc_library {
2354 name: "libprod",
2355 product_specific: true,
2356 shared_libs: [
2357 "libsystem_ext",
2358 ],
2359 nocrt: true,
2360 }
2361 cc_library {
2362 name: "libsystem_ext",
2363 system_ext_specific: true,
2364 nocrt: true,
2365 }
2366 `)
2367 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2368 cc_library {
2369 name: "libsystem",
2370 shared_libs: [
2371 "libproduct_va",
2372 ],
2373 nocrt: true,
2374 }
2375 cc_library {
2376 name: "libproduct_va",
2377 product_specific: true,
2378 vendor_available: true,
2379 nocrt: true,
2380 }
2381 `)
2382}
2383
Jooyung Han38002912019-05-16 04:01:54 +09002384func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002385 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002386 bp := `
2387 cc_library {
2388 name: "libvndk",
2389 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002390 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002391 vndk: {
2392 enabled: true,
2393 },
2394 }
2395 cc_library {
2396 name: "libvndksp",
2397 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002398 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002399 vndk: {
2400 enabled: true,
2401 support_system_process: true,
2402 },
2403 }
2404 cc_library {
2405 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002406 vendor_available: true,
2407 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002408 vndk: {
2409 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002410 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002411 },
2412 }
2413 cc_library {
2414 name: "libvendor",
2415 vendor: true,
2416 }
2417 cc_library {
2418 name: "libvndkext",
2419 vendor: true,
2420 vndk: {
2421 enabled: true,
2422 extends: "libvndk",
2423 },
2424 }
2425 vndk_prebuilt_shared {
2426 name: "prevndk",
2427 version: "27",
2428 target_arch: "arm",
2429 binder32bit: true,
2430 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002431 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002432 vndk: {
2433 enabled: true,
2434 },
2435 arch: {
2436 arm: {
2437 srcs: ["liba.so"],
2438 },
2439 },
2440 }
2441 cc_library {
2442 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002443 llndk: {
2444 symbol_file: "libllndk.map.txt",
2445 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002446 }
2447 cc_library {
2448 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002449 llndk: {
2450 symbol_file: "libllndkprivate.map.txt",
2451 private: true,
2452 }
Colin Cross78212242021-01-06 14:51:30 -08002453 }
2454
2455 llndk_libraries_txt {
2456 name: "llndk.libraries.txt",
2457 }
2458 vndkcore_libraries_txt {
2459 name: "vndkcore.libraries.txt",
2460 }
2461 vndksp_libraries_txt {
2462 name: "vndksp.libraries.txt",
2463 }
2464 vndkprivate_libraries_txt {
2465 name: "vndkprivate.libraries.txt",
2466 }
2467 vndkcorevariant_libraries_txt {
2468 name: "vndkcorevariant.libraries.txt",
2469 insert_vndk_version: false,
2470 }
2471 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002472
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002473 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002474 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002475 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002476 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002477 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002478
Colin Cross78212242021-01-06 14:51:30 -08002479 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2480 []string{"libvndk.so", "libvndkprivate.so"})
2481 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2482 []string{"libc++.so", "libvndksp.so"})
2483 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2484 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2485 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2486 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002487
Colin Crossfb0c16e2019-11-20 17:12:35 -08002488 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002489
Jooyung Han38002912019-05-16 04:01:54 +09002490 tests := []struct {
2491 variant string
2492 name string
2493 expected string
2494 }{
2495 {vendorVariant, "libvndk", "native:vndk"},
2496 {vendorVariant, "libvndksp", "native:vndk"},
2497 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2498 {vendorVariant, "libvendor", "native:vendor"},
2499 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002500 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002501 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002502 {coreVariant, "libvndk", "native:platform"},
2503 {coreVariant, "libvndkprivate", "native:platform"},
2504 {coreVariant, "libllndk", "native:platform"},
2505 }
2506 for _, test := range tests {
2507 t.Run(test.name, func(t *testing.T) {
2508 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2509 assertString(t, module.makeLinkType, test.expected)
2510 })
2511 }
2512}
2513
Jeff Gaston294356f2017-09-27 17:05:30 -07002514var staticLinkDepOrderTestCases = []struct {
2515 // This is a string representation of a map[moduleName][]moduleDependency .
2516 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002517 inStatic string
2518
2519 // This is a string representation of a map[moduleName][]moduleDependency .
2520 // It models the dependencies declared in an Android.bp file.
2521 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002522
2523 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2524 // The keys of allOrdered specify which modules we would like to check.
2525 // The values of allOrdered specify the expected result (of the transitive closure of all
2526 // dependencies) for each module to test
2527 allOrdered string
2528
2529 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2530 // The keys of outOrdered specify which modules we would like to check.
2531 // The values of outOrdered specify the expected result (of the ordered linker command line)
2532 // for each module to test.
2533 outOrdered string
2534}{
2535 // Simple tests
2536 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002537 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002538 outOrdered: "",
2539 },
2540 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002541 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002542 outOrdered: "a:",
2543 },
2544 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002545 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 outOrdered: "a:b; b:",
2547 },
2548 // Tests of reordering
2549 {
2550 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002551 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 outOrdered: "a:b,c,d; b:d; c:d; d:",
2553 },
2554 {
2555 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002556 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002557 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2558 },
2559 {
2560 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002561 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002562 outOrdered: "a:d,b,e,c; d:b; e:c",
2563 },
2564 {
2565 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002566 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2568 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2569 },
2570 {
2571 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002572 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 -07002573 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2574 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2575 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002576 // shared dependencies
2577 {
2578 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2579 // So, we don't actually have to check that a shared dependency of c will change the order
2580 // of a library that depends statically on b and on c. We only need to check that if c has
2581 // a shared dependency on b, that that shows up in allOrdered.
2582 inShared: "c:b",
2583 allOrdered: "c:b",
2584 outOrdered: "c:",
2585 },
2586 {
2587 // This test doesn't actually include any shared dependencies but it's a reminder of what
2588 // the second phase of the above test would look like
2589 inStatic: "a:b,c; c:b",
2590 allOrdered: "a:c,b; c:b",
2591 outOrdered: "a:c,b; c:b",
2592 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002593 // tiebreakers for when two modules specifying different orderings and there is no dependency
2594 // to dictate an order
2595 {
2596 // 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 -08002597 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002598 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2599 },
2600 {
2601 // 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 -08002602 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 -07002603 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2604 },
2605 // Tests involving duplicate dependencies
2606 {
2607 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002608 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002609 outOrdered: "a:c,b",
2610 },
2611 {
2612 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002613 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002614 outOrdered: "a:d,c,b",
2615 },
2616 // Tests to confirm the nonexistence of infinite loops.
2617 // These cases should never happen, so as long as the test terminates and the
2618 // result is deterministic then that should be fine.
2619 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002620 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002621 outOrdered: "a:a",
2622 },
2623 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002624 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002625 allOrdered: "a:b,c; b:c,a; c:a,b",
2626 outOrdered: "a:b; b:c; c:a",
2627 },
2628 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002629 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002630 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2631 outOrdered: "a:c,b; b:a,c; c:b,a",
2632 },
2633}
2634
2635// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2636func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2637 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2638 strippedText := strings.Replace(text, " ", "", -1)
2639 if len(strippedText) < 1 {
2640 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2641 }
2642 allDeps = make(map[android.Path][]android.Path, 0)
2643
2644 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2645 moduleTexts := strings.Split(strippedText, ";")
2646
2647 outputForModuleName := func(moduleName string) android.Path {
2648 return android.PathForTesting(moduleName)
2649 }
2650
2651 for _, moduleText := range moduleTexts {
2652 // convert from "a:b,c" to ["a", "b,c"]
2653 components := strings.Split(moduleText, ":")
2654 if len(components) != 2 {
2655 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2656 }
2657 moduleName := components[0]
2658 moduleOutput := outputForModuleName(moduleName)
2659 modulesInOrder = append(modulesInOrder, moduleOutput)
2660
2661 depString := components[1]
2662 // convert from "b,c" to ["b", "c"]
2663 depNames := strings.Split(depString, ",")
2664 if len(depString) < 1 {
2665 depNames = []string{}
2666 }
2667 var deps []android.Path
2668 for _, depName := range depNames {
2669 deps = append(deps, outputForModuleName(depName))
2670 }
2671 allDeps[moduleOutput] = deps
2672 }
2673 return modulesInOrder, allDeps
2674}
2675
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002676func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002677 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002678 ctx := testCc(t, `
2679 cc_library {
2680 name: "a",
2681 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002682 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002683 }
2684 cc_library {
2685 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002686 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002687 }
2688 cc_library {
2689 name: "c",
2690 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002691 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002692 }
2693 cc_library {
2694 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002695 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002696 }
2697
2698 `)
2699
Colin Cross7113d202019-11-20 16:39:12 -08002700 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002701 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002702 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2703 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002704 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002705
2706 if !reflect.DeepEqual(actual, expected) {
2707 t.Errorf("staticDeps orderings were not propagated correctly"+
2708 "\nactual: %v"+
2709 "\nexpected: %v",
2710 actual,
2711 expected,
2712 )
2713 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002714}
Jeff Gaston294356f2017-09-27 17:05:30 -07002715
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002716func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002717 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002718 ctx := testCc(t, `
2719 cc_library {
2720 name: "a",
2721 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002722 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002723 }
2724 cc_library {
2725 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002726 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002727 }
2728 cc_library {
2729 name: "c",
2730 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002731 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002732 }
2733
2734 `)
2735
Colin Cross7113d202019-11-20 16:39:12 -08002736 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002737 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002738 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2739 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002740 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002741
2742 if !reflect.DeepEqual(actual, expected) {
2743 t.Errorf("staticDeps orderings did not account for shared libs"+
2744 "\nactual: %v"+
2745 "\nexpected: %v",
2746 actual,
2747 expected,
2748 )
2749 }
2750}
2751
Jooyung Hanb04a4992020-03-13 18:57:35 +09002752func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002753 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002754 if !reflect.DeepEqual(actual, expected) {
2755 t.Errorf(message+
2756 "\nactual: %v"+
2757 "\nexpected: %v",
2758 actual,
2759 expected,
2760 )
2761 }
2762}
2763
Jooyung Han61b66e92020-03-21 14:21:46 +00002764func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002765 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002766 result := prepareForCcTest.RunTestWithBp(t, `
2767 cc_library {
2768 name: "libllndk",
2769 stubs: { versions: ["1", "2"] },
2770 llndk: {
2771 symbol_file: "libllndk.map.txt",
2772 },
2773 export_include_dirs: ["include"],
2774 }
2775
2776 cc_prebuilt_library_shared {
2777 name: "libllndkprebuilt",
2778 stubs: { versions: ["1", "2"] },
2779 llndk: {
2780 symbol_file: "libllndkprebuilt.map.txt",
2781 },
2782 }
2783
2784 cc_library {
2785 name: "libllndk_with_external_headers",
2786 stubs: { versions: ["1", "2"] },
2787 llndk: {
2788 symbol_file: "libllndk.map.txt",
2789 export_llndk_headers: ["libexternal_llndk_headers"],
2790 },
2791 header_libs: ["libexternal_headers"],
2792 export_header_lib_headers: ["libexternal_headers"],
2793 }
2794 cc_library_headers {
2795 name: "libexternal_headers",
2796 export_include_dirs: ["include"],
2797 vendor_available: true,
2798 }
2799 cc_library_headers {
2800 name: "libexternal_llndk_headers",
2801 export_include_dirs: ["include_llndk"],
2802 llndk: {
2803 symbol_file: "libllndk.map.txt",
2804 },
2805 vendor_available: true,
2806 }
2807
2808 cc_library {
2809 name: "libllndk_with_override_headers",
2810 stubs: { versions: ["1", "2"] },
2811 llndk: {
2812 symbol_file: "libllndk.map.txt",
2813 override_export_include_dirs: ["include_llndk"],
2814 },
2815 export_include_dirs: ["include"],
2816 }
2817 `)
2818 actual := result.ModuleVariantsForTests("libllndk")
2819 for i := 0; i < len(actual); i++ {
2820 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2821 actual = append(actual[:i], actual[i+1:]...)
2822 i--
2823 }
2824 }
2825 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002826 "android_vendor.29_arm64_armv8-a_shared_current",
2827 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002828 "android_vendor.29_arm_armv7-a-neon_shared_current",
2829 "android_vendor.29_arm_armv7-a-neon_shared",
2830 }
2831 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2832
2833 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2834 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2835
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002836 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2837 t.Helper()
2838 m := result.ModuleForTests(module, variant).Module()
2839 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2840 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2841 expectedDirs, f.IncludeDirs)
2842 }
2843
2844 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2845 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2846 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2847 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2848 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2849 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2850}
2851
Jiyong Parka46a4d52017-12-14 19:54:34 +09002852func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002853 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002854 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002855 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002856 name: "libllndk_headers",
2857 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002858 llndk: {
2859 llndk_headers: true,
2860 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002861 }
2862 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002863 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002864 llndk: {
2865 symbol_file: "libllndk.map.txt",
2866 export_llndk_headers: ["libllndk_headers"],
2867 }
Colin Cross0477b422020-10-13 18:43:54 -07002868 }
2869
2870 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002871 name: "libvendor",
2872 shared_libs: ["libllndk"],
2873 vendor: true,
2874 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002875 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002876 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002877 }
2878 `)
2879
2880 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002881 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002882 cflags := cc.Args["cFlags"]
2883 if !strings.Contains(cflags, "-Imy_include") {
2884 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2885 }
2886}
2887
Logan Chien43d34c32017-12-20 01:17:32 +08002888func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2889 actual := module.Properties.AndroidMkRuntimeLibs
2890 if !reflect.DeepEqual(actual, expected) {
2891 t.Errorf("incorrect runtime_libs for shared libs"+
2892 "\nactual: %v"+
2893 "\nexpected: %v",
2894 actual,
2895 expected,
2896 )
2897 }
2898}
2899
2900const runtimeLibAndroidBp = `
2901 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002902 name: "liball_available",
2903 vendor_available: true,
2904 product_available: true,
2905 no_libcrt : true,
2906 nocrt : true,
2907 system_shared_libs : [],
2908 }
2909 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002910 name: "libvendor_available1",
2911 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002912 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002913 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002914 nocrt : true,
2915 system_shared_libs : [],
2916 }
2917 cc_library {
2918 name: "libvendor_available2",
2919 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002920 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002921 target: {
2922 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002923 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002924 }
2925 },
Yi Konge7fe9912019-06-02 00:53:50 -07002926 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002927 nocrt : true,
2928 system_shared_libs : [],
2929 }
2930 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002931 name: "libproduct_vendor",
2932 product_specific: true,
2933 vendor_available: true,
2934 no_libcrt : true,
2935 nocrt : true,
2936 system_shared_libs : [],
2937 }
2938 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002939 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002940 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002941 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002942 nocrt : true,
2943 system_shared_libs : [],
2944 }
2945 cc_library {
2946 name: "libvendor1",
2947 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002948 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002949 nocrt : true,
2950 system_shared_libs : [],
2951 }
2952 cc_library {
2953 name: "libvendor2",
2954 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002955 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002956 no_libcrt : true,
2957 nocrt : true,
2958 system_shared_libs : [],
2959 }
2960 cc_library {
2961 name: "libproduct_available1",
2962 product_available: true,
2963 runtime_libs: ["liball_available"],
2964 no_libcrt : true,
2965 nocrt : true,
2966 system_shared_libs : [],
2967 }
2968 cc_library {
2969 name: "libproduct1",
2970 product_specific: true,
2971 no_libcrt : true,
2972 nocrt : true,
2973 system_shared_libs : [],
2974 }
2975 cc_library {
2976 name: "libproduct2",
2977 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002978 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002979 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002980 nocrt : true,
2981 system_shared_libs : [],
2982 }
2983`
2984
2985func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002986 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002987 ctx := testCc(t, runtimeLibAndroidBp)
2988
2989 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002990 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002991
Justin Yun8a2600c2020-12-07 12:44:03 +09002992 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2993 checkRuntimeLibs(t, []string{"liball_available"}, module)
2994
2995 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2996 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002997
2998 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002999 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003000
3001 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3002 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003003 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003004
Justin Yun8a2600c2020-12-07 12:44:03 +09003005 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3006 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003007
3008 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003009 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003010
3011 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3012 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003013 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003014
3015 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3016 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3017
3018 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09003019 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003020}
3021
3022func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003023 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003024 ctx := testCc(t, runtimeLibAndroidBp)
3025
Colin Cross7113d202019-11-20 16:39:12 -08003026 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003027 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3028 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003029
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003030 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003031 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003032 checkRuntimeLibs(t, nil, module)
3033}
3034
3035func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003036 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003037 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3038
3039 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3040
Colin Cross7113d202019-11-20 16:39:12 -08003041 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003042
Justin Yun8a2600c2020-12-07 12:44:03 +09003043 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3044 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003045
3046 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003047 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003048
3049 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003050 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003051}
3052
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003053func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003054 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055 actual := module.Properties.AndroidMkStaticLibs
3056 if !reflect.DeepEqual(actual, expected) {
3057 t.Errorf("incorrect static_libs"+
3058 "\nactual: %v"+
3059 "\nexpected: %v",
3060 actual,
3061 expected,
3062 )
3063 }
3064}
3065
3066const staticLibAndroidBp = `
3067 cc_library {
3068 name: "lib1",
3069 }
3070 cc_library {
3071 name: "lib2",
3072 static_libs: ["lib1"],
3073 }
3074`
3075
3076func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003077 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003078 ctx := testCc(t, staticLibAndroidBp)
3079
3080 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003081 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003082 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003083 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003084
3085 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003086 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003087 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3088 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003089 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003090}
3091
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003092func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3093 bp := `
3094 cc_library {
3095 name: "static_dep",
3096 }
3097 cc_library {
3098 name: "whole_static_dep",
3099 }
3100 cc_library {
3101 name: "shared_dep",
3102 }
3103 cc_library {
3104 name: "lib",
3105 bazel_module: { label: "//:lib" },
3106 static_libs: ["static_dep"],
3107 whole_static_libs: ["whole_static_dep"],
3108 shared_libs: ["shared_dep"],
3109 }
3110 cc_test {
3111 name: "test",
3112 bazel_module: { label: "//:test" },
3113 static_libs: ["static_dep"],
3114 whole_static_libs: ["whole_static_dep"],
3115 shared_libs: ["shared_dep"],
3116 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04003117 sanitize: {
3118 // cc_test modules default to memtag_heap: true,
3119 // but this adds extra dependencies that we don't care about
3120 never: true,
3121 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003122 }
3123 cc_binary {
3124 name: "binary",
3125 bazel_module: { label: "//:binary" },
3126 static_libs: ["static_dep"],
3127 whole_static_libs: ["whole_static_dep"],
3128 shared_libs: ["shared_dep"],
3129 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003130 cc_library_headers {
3131 name: "lib_headers",
3132 bazel_module: { label: "//:lib_headers" },
3133 static_libs: ["static_dep"],
3134 whole_static_libs: ["whole_static_dep"],
3135 shared_libs: ["shared_dep"],
3136 }
3137 cc_prebuilt_library {
3138 name: "lib_prebuilt",
3139 bazel_module: { label: "//:lib_prebuilt" },
3140 static_libs: ["static_dep"],
3141 whole_static_libs: ["whole_static_dep"],
3142 shared_libs: ["shared_dep"],
3143 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003144 `
3145
3146 testCases := []struct {
3147 name string
3148 moduleName string
3149 variant string
3150 androidMkInfo cquery.CcAndroidMkInfo
3151 }{
3152 {
3153 name: "shared lib",
3154 moduleName: "lib",
3155 variant: "android_arm64_armv8-a_shared",
3156 androidMkInfo: cquery.CcAndroidMkInfo{
3157 LocalStaticLibs: []string{"static_dep"},
3158 LocalWholeStaticLibs: []string{"whole_static_dep"},
3159 LocalSharedLibs: []string{"shared_dep"},
3160 },
3161 },
3162 {
3163 name: "static lib",
3164 moduleName: "lib",
3165 variant: "android_arm64_armv8-a_static",
3166 androidMkInfo: cquery.CcAndroidMkInfo{
3167 LocalStaticLibs: []string{"static_dep"},
3168 LocalWholeStaticLibs: []string{"whole_static_dep"},
3169 LocalSharedLibs: []string{"shared_dep"},
3170 },
3171 },
3172 {
3173 name: "cc_test arm64",
3174 moduleName: "test",
3175 variant: "android_arm64_armv8-a",
3176 androidMkInfo: cquery.CcAndroidMkInfo{
3177 LocalStaticLibs: []string{"static_dep"},
3178 LocalWholeStaticLibs: []string{"whole_static_dep"},
3179 LocalSharedLibs: []string{"shared_dep"},
3180 },
3181 },
3182 {
3183 name: "cc_test arm",
3184 moduleName: "test",
3185 variant: "android_arm_armv7-a-neon",
3186 androidMkInfo: cquery.CcAndroidMkInfo{
3187 LocalStaticLibs: []string{"static_dep"},
3188 LocalWholeStaticLibs: []string{"whole_static_dep"},
3189 LocalSharedLibs: []string{"shared_dep"},
3190 },
3191 },
3192 {
3193 name: "cc_binary",
3194 moduleName: "binary",
3195 variant: "android_arm64_armv8-a",
3196 androidMkInfo: cquery.CcAndroidMkInfo{
3197 LocalStaticLibs: []string{"static_dep"},
3198 LocalWholeStaticLibs: []string{"whole_static_dep"},
3199 LocalSharedLibs: []string{"shared_dep"},
3200 },
3201 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003202 {
3203 name: "cc_library_headers",
3204 moduleName: "lib_headers",
3205 variant: "android_arm64_armv8-a",
3206 androidMkInfo: cquery.CcAndroidMkInfo{
3207 LocalStaticLibs: []string{"static_dep"},
3208 LocalWholeStaticLibs: []string{"whole_static_dep"},
3209 LocalSharedLibs: []string{"shared_dep"},
3210 },
3211 },
3212 {
3213 name: "prebuilt lib static",
3214 moduleName: "lib_prebuilt",
3215 variant: "android_arm64_armv8-a_static",
3216 androidMkInfo: cquery.CcAndroidMkInfo{
3217 LocalStaticLibs: []string{"static_dep"},
3218 LocalWholeStaticLibs: []string{"whole_static_dep"},
3219 LocalSharedLibs: []string{"shared_dep"},
3220 },
3221 },
3222 {
3223 name: "prebuilt lib shared",
3224 moduleName: "lib_prebuilt",
3225 variant: "android_arm64_armv8-a_shared",
3226 androidMkInfo: cquery.CcAndroidMkInfo{
3227 LocalStaticLibs: []string{"static_dep"},
3228 LocalWholeStaticLibs: []string{"whole_static_dep"},
3229 LocalSharedLibs: []string{"shared_dep"},
3230 },
3231 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003232 }
3233
3234 outputBaseDir := "out/bazel"
3235 for _, tc := range testCases {
3236 t.Run(tc.name, func(t *testing.T) {
3237 result := android.GroupFixturePreparers(
3238 prepareForCcTest,
3239 android.FixtureModifyConfig(func(config android.Config) {
3240 config.BazelContext = android.MockBazelContext{
3241 OutputBaseDir: outputBaseDir,
3242 LabelToCcInfo: map[string]cquery.CcInfo{
3243 "//:lib": cquery.CcInfo{
3244 CcAndroidMkInfo: tc.androidMkInfo,
3245 RootDynamicLibraries: []string{""},
3246 },
3247 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3248 CcAndroidMkInfo: tc.androidMkInfo,
3249 RootStaticArchives: []string{""},
3250 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003251 "//:lib_headers": cquery.CcInfo{
3252 CcAndroidMkInfo: tc.androidMkInfo,
3253 OutputFiles: []string{""},
3254 },
3255 "//:lib_prebuilt": cquery.CcInfo{
3256 CcAndroidMkInfo: tc.androidMkInfo,
3257 },
3258 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3259 CcAndroidMkInfo: tc.androidMkInfo,
3260 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003261 },
3262 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3263 "//:test": cquery.CcUnstrippedInfo{
3264 CcAndroidMkInfo: tc.androidMkInfo,
3265 },
3266 "//:binary": cquery.CcUnstrippedInfo{
3267 CcAndroidMkInfo: tc.androidMkInfo,
3268 },
3269 },
3270 }
3271 }),
3272 ).RunTestWithBp(t, bp)
3273 ctx := result.TestContext
3274
3275 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3276 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003277 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3278 t.Errorf("incorrect static_libs"+
3279 "\nactual: %v"+
3280 "\nexpected: %v",
3281 module.Properties.AndroidMkStaticLibs,
3282 tc.androidMkInfo.LocalStaticLibs,
3283 )
3284 }
3285 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3286 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3287 tc.androidMkInfo.LocalStaticLibs,
3288 )
3289 if staticDepsDiffer {
3290 t.Errorf(
3291 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3292 tc.androidMkInfo.LocalStaticLibs,
3293 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3294 missingStaticDeps,
3295 additionalStaticDeps,
3296 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003297 }
3298
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003299 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3300 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3301 tc.androidMkInfo.LocalWholeStaticLibs,
3302 module.Properties.AndroidMkWholeStaticLibs,
3303 )
3304 }
3305 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3306 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3307 tc.androidMkInfo.LocalWholeStaticLibs,
3308 )
3309 if wholeStaticDepsDiffer {
3310 t.Errorf(
3311 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3312 tc.androidMkInfo.LocalWholeStaticLibs,
3313 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3314 missingWholeStaticDeps,
3315 additionalWholeStaticDeps,
3316 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003317 }
3318
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003319 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3320 t.Errorf("incorrect shared_libs"+
3321 "\nactual: %v"+
3322 "\nexpected: %v",
3323 module.Properties.AndroidMkSharedLibs,
3324 tc.androidMkInfo.LocalSharedLibs,
3325 )
3326 }
3327 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3328 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3329 tc.androidMkInfo.LocalSharedLibs,
3330 )
3331 if sharedDepsDiffer {
3332 t.Errorf(
3333 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3334 tc.androidMkInfo.LocalSharedLibs,
3335 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3336 missingSharedDeps,
3337 additionalSharedDeps,
3338 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003339 }
3340 })
3341 }
3342}
3343
Jiyong Parkd08b6972017-09-26 10:50:54 +09003344var compilerFlagsTestCases = []struct {
3345 in string
3346 out bool
3347}{
3348 {
3349 in: "a",
3350 out: false,
3351 },
3352 {
3353 in: "-a",
3354 out: true,
3355 },
3356 {
3357 in: "-Ipath/to/something",
3358 out: false,
3359 },
3360 {
3361 in: "-isystempath/to/something",
3362 out: false,
3363 },
3364 {
3365 in: "--coverage",
3366 out: false,
3367 },
3368 {
3369 in: "-include a/b",
3370 out: true,
3371 },
3372 {
3373 in: "-include a/b c/d",
3374 out: false,
3375 },
3376 {
3377 in: "-DMACRO",
3378 out: true,
3379 },
3380 {
3381 in: "-DMAC RO",
3382 out: false,
3383 },
3384 {
3385 in: "-a -b",
3386 out: false,
3387 },
3388 {
3389 in: "-DMACRO=definition",
3390 out: true,
3391 },
3392 {
3393 in: "-DMACRO=defi nition",
3394 out: true, // TODO(jiyong): this should be false
3395 },
3396 {
3397 in: "-DMACRO(x)=x + 1",
3398 out: true,
3399 },
3400 {
3401 in: "-DMACRO=\"defi nition\"",
3402 out: true,
3403 },
3404}
3405
3406type mockContext struct {
3407 BaseModuleContext
3408 result bool
3409}
3410
3411func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3412 // CheckBadCompilerFlags calls this function when the flag should be rejected
3413 ctx.result = false
3414}
3415
3416func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003417 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003418 for _, testCase := range compilerFlagsTestCases {
3419 ctx := &mockContext{result: true}
3420 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3421 if ctx.result != testCase.out {
3422 t.Errorf("incorrect output:")
3423 t.Errorf(" input: %#v", testCase.in)
3424 t.Errorf(" expected: %#v", testCase.out)
3425 t.Errorf(" got: %#v", ctx.result)
3426 }
3427 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003428}
Jiyong Park374510b2018-03-19 18:23:01 +09003429
Jiyong Park37b25202018-07-11 10:49:27 +09003430func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003431 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003432 ctx := testCc(t, `
3433 cc_library_shared {
3434 name: "librecovery",
3435 recovery: true,
3436 }
3437 cc_library_shared {
3438 name: "librecovery32",
3439 recovery: true,
3440 compile_multilib:"32",
3441 }
Jiyong Park5baac542018-08-28 09:55:37 +09003442 cc_library_shared {
3443 name: "libHalInRecovery",
3444 recovery_available: true,
3445 vendor: true,
3446 }
Jiyong Park37b25202018-07-11 10:49:27 +09003447 `)
3448
3449 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003450 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003451 if len(variants) != 1 || !android.InList(arm64, variants) {
3452 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3453 }
3454
3455 variants = ctx.ModuleVariantsForTests("librecovery32")
3456 if android.InList(arm64, variants) {
3457 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3458 }
Jiyong Park5baac542018-08-28 09:55:37 +09003459
3460 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3461 if !recoveryModule.Platform() {
3462 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3463 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003464}
Jiyong Park5baac542018-08-28 09:55:37 +09003465
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003466func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003467 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003468 bp := `
3469 cc_prebuilt_test_library_shared {
3470 name: "test_lib",
3471 relative_install_path: "foo/bar/baz",
3472 srcs: ["srcpath/dontusethispath/baz.so"],
3473 }
3474
3475 cc_test {
3476 name: "main_test",
3477 data_libs: ["test_lib"],
3478 gtest: false,
3479 }
3480 `
3481
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003482 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003483 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003484 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003485 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3486
3487 ctx := testCcWithConfig(t, config)
3488 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3489 testBinary := module.(*Module).linker.(*testBinary)
3490 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3491 if err != nil {
3492 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3493 }
3494 if len(outputFiles) != 1 {
3495 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3496 }
3497 if len(testBinary.dataPaths()) != 1 {
3498 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3499 }
3500
3501 outputPath := outputFiles[0].String()
3502
3503 if !strings.HasSuffix(outputPath, "/main_test") {
3504 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3505 }
Colin Crossaa255532020-07-03 13:18:24 -07003506 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003507 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3508 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3509 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3510 }
3511}
3512
Jiyong Park7ed9de32018-10-15 22:25:07 +09003513func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003514 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003515 ctx := testCc(t, `
3516 cc_library_shared {
3517 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003518 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003519 stubs: {
3520 symbol_file: "foo.map.txt",
3521 versions: ["1", "2", "3"],
3522 },
3523 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003524
Jiyong Park7ed9de32018-10-15 22:25:07 +09003525 cc_library_shared {
3526 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003527 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003528 shared_libs: ["libFoo#1"],
3529 }`)
3530
3531 variants := ctx.ModuleVariantsForTests("libFoo")
3532 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003533 "android_arm64_armv8-a_shared",
3534 "android_arm64_armv8-a_shared_1",
3535 "android_arm64_armv8-a_shared_2",
3536 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003537 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003538 "android_arm_armv7-a-neon_shared",
3539 "android_arm_armv7-a-neon_shared_1",
3540 "android_arm_armv7-a-neon_shared_2",
3541 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003542 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003543 }
3544 variantsMismatch := false
3545 if len(variants) != len(expectedVariants) {
3546 variantsMismatch = true
3547 } else {
3548 for _, v := range expectedVariants {
3549 if !inList(v, variants) {
3550 variantsMismatch = false
3551 }
3552 }
3553 }
3554 if variantsMismatch {
3555 t.Errorf("variants of libFoo expected:\n")
3556 for _, v := range expectedVariants {
3557 t.Errorf("%q\n", v)
3558 }
3559 t.Errorf(", but got:\n")
3560 for _, v := range variants {
3561 t.Errorf("%q\n", v)
3562 }
3563 }
3564
Colin Cross7113d202019-11-20 16:39:12 -08003565 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003566 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003567 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003568 if !strings.Contains(libFlags, libFoo1StubPath) {
3569 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3570 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003571
Colin Cross7113d202019-11-20 16:39:12 -08003572 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003573 cFlags := libBarCompileRule.Args["cFlags"]
3574 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3575 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3576 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3577 }
Jiyong Park37b25202018-07-11 10:49:27 +09003578}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003579
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003580func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3581 t.Parallel()
3582 ctx := testCc(t, `
3583 cc_library_shared {
3584 name: "libFoo",
3585 srcs: ["foo.c"],
3586 stubs: {
3587 symbol_file: "foo.map.txt",
3588 versions: ["current"],
3589 },
3590 apex_available: ["bar", "a1"],
3591 }
3592
3593 cc_library_shared {
3594 name: "libBar",
3595 srcs: ["bar.c"],
3596 shared_libs: ["libFoo"],
3597 apex_available: ["a1"],
3598 }
3599
3600 cc_library_shared {
3601 name: "libA1",
3602 srcs: ["a1.c"],
3603 shared_libs: ["libFoo"],
3604 apex_available: ["a1"],
3605 }
3606
3607 cc_library_shared {
3608 name: "libBarA1",
3609 srcs: ["bara1.c"],
3610 shared_libs: ["libFoo"],
3611 apex_available: ["bar", "a1"],
3612 }
3613
3614 cc_library_shared {
3615 name: "libAnyApex",
3616 srcs: ["anyApex.c"],
3617 shared_libs: ["libFoo"],
3618 apex_available: ["//apex_available:anyapex"],
3619 }
3620
3621 cc_library_shared {
3622 name: "libBaz",
3623 srcs: ["baz.c"],
3624 shared_libs: ["libFoo"],
3625 apex_available: ["baz"],
3626 }
3627
3628 cc_library_shared {
3629 name: "libQux",
3630 srcs: ["qux.c"],
3631 shared_libs: ["libFoo"],
3632 apex_available: ["qux", "bar"],
3633 }`)
3634
3635 variants := ctx.ModuleVariantsForTests("libFoo")
3636 expectedVariants := []string{
3637 "android_arm64_armv8-a_shared",
3638 "android_arm64_armv8-a_shared_current",
3639 "android_arm_armv7-a-neon_shared",
3640 "android_arm_armv7-a-neon_shared_current",
3641 }
3642 variantsMismatch := false
3643 if len(variants) != len(expectedVariants) {
3644 variantsMismatch = true
3645 } else {
3646 for _, v := range expectedVariants {
3647 if !inList(v, variants) {
3648 variantsMismatch = false
3649 }
3650 }
3651 }
3652 if variantsMismatch {
3653 t.Errorf("variants of libFoo expected:\n")
3654 for _, v := range expectedVariants {
3655 t.Errorf("%q\n", v)
3656 }
3657 t.Errorf(", but got:\n")
3658 for _, v := range variants {
3659 t.Errorf("%q\n", v)
3660 }
3661 }
3662
3663 linkAgainstFoo := []string{"libBarA1"}
3664 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3665
3666 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3667 for _, lib := range linkAgainstFoo {
3668 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3669 libFlags := libLinkRule.Args["libFlags"]
3670 if !strings.Contains(libFlags, libFooPath) {
3671 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3672 }
3673 }
3674
3675 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3676 for _, lib := range linkAgainstFooStubs {
3677 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3678 libFlags := libLinkRule.Args["libFlags"]
3679 if !strings.Contains(libFlags, libFooStubPath) {
3680 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3681 }
3682 }
3683}
3684
Sam Delmerico75dbca22023-04-20 13:13:25 +00003685func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003686 t.Parallel()
3687 bp := `
3688 cc_library_shared {
3689 name: "libFoo",
3690 bazel_module: { label: "//:libFoo" },
3691 srcs: ["foo.c"],
3692 stubs: {
3693 symbol_file: "foo.map.txt",
3694 versions: ["current"],
3695 },
3696 apex_available: ["bar", "a1"],
3697 }
3698
3699 cc_library_shared {
3700 name: "libBar",
3701 srcs: ["bar.c"],
3702 shared_libs: ["libFoo"],
3703 apex_available: ["a1"],
3704 }
3705
3706 cc_library_shared {
3707 name: "libA1",
3708 srcs: ["a1.c"],
3709 shared_libs: ["libFoo"],
3710 apex_available: ["a1"],
3711 }
3712
3713 cc_library_shared {
3714 name: "libBarA1",
3715 srcs: ["bara1.c"],
3716 shared_libs: ["libFoo"],
3717 apex_available: ["bar", "a1"],
3718 }
3719
3720 cc_library_shared {
3721 name: "libAnyApex",
3722 srcs: ["anyApex.c"],
3723 shared_libs: ["libFoo"],
3724 apex_available: ["//apex_available:anyapex"],
3725 }
3726
3727 cc_library_shared {
3728 name: "libBaz",
3729 srcs: ["baz.c"],
3730 shared_libs: ["libFoo"],
3731 apex_available: ["baz"],
3732 }
3733
3734 cc_library_shared {
3735 name: "libQux",
3736 srcs: ["qux.c"],
3737 shared_libs: ["libFoo"],
3738 apex_available: ["qux", "bar"],
3739 }`
3740
3741 result := android.GroupFixturePreparers(
3742 prepareForCcTest,
3743 android.FixtureModifyConfig(func(config android.Config) {
3744 config.BazelContext = android.MockBazelContext{
3745 OutputBaseDir: "out/bazel",
3746 LabelToCcInfo: map[string]cquery.CcInfo{
3747 "//:libFoo": {
3748 RootDynamicLibraries: []string{"libFoo.so"},
3749 },
3750 "//:libFoo_stub_libs-current": {
3751 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3752 },
3753 },
3754 }
3755 }),
3756 ).RunTestWithBp(t, bp)
3757 ctx := result.TestContext
3758
3759 variants := ctx.ModuleVariantsForTests("libFoo")
3760 expectedVariants := []string{
3761 "android_arm64_armv8-a_shared",
3762 "android_arm64_armv8-a_shared_current",
3763 "android_arm_armv7-a-neon_shared",
3764 "android_arm_armv7-a-neon_shared_current",
3765 }
3766 variantsMismatch := false
3767 if len(variants) != len(expectedVariants) {
3768 variantsMismatch = true
3769 } else {
3770 for _, v := range expectedVariants {
3771 if !inList(v, variants) {
3772 variantsMismatch = false
3773 }
3774 }
3775 }
3776 if variantsMismatch {
3777 t.Errorf("variants of libFoo expected:\n")
3778 for _, v := range expectedVariants {
3779 t.Errorf("%q\n", v)
3780 }
3781 t.Errorf(", but got:\n")
3782 for _, v := range variants {
3783 t.Errorf("%q\n", v)
3784 }
3785 }
3786
3787 linkAgainstFoo := []string{"libBarA1"}
3788 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3789
3790 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3791 for _, lib := range linkAgainstFoo {
3792 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3793 libFlags := libLinkRule.Args["libFlags"]
3794 if !strings.Contains(libFlags, libFooPath) {
3795 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3796 }
3797 }
3798
3799 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3800 for _, lib := range linkAgainstFooStubs {
3801 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3802 libFlags := libLinkRule.Args["libFlags"]
3803 if !strings.Contains(libFlags, libFooStubPath) {
3804 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3805 }
3806 }
3807}
3808
Jooyung Hanb04a4992020-03-13 18:57:35 +09003809func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003810 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003811 for _, tc := range []struct{ moduleName, expected string }{
3812 {"libc", "__LIBC_API__"},
3813 {"libfoo", "__LIBFOO_API__"},
3814 {"libfoo@1", "__LIBFOO_1_API__"},
3815 {"libfoo-v1", "__LIBFOO_V1_API__"},
3816 {"libfoo.v1", "__LIBFOO_V1_API__"},
3817 } {
3818 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3819 }
3820}
3821
Liz Kammer83cf81b2022-09-22 08:24:20 -04003822func pathsToBase(paths android.Paths) []string {
3823 var ret []string
3824 for _, p := range paths {
3825 ret = append(ret, p.Base())
3826 }
3827 return ret
3828}
3829
3830func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003831 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003832 ctx := testCc(t, `
3833 cc_library_static {
3834 name: "foo",
3835 srcs: ["foo.c"],
3836 }
3837
3838 cc_library_static {
3839 name: "bar",
3840 srcs: ["bar.c"],
3841 }
3842
3843 cc_library_shared {
3844 name: "qux",
3845 srcs: ["qux.c"],
3846 }
3847
3848 cc_library_static {
3849 name: "baz",
3850 srcs: ["baz.c"],
3851 static_libs: ["foo"],
3852 shared_libs: ["qux"],
3853 whole_static_libs: ["bar"],
3854 }`)
3855
3856 variant := "android_arm64_armv8-a_static"
3857 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3858
3859 // For static libraries, the object files of a whole static dep are included in the archive
3860 // directly
3861 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3862 t.Errorf("Expected input objects %q, got %q", w, g)
3863 }
3864
3865 // non whole static dependencies are not linked into the archive
3866 if len(arRule.Implicits) > 0 {
3867 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3868 }
3869}
3870
3871func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003872 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003873 ctx := testCc(t, `
3874 cc_library_static {
3875 name: "foo",
3876 srcs: ["foo.c"],
3877 }
3878
3879 cc_library_static {
3880 name: "bar",
3881 srcs: ["bar.c"],
3882 }
3883
3884 cc_library_shared {
3885 name: "qux",
3886 srcs: ["qux.c"],
3887 }
3888
3889 cc_library_shared {
3890 name: "baz",
3891 srcs: ["baz.c"],
3892 static_libs: ["foo"],
3893 shared_libs: ["qux"],
3894 whole_static_libs: ["bar"],
3895 }`)
3896
3897 variant := "android_arm64_armv8-a_shared"
3898 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3899 libFlags := linkRule.Args["libFlags"]
3900 // When dynamically linking, we expect static dependencies to be found on the command line
3901 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3902 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3903 }
3904 // When dynamically linking, we expect whole static dependencies to be found on the command line
3905 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3906 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3907 }
3908
3909 // When dynamically linking, we expect shared dependencies to be found on the command line
3910 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3911 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3912 }
3913
3914 // We should only have the objects from the shared library srcs, not the whole static dependencies
3915 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3916 t.Errorf("Expected input objects %q, got %q", w, g)
3917 }
3918}
3919
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003920func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003921 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003922 ctx := testCc(t, `
3923 cc_binary {
3924 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003925 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003926 static_executable: true,
3927 }`)
3928
Colin Cross7113d202019-11-20 16:39:12 -08003929 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003930 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3931 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003932 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003933 for _, lib := range systemStaticLibs {
3934 if !strings.Contains(libFlags, lib) {
3935 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3936 }
3937 }
3938 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3939 for _, lib := range systemSharedLibs {
3940 if strings.Contains(libFlags, lib) {
3941 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3942 }
3943 }
3944}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003945
3946func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003947 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003948 ctx := testCc(t, `
3949 cc_binary {
3950 name: "mybin",
3951 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003952 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003953 static_executable: true,
3954 stl: "none",
3955 }
3956
3957 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003958 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003959 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003960 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003961 stl: "none",
3962 }
3963
3964 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003965 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003966 srcs: ["foo.c"],
3967 stl: "none",
3968 stubs: {
3969 versions: ["1"],
3970 },
3971 }`)
3972
Colin Cross0de8a1e2020-09-18 14:15:30 -07003973 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3974 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003975 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003976
3977 if !reflect.DeepEqual(actual, expected) {
3978 t.Errorf("staticDeps orderings were not propagated correctly"+
3979 "\nactual: %v"+
3980 "\nexpected: %v",
3981 actual,
3982 expected,
3983 )
3984 }
3985}
Jooyung Han38002912019-05-16 04:01:54 +09003986
Jooyung Hand48f3c32019-08-23 11:18:57 +09003987func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003988 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003989 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3990 cc_library {
3991 name: "libA",
3992 srcs: ["foo.c"],
3993 shared_libs: ["libB"],
3994 stl: "none",
3995 }
3996
3997 cc_library {
3998 name: "libB",
3999 srcs: ["foo.c"],
4000 enabled: false,
4001 stl: "none",
4002 }
4003 `)
4004}
4005
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004006func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
4007 bp := `
4008 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00004009 name: "test_afl_fuzz_target",
4010 srcs: ["foo.c"],
4011 host_supported: true,
4012 static_libs: [
4013 "afl_fuzz_static_lib",
4014 ],
4015 shared_libs: [
4016 "afl_fuzz_shared_lib",
4017 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004018 fuzzing_frameworks: {
4019 afl: true,
4020 libfuzzer: false,
4021 },
Cory Barkera1da26f2022-06-07 20:12:06 +00004022 }
4023 cc_library {
4024 name: "afl_fuzz_static_lib",
4025 host_supported: true,
4026 srcs: ["static_file.c"],
4027 }
4028 cc_library {
4029 name: "libfuzzer_only_static_lib",
4030 host_supported: true,
4031 srcs: ["static_file.c"],
4032 }
4033 cc_library {
4034 name: "afl_fuzz_shared_lib",
4035 host_supported: true,
4036 srcs: ["shared_file.c"],
4037 static_libs: [
4038 "second_static_lib",
4039 ],
4040 }
4041 cc_library_headers {
4042 name: "libafl_headers",
4043 vendor_available: true,
4044 host_supported: true,
4045 export_include_dirs: [
4046 "include",
4047 "instrumentation",
4048 ],
4049 }
4050 cc_object {
4051 name: "afl-compiler-rt",
4052 vendor_available: true,
4053 host_supported: true,
4054 cflags: [
4055 "-fPIC",
4056 ],
4057 srcs: [
4058 "instrumentation/afl-compiler-rt.o.c",
4059 ],
4060 }
4061 cc_library {
4062 name: "second_static_lib",
4063 host_supported: true,
4064 srcs: ["second_file.c"],
4065 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004066 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00004067 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004068 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00004069 srcs: [
4070 "aflpp_driver.c",
4071 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004072 }`
4073
4074 testEnv := map[string]string{
4075 "FUZZ_FRAMEWORK": "AFL",
4076 }
4077
4078 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00004079
4080 checkPcGuardFlag := func(
4081 modName string, variantName string, shouldHave bool) {
4082 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
4083
4084 cFlags, ok := cc.Args["cFlags"]
4085 if !ok {
4086 t.Errorf("Could not find cFlags for module %s and variant %s",
4087 modName, variantName)
4088 }
4089
4090 if strings.Contains(
4091 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
4092 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
4093 "Test failed for module %s and variant %s",
4094 !shouldHave, shouldHave, modName, variantName)
4095 }
4096 }
4097
Cory Barkera1da26f2022-06-07 20:12:06 +00004098 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004099 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004100
4101 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004102 checkPcGuardFlag(moduleName, variant+"_static", false)
4103 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004104
4105 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004106 checkPcGuardFlag(moduleName, variant+"_static", false)
4107 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00004108
4109 ctx.ModuleForTests("afl_fuzz_shared_lib",
4110 "android_arm64_armv8-a_shared").Rule("cc")
4111 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004112 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
4113}
4114
4115func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004116 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004117 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
4118}
4119
4120func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004121 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004122 if runtime.GOOS != "linux" {
4123 t.Skip("requires linux")
4124 }
4125
4126 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00004127}
4128
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004129// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4130// correctly.
4131func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004132 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004133 ctx := testCc(t, `
4134 cc_fuzz {
4135 name: "fuzz_smoke_test",
4136 srcs: ["foo.c"],
4137 }`)
4138
Paul Duffin075c4172019-12-19 19:06:13 +00004139 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004140 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4141}
4142
Jooyung Han38002912019-05-16 04:01:54 +09004143func assertString(t *testing.T, got, expected string) {
4144 t.Helper()
4145 if got != expected {
4146 t.Errorf("expected %q got %q", expected, got)
4147 }
4148}
4149
4150func assertArrayString(t *testing.T, got, expected []string) {
4151 t.Helper()
4152 if len(got) != len(expected) {
4153 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4154 return
4155 }
4156 for i := range got {
4157 if got[i] != expected[i] {
4158 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4159 i, expected[i], expected, got[i], got)
4160 return
4161 }
4162 }
4163}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004164
Jooyung Han0302a842019-10-30 18:43:49 +09004165func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4166 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004167 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004168}
4169
Colin Crosse1bb5d02019-09-24 14:55:04 -07004170func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004171 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004172 ctx := testCc(t, `
4173 cc_defaults {
4174 name: "defaults",
4175 srcs: ["foo.c"],
4176 static: {
4177 srcs: ["bar.c"],
4178 },
4179 shared: {
4180 srcs: ["baz.c"],
4181 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004182 bazel_module: {
4183 bp2build_available: true,
4184 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004185 }
4186
4187 cc_library_static {
4188 name: "libstatic",
4189 defaults: ["defaults"],
4190 }
4191
4192 cc_library_shared {
4193 name: "libshared",
4194 defaults: ["defaults"],
4195 }
4196
4197 cc_library {
4198 name: "libboth",
4199 defaults: ["defaults"],
4200 }
4201
4202 cc_binary {
4203 name: "binary",
4204 defaults: ["defaults"],
4205 }`)
4206
Colin Cross7113d202019-11-20 16:39:12 -08004207 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004208 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4209 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4210 }
Colin Cross7113d202019-11-20 16:39:12 -08004211 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004212 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4213 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4214 }
Colin Cross7113d202019-11-20 16:39:12 -08004215 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004216 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4217 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4218 }
4219
Colin Cross7113d202019-11-20 16:39:12 -08004220 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004221 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4222 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4223 }
Colin Cross7113d202019-11-20 16:39:12 -08004224 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004225 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4226 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4227 }
4228}
Colin Crosseabaedd2020-02-06 17:01:55 -08004229
4230func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004231 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004232 bp := `
4233 cc_defaults {
4234 name: "libfoo_defaults",
4235 srcs: ["foo.c"],
4236 cppflags: ["-DFOO"],
4237 product_variables: {
4238 debuggable: {
4239 cppflags: ["-DBAR"],
4240 },
4241 },
4242 }
4243
4244 cc_library {
4245 name: "libfoo",
4246 defaults: ["libfoo_defaults"],
4247 }
4248 `
4249
Paul Duffin8567f222021-03-23 00:02:06 +00004250 result := android.GroupFixturePreparers(
4251 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004252 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004253
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004254 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4255 variables.Debuggable = BoolPtr(true)
4256 }),
4257 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004258
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004259 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004260 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004261}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004262
4263func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4264 t.Parallel()
4265 bp := `
4266 cc_library_static {
4267 name: "libfoo",
4268 srcs: ["foo.c"],
4269 whole_static_libs: ["libbar"],
4270 }
4271
4272 cc_library_static {
4273 name: "libbar",
4274 whole_static_libs: ["libmissing"],
4275 }
4276 `
4277
Paul Duffin8567f222021-03-23 00:02:06 +00004278 result := android.GroupFixturePreparers(
4279 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004280 android.PrepareForTestWithAllowMissingDependencies,
4281 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004282
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004283 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004284 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004285
Paul Duffine84b1332021-03-12 11:59:43 +00004286 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004287
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004288 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004289 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004290}
Colin Crosse9fe2942020-11-10 18:12:15 -08004291
4292func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004293 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004294 bp := `
4295 cc_binary {
4296 name: "bin",
4297 host_supported: true,
4298 shared_libs: ["libshared"],
4299 runtime_libs: ["libruntime"],
4300 srcs: [":gen"],
4301 }
4302
4303 cc_library_shared {
4304 name: "libshared",
4305 host_supported: true,
4306 shared_libs: ["libtransitive"],
4307 }
4308
4309 cc_library_shared {
4310 name: "libtransitive",
4311 host_supported: true,
4312 }
4313
4314 cc_library_shared {
4315 name: "libruntime",
4316 host_supported: true,
4317 }
4318
4319 cc_binary_host {
4320 name: "tool",
4321 srcs: ["foo.cpp"],
4322 }
4323
4324 genrule {
4325 name: "gen",
4326 tools: ["tool"],
4327 out: ["gen.cpp"],
4328 cmd: "$(location tool) $(out)",
4329 }
4330 `
4331
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004332 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004333 ctx := testCcWithConfig(t, config)
4334
4335 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4336 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4337 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4338 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4339 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4340
4341 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4342 t.Errorf("expected host bin dependency %q, got %q", w, g)
4343 }
4344
4345 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4346 t.Errorf("expected host bin dependency %q, got %q", w, g)
4347 }
4348
4349 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4350 t.Errorf("expected host bin dependency %q, got %q", w, g)
4351 }
4352
4353 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4354 t.Errorf("expected host bin dependency %q, got %q", w, g)
4355 }
4356
4357 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4358 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4359 }
4360
4361 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4362 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4363 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4364 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4365
4366 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4367 t.Errorf("expected device bin dependency %q, got %q", w, g)
4368 }
4369
4370 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4371 t.Errorf("expected device bin dependency %q, got %q", w, g)
4372 }
4373
4374 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4375 t.Errorf("expected device bin dependency %q, got %q", w, g)
4376 }
4377
4378 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4379 t.Errorf("expected device bin dependency %q, got %q", w, g)
4380 }
4381
4382 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4383 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4384 }
4385
4386}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004387
4388func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004389 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004390 ctx := testCc(t, `
4391 cc_library_shared {
4392 name: "libclient",
4393 srcs: ["foo.c"],
4394 shared_libs: ["libfoo#1"],
4395 }
4396
4397 cc_library_shared {
4398 name: "libfoo",
4399 srcs: ["foo.c"],
4400 shared_libs: ["libbar"],
4401 export_shared_lib_headers: ["libbar"],
4402 stubs: {
4403 symbol_file: "foo.map.txt",
4404 versions: ["1", "2", "3"],
4405 },
4406 }
4407
4408 cc_library_shared {
4409 name: "libbar",
4410 export_include_dirs: ["include/libbar"],
4411 srcs: ["foo.c"],
4412 }`)
4413
4414 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4415
4416 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4417 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4418 }
4419}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004420
4421func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004422 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09004423 ctx := testCc(t, `
4424 cc_library {
4425 name: "libfoo",
4426 srcs: ["a/Foo.aidl"],
4427 aidl: { flags: ["-Werror"], },
4428 }
4429 `)
4430
4431 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4432 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4433 aidlCommand := manifest.Commands[0].GetCommand()
4434 expectedAidlFlag := "-Werror"
4435 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4436 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4437 }
4438}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004439
Jooyung Han07f70c02021-11-06 07:08:45 +09004440func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004441 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004442 for _, tc := range []struct {
4443 name string
4444 sdkVersion string
4445 variant string
4446 expected string
4447 }{
4448 {
4449 name: "default is current",
4450 sdkVersion: "",
4451 variant: "android_arm64_armv8-a_static",
4452 expected: "platform_apis",
4453 },
4454 {
4455 name: "use sdk_version",
4456 sdkVersion: `sdk_version: "29"`,
4457 variant: "android_arm64_armv8-a_static",
4458 expected: "platform_apis",
4459 },
4460 {
4461 name: "use sdk_version(sdk variant)",
4462 sdkVersion: `sdk_version: "29"`,
4463 variant: "android_arm64_armv8-a_sdk_static",
4464 expected: "29",
4465 },
4466 {
4467 name: "use min_sdk_version",
4468 sdkVersion: `min_sdk_version: "29"`,
4469 variant: "android_arm64_armv8-a_static",
4470 expected: "29",
4471 },
4472 } {
4473 t.Run(tc.name, func(t *testing.T) {
4474 ctx := testCc(t, `
4475 cc_library {
4476 name: "libfoo",
4477 stl: "none",
4478 srcs: ["a/Foo.aidl"],
4479 `+tc.sdkVersion+`
4480 }
4481 `)
4482 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4483 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4484 aidlCommand := manifest.Commands[0].GetCommand()
4485 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4486 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4487 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4488 }
4489 })
4490 }
4491}
4492
Jiyong Parka008fb02021-03-16 17:15:53 +09004493func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004494 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004495 ctx := testCc(t, `
4496 cc_library_shared {
4497 name: "libfoo",
4498 srcs: ["foo.c"],
4499 min_sdk_version: "29",
4500 }`)
4501
4502 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4503 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4504}
4505
Vinh Tranf1924742022-06-24 16:40:11 -04004506func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004507 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004508 bp := `
4509 cc_library_shared {
4510 name: "libfoo",
4511 srcs: ["foo.c"],
4512 min_sdk_version: "S",
4513 }
4514 `
4515 result := android.GroupFixturePreparers(
4516 prepareForCcTest,
4517 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4518 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4519 }),
4520 ).RunTestWithBp(t, bp)
4521 ctx := result.TestContext
4522 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4523 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4524}
4525
Paul Duffin3cb603e2021-02-19 13:57:10 +00004526func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004527 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004528
4529 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4530 // embedded newline characters alone.
4531 trimIndentingSpaces := func(s string) string {
4532 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4533 }
4534
4535 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4536 t.Helper()
4537 expected = trimIndentingSpaces(expected)
4538 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4539 if expected != actual {
4540 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4541 }
4542 }
4543
4544 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4545
4546 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4547 t.Helper()
4548 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4549 name := module.Name()
4550
4551 for _, checker := range checkers {
4552 checker(t, name, exported)
4553 }
4554 }
4555
4556 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4557 return func(t *testing.T, name string, exported FlagExporterInfo) {
4558 t.Helper()
4559 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4560 }
4561 }
4562
4563 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4564 return func(t *testing.T, name string, exported FlagExporterInfo) {
4565 t.Helper()
4566 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4567 }
4568 }
4569
4570 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4571 return func(t *testing.T, name string, exported FlagExporterInfo) {
4572 t.Helper()
4573 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4574 }
4575 }
4576
4577 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4578 return func(t *testing.T, name string, exported FlagExporterInfo) {
4579 t.Helper()
4580 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4581 }
4582 }
4583
4584 genRuleModules := `
4585 genrule {
4586 name: "genrule_foo",
4587 cmd: "generate-foo",
4588 out: [
4589 "generated_headers/foo/generated_header.h",
4590 ],
4591 export_include_dirs: [
4592 "generated_headers",
4593 ],
4594 }
4595
4596 genrule {
4597 name: "genrule_bar",
4598 cmd: "generate-bar",
4599 out: [
4600 "generated_headers/bar/generated_header.h",
4601 ],
4602 export_include_dirs: [
4603 "generated_headers",
4604 ],
4605 }
4606 `
4607
4608 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4609 ctx := testCc(t, genRuleModules+`
4610 cc_library {
4611 name: "libfoo",
4612 srcs: ["foo.c"],
4613 export_include_dirs: ["foo/standard"],
4614 export_system_include_dirs: ["foo/system"],
4615 generated_headers: ["genrule_foo"],
4616 export_generated_headers: ["genrule_foo"],
4617 }
4618
4619 cc_library {
4620 name: "libbar",
4621 srcs: ["bar.c"],
4622 shared_libs: ["libfoo"],
4623 export_include_dirs: ["bar/standard"],
4624 export_system_include_dirs: ["bar/system"],
4625 generated_headers: ["genrule_bar"],
4626 export_generated_headers: ["genrule_bar"],
4627 }
4628 `)
4629 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4630 checkIncludeDirs(t, ctx, foo,
4631 expectedIncludeDirs(`
4632 foo/standard
4633 .intermediates/genrule_foo/gen/generated_headers
4634 `),
4635 expectedSystemIncludeDirs(`foo/system`),
4636 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4637 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4638 )
4639
4640 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4641 checkIncludeDirs(t, ctx, bar,
4642 expectedIncludeDirs(`
4643 bar/standard
4644 .intermediates/genrule_bar/gen/generated_headers
4645 `),
4646 expectedSystemIncludeDirs(`bar/system`),
4647 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4648 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4649 )
4650 })
4651
4652 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4653 ctx := testCc(t, genRuleModules+`
4654 cc_library {
4655 name: "libfoo",
4656 srcs: ["foo.c"],
4657 export_include_dirs: ["foo/standard"],
4658 export_system_include_dirs: ["foo/system"],
4659 generated_headers: ["genrule_foo"],
4660 export_generated_headers: ["genrule_foo"],
4661 }
4662
4663 cc_library {
4664 name: "libbar",
4665 srcs: ["bar.c"],
4666 whole_static_libs: ["libfoo"],
4667 export_include_dirs: ["bar/standard"],
4668 export_system_include_dirs: ["bar/system"],
4669 generated_headers: ["genrule_bar"],
4670 export_generated_headers: ["genrule_bar"],
4671 }
4672 `)
4673 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4674 checkIncludeDirs(t, ctx, foo,
4675 expectedIncludeDirs(`
4676 foo/standard
4677 .intermediates/genrule_foo/gen/generated_headers
4678 `),
4679 expectedSystemIncludeDirs(`foo/system`),
4680 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4681 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4682 )
4683
4684 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4685 checkIncludeDirs(t, ctx, bar,
4686 expectedIncludeDirs(`
4687 bar/standard
4688 foo/standard
4689 .intermediates/genrule_foo/gen/generated_headers
4690 .intermediates/genrule_bar/gen/generated_headers
4691 `),
4692 expectedSystemIncludeDirs(`
4693 bar/system
4694 foo/system
4695 `),
4696 expectedGeneratedHeaders(`
4697 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4698 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4699 `),
4700 expectedOrderOnlyDeps(`
4701 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4702 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4703 `),
4704 )
4705 })
4706
Paul Duffin3cb603e2021-02-19 13:57:10 +00004707 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4708 ctx := testCc(t, genRuleModules+`
4709 cc_library_shared {
4710 name: "libfoo",
4711 srcs: [
4712 "foo.c",
4713 "b.aidl",
4714 "a.proto",
4715 ],
4716 aidl: {
4717 export_aidl_headers: true,
4718 }
4719 }
4720 `)
4721 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4722 checkIncludeDirs(t, ctx, foo,
4723 expectedIncludeDirs(`
4724 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4725 `),
4726 expectedSystemIncludeDirs(``),
4727 expectedGeneratedHeaders(`
4728 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4729 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4730 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004731 `),
4732 expectedOrderOnlyDeps(`
4733 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4734 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4735 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004736 `),
4737 )
4738 })
4739
Paul Duffin3cb603e2021-02-19 13:57:10 +00004740 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4741 ctx := testCc(t, genRuleModules+`
4742 cc_library_shared {
4743 name: "libfoo",
4744 srcs: [
4745 "foo.c",
4746 "b.aidl",
4747 "a.proto",
4748 ],
4749 proto: {
4750 export_proto_headers: true,
4751 }
4752 }
4753 `)
4754 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4755 checkIncludeDirs(t, ctx, foo,
4756 expectedIncludeDirs(`
4757 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4758 `),
4759 expectedSystemIncludeDirs(``),
4760 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004761 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4762 `),
4763 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004764 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4765 `),
4766 )
4767 })
4768
Paul Duffin33056e82021-02-19 13:49:08 +00004769 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004770 ctx := testCc(t, genRuleModules+`
4771 cc_library_shared {
4772 name: "libfoo",
4773 srcs: [
4774 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004775 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004776 "b.aidl",
4777 "a.proto",
4778 ],
4779 }
4780 `)
4781 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4782 checkIncludeDirs(t, ctx, foo,
4783 expectedIncludeDirs(`
4784 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4785 `),
4786 expectedSystemIncludeDirs(``),
4787 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004788 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004789 `),
4790 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004791 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4792 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004793 `),
4794 )
4795 })
4796}
Colin Crossae628182021-06-14 16:52:28 -07004797
4798func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004799 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004800 baseExpectedFlags := []string{
4801 "${config.ArmThumbCflags}",
4802 "${config.ArmCflags}",
4803 "${config.CommonGlobalCflags}",
4804 "${config.DeviceGlobalCflags}",
4805 "${config.ExternalCflags}",
4806 "${config.ArmToolchainCflags}",
4807 "${config.ArmArmv7ANeonCflags}",
4808 "${config.ArmGenericCflags}",
4809 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004810 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004811 }
4812
4813 expectedIncludes := []string{
4814 "external/foo/android_arm_export_include_dirs",
4815 "external/foo/lib32_export_include_dirs",
4816 "external/foo/arm_export_include_dirs",
4817 "external/foo/android_export_include_dirs",
4818 "external/foo/linux_export_include_dirs",
4819 "external/foo/export_include_dirs",
4820 "external/foo/android_arm_local_include_dirs",
4821 "external/foo/lib32_local_include_dirs",
4822 "external/foo/arm_local_include_dirs",
4823 "external/foo/android_local_include_dirs",
4824 "external/foo/linux_local_include_dirs",
4825 "external/foo/local_include_dirs",
4826 "external/foo",
4827 "external/foo/libheader1",
4828 "external/foo/libheader2",
4829 "external/foo/libwhole1",
4830 "external/foo/libwhole2",
4831 "external/foo/libstatic1",
4832 "external/foo/libstatic2",
4833 "external/foo/libshared1",
4834 "external/foo/libshared2",
4835 "external/foo/liblinux",
4836 "external/foo/libandroid",
4837 "external/foo/libarm",
4838 "external/foo/lib32",
4839 "external/foo/libandroid_arm",
4840 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004841 }
4842
4843 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4844 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4845
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004846 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004847 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004848 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004849
4850 lastIncludes := []string{
4851 "out/soong/ndk/sysroot/usr/include",
4852 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4853 }
4854
4855 combineSlices := func(slices ...[]string) []string {
4856 var ret []string
4857 for _, s := range slices {
4858 ret = append(ret, s...)
4859 }
4860 return ret
4861 }
4862
4863 testCases := []struct {
4864 name string
4865 src string
4866 expected []string
4867 }{
4868 {
4869 name: "c",
4870 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004871 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004872 },
4873 {
4874 name: "cc",
4875 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004876 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004877 },
4878 {
4879 name: "assemble",
4880 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004881 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004882 },
4883 }
4884
4885 for _, tc := range testCases {
4886 t.Run(tc.name, func(t *testing.T) {
4887 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004888 cc_library {
4889 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004890 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004891 cflags: ["-std=candcpp"],
4892 conlyflags: ["-std=conly"],
4893 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004894 local_include_dirs: ["local_include_dirs"],
4895 export_include_dirs: ["export_include_dirs"],
4896 export_system_include_dirs: ["export_system_include_dirs"],
4897 static_libs: ["libstatic1", "libstatic2"],
4898 whole_static_libs: ["libwhole1", "libwhole2"],
4899 shared_libs: ["libshared1", "libshared2"],
4900 header_libs: ["libheader1", "libheader2"],
4901 target: {
4902 android: {
4903 shared_libs: ["libandroid"],
4904 local_include_dirs: ["android_local_include_dirs"],
4905 export_include_dirs: ["android_export_include_dirs"],
4906 },
4907 android_arm: {
4908 shared_libs: ["libandroid_arm"],
4909 local_include_dirs: ["android_arm_local_include_dirs"],
4910 export_include_dirs: ["android_arm_export_include_dirs"],
4911 },
4912 linux: {
4913 shared_libs: ["liblinux"],
4914 local_include_dirs: ["linux_local_include_dirs"],
4915 export_include_dirs: ["linux_export_include_dirs"],
4916 },
4917 },
4918 multilib: {
4919 lib32: {
4920 shared_libs: ["lib32"],
4921 local_include_dirs: ["lib32_local_include_dirs"],
4922 export_include_dirs: ["lib32_export_include_dirs"],
4923 },
4924 },
4925 arch: {
4926 arm: {
4927 shared_libs: ["libarm"],
4928 local_include_dirs: ["arm_local_include_dirs"],
4929 export_include_dirs: ["arm_export_include_dirs"],
4930 },
4931 },
4932 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004933 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004934 }
4935
4936 cc_library_headers {
4937 name: "libheader1",
4938 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004939 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004940 stl: "none",
4941 }
4942
4943 cc_library_headers {
4944 name: "libheader2",
4945 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004946 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004947 stl: "none",
4948 }
Liz Kammer08572c62021-09-30 10:11:04 -04004949 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004950
Liz Kammer08572c62021-09-30 10:11:04 -04004951 libs := []string{
4952 "libstatic1",
4953 "libstatic2",
4954 "libwhole1",
4955 "libwhole2",
4956 "libshared1",
4957 "libshared2",
4958 "libandroid",
4959 "libandroid_arm",
4960 "liblinux",
4961 "lib32",
4962 "libarm",
4963 }
Colin Crossae628182021-06-14 16:52:28 -07004964
Liz Kammer08572c62021-09-30 10:11:04 -04004965 for _, lib := range libs {
4966 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004967 cc_library {
4968 name: "%s",
4969 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004970 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004971 stl: "none",
4972 }
4973 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004974 }
4975
4976 ctx := android.GroupFixturePreparers(
4977 PrepareForIntegrationTestWithCc,
4978 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4979 ).RunTest(t)
4980 // Use the arm variant instead of the arm64 variant so that it gets headers from
4981 // ndk_libandroid_support to test LateStaticLibs.
4982 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4983
4984 var includes []string
4985 flags := strings.Split(cflags, " ")
4986 for _, flag := range flags {
4987 if strings.HasPrefix(flag, "-I") {
4988 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4989 } else if flag == "-isystem" {
4990 // skip isystem, include next
4991 } else if len(flag) > 0 {
4992 includes = append(includes, flag)
4993 }
4994 }
4995
4996 android.AssertArrayString(t, "includes", tc.expected, includes)
4997 })
Colin Crossae628182021-06-14 16:52:28 -07004998 }
4999
Colin Crossae628182021-06-14 16:52:28 -07005000}
Alixb5f6d9e2022-04-20 23:00:58 +00005001
zijunzhao933e3802023-01-12 07:26:20 +00005002func TestAddnoOverride64GlobalCflags(t *testing.T) {
5003 t.Parallel()
5004 ctx := testCc(t, `
5005 cc_library_shared {
5006 name: "libclient",
5007 srcs: ["foo.c"],
5008 shared_libs: ["libfoo#1"],
5009 }
5010
5011 cc_library_shared {
5012 name: "libfoo",
5013 srcs: ["foo.c"],
5014 shared_libs: ["libbar"],
5015 export_shared_lib_headers: ["libbar"],
5016 stubs: {
5017 symbol_file: "foo.map.txt",
5018 versions: ["1", "2", "3"],
5019 },
5020 }
5021
5022 cc_library_shared {
5023 name: "libbar",
5024 export_include_dirs: ["include/libbar"],
5025 srcs: ["foo.c"],
5026 }`)
5027
5028 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5029
5030 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5031 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5032 }
5033}
5034
Alixb5f6d9e2022-04-20 23:00:58 +00005035func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005036 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005037 tests := []struct {
5038 name string
5039 clang bool
5040 BuildBrokenClangProperty bool
5041 err string
5042 }{
5043 {
5044 name: "error when clang is set to false",
5045 clang: false,
5046 err: "is no longer supported",
5047 },
5048 {
5049 name: "error when clang is set to true",
5050 clang: true,
5051 err: "property is deprecated, see Changes.md",
5052 },
5053 {
5054 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5055 clang: true,
5056 BuildBrokenClangProperty: true,
5057 },
5058 }
5059
5060 for _, test := range tests {
5061 t.Run(test.name, func(t *testing.T) {
5062 bp := fmt.Sprintf(`
5063 cc_library {
5064 name: "foo",
5065 clang: %t,
5066 }`, test.clang)
5067
5068 if test.err == "" {
5069 android.GroupFixturePreparers(
5070 prepareForCcTest,
5071 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5072 if test.BuildBrokenClangProperty {
5073 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5074 }
5075 }),
5076 ).RunTestWithBp(t, bp)
5077 } else {
5078 prepareForCcTest.
5079 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5080 RunTestWithBp(t, bp)
5081 }
5082 })
5083 }
5084}
Alix Espinoef47e542022-09-14 19:10:51 +00005085
5086func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005087 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005088 tests := []struct {
5089 name string
5090 clangAsFlags []string
5091 BuildBrokenClangAsFlags bool
5092 err string
5093 }{
5094 {
5095 name: "error when clang_asflags is set",
5096 clangAsFlags: []string{"-a", "-b"},
5097 err: "clang_asflags: property is deprecated",
5098 },
5099 {
5100 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5101 clangAsFlags: []string{"-a", "-b"},
5102 BuildBrokenClangAsFlags: true,
5103 },
5104 }
5105
5106 for _, test := range tests {
5107 t.Run(test.name, func(t *testing.T) {
5108 bp := fmt.Sprintf(`
5109 cc_library {
5110 name: "foo",
5111 clang_asflags: %s,
5112 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5113
5114 if test.err == "" {
5115 android.GroupFixturePreparers(
5116 prepareForCcTest,
5117 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5118 if test.BuildBrokenClangAsFlags {
5119 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5120 }
5121 }),
5122 ).RunTestWithBp(t, bp)
5123 } else {
5124 prepareForCcTest.
5125 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5126 RunTestWithBp(t, bp)
5127 }
5128 })
5129 }
5130}
5131
5132func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005133 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005134 tests := []struct {
5135 name string
5136 clangCFlags []string
5137 BuildBrokenClangCFlags bool
5138 err string
5139 }{
5140 {
5141 name: "error when clang_cflags is set",
5142 clangCFlags: []string{"-a", "-b"},
5143 err: "clang_cflags: property is deprecated",
5144 },
5145 {
5146 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5147 clangCFlags: []string{"-a", "-b"},
5148 BuildBrokenClangCFlags: true,
5149 },
5150 }
5151
5152 for _, test := range tests {
5153 t.Run(test.name, func(t *testing.T) {
5154 bp := fmt.Sprintf(`
5155 cc_library {
5156 name: "foo",
5157 clang_cflags: %s,
5158 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5159
5160 if test.err == "" {
5161 android.GroupFixturePreparers(
5162 prepareForCcTest,
5163 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5164 if test.BuildBrokenClangCFlags {
5165 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5166 }
5167 }),
5168 ).RunTestWithBp(t, bp)
5169 } else {
5170 prepareForCcTest.
5171 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5172 RunTestWithBp(t, bp)
5173 }
5174 })
5175 }
5176}
Yu Liue4312402023-01-18 09:15:31 -08005177
5178func TestDclaLibraryInApex(t *testing.T) {
5179 t.Parallel()
5180 bp := `
5181 cc_library_shared {
5182 name: "cc_lib_in_apex",
5183 srcs: ["foo.cc"],
5184 apex_available: ["myapex"],
5185 bazel_module: { label: "//foo/bar:bar" },
5186 }`
5187 label := "//foo/bar:bar"
5188 arch64 := "arm64_armv8-a"
5189 arch32 := "arm_armv7-a-neon"
5190 apexCfgKey := android.ApexConfigKey{
5191 WithinApex: true,
5192 ApexSdkVersion: "28",
5193 }
5194
5195 result := android.GroupFixturePreparers(
5196 prepareForCcTest,
5197 android.FixtureRegisterWithContext(registerTestMutators),
5198 android.FixtureModifyConfig(func(config android.Config) {
5199 config.BazelContext = android.MockBazelContext{
5200 OutputBaseDir: "outputbase",
5201 LabelToCcInfo: map[string]cquery.CcInfo{
5202 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5203 RootDynamicLibraries: []string{"foo.so"},
5204 },
5205 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5206 RootDynamicLibraries: []string{"foo.so"},
5207 },
5208 },
5209 BazelRequests: make(map[string]bool),
5210 }
5211 }),
5212 ).RunTestWithBp(t, bp)
5213 ctx := result.TestContext
5214
5215 // Test if the bazel request is queued correctly
5216 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5217 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5218 t.Errorf("Bazel request was not queued: %s", key)
5219 }
5220
5221 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5222 producer := sharedFoo.(android.OutputFileProducer)
5223 outputFiles, err := producer.OutputFiles("")
5224 if err != nil {
5225 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5226 }
5227 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5228 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5229}
Sam Delmericoef69d472023-04-18 17:32:43 -04005230
5231func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5232 t.Parallel()
5233 bp := `
5234 cc_library_static {
5235 name: "foo_ubsan_minimal",
5236 srcs: ["foo.cc"],
5237 bazel_module: { label: "//foo_ubsan_minimal" },
5238 sanitize: {
5239 all_undefined: true,
5240 integer_overflow: true,
5241 },
5242 }
5243 cc_library_static {
5244 name: "foo",
5245 srcs: ["foo.cc"],
5246 bazel_module: { label: "//foo" },
5247 sanitize: {
5248 address: true,
5249 hwaddress: true,
5250 fuzzer: true,
5251 integer_overflow: true,
5252 scs: true,
5253 },
5254 }
5255 cc_library_static {
5256 name: "foo_tsan",
5257 srcs: ["foo.cc"],
5258 bazel_module: { label: "//foo_tsan" },
5259 sanitize: {
5260 thread: true,
5261 },
5262 }
5263 cc_library_static {
5264 name: "foo_cfi",
5265 srcs: ["foo.cc"],
5266 bazel_module: { label: "//foo_cfi" },
5267 sanitize: {
5268 cfi: true,
5269 },
5270 }
5271 cc_library_static {
5272 name: "foo_memtag_stack",
5273 srcs: ["foo.cc"],
5274 bazel_module: { label: "//foo_memtag_stack" },
5275 sanitize: {
5276 memtag_stack: true,
5277 },
5278 }
5279 cc_library_static {
5280 name: "foo_memtag_heap",
5281 srcs: ["foo.cc"],
5282 bazel_module: { label: "//foo_memtag_heap" },
5283 sanitize: {
5284 memtag_heap: true,
5285 },
5286 }
5287 cc_library_static {
5288 name: "foo_safestack",
5289 srcs: ["foo.cc"],
5290 bazel_module: { label: "//foo_safestack" },
5291 sanitize: {
5292 safestack: true,
5293 },
5294 }
5295 cc_library_static {
5296 name: "foo_scudo",
5297 srcs: ["foo.cc"],
5298 bazel_module: { label: "//foo_scudo" },
5299 sanitize: {
5300 scudo: true,
5301 },
5302 }
5303 `
5304 testcases := []struct {
5305 name string
5306 variant string
5307 expectedOutputPaths []string
5308 }{
5309 {
5310 name: "foo_ubsan_minimal",
5311 variant: "android_arm64_armv8-a_static_apex28",
5312 expectedOutputPaths: []string{
5313 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5314 },
5315 },
5316 {
5317 name: "foo",
5318 variant: "android_arm64_armv8-a_static_apex28",
5319 expectedOutputPaths: []string{
5320 "outputbase/execroot/__main__/foo.a",
5321 },
5322 },
5323 {
5324 name: "foo",
5325 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5326 expectedOutputPaths: []string{
5327 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5328 },
5329 },
5330 {
5331 name: "foo",
5332 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5333 expectedOutputPaths: []string{
5334 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5335 },
5336 },
5337 {
5338 name: "foo",
5339 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5340 expectedOutputPaths: []string{
5341 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5342 },
5343 },
5344 {
5345 name: "foo",
5346 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5347 expectedOutputPaths: []string{
5348 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5349 },
5350 },
5351 {
5352 name: "foo",
5353 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5354 expectedOutputPaths: []string{
5355 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5356 },
5357 },
5358 {
5359 name: "foo",
5360 variant: "android_arm64_armv8-a_static_scs_apex28",
5361 expectedOutputPaths: []string{
5362 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5363 },
5364 },
5365 {
5366 name: "foo",
5367 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5368 expectedOutputPaths: []string{
5369 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5370 },
5371 },
5372 {
5373 name: "foo",
5374 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5375 expectedOutputPaths: []string{
5376 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5377 },
5378 },
5379 {
5380 name: "foo_tsan",
5381 variant: "android_arm64_armv8-a_static_apex28",
5382 expectedOutputPaths: []string{
5383 "outputbase/execroot/__main__/foo_tsan.a",
5384 },
5385 },
5386 {
5387 name: "foo_tsan",
5388 variant: "android_arm64_armv8-a_static_tsan_apex28",
5389 expectedOutputPaths: []string{
5390 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5391 },
5392 },
5393 {
5394 name: "foo_cfi",
5395 variant: "android_arm64_armv8-a_static_apex28",
5396 expectedOutputPaths: []string{
5397 "outputbase/execroot/__main__/foo_cfi.a",
5398 },
5399 },
5400 {
5401 name: "foo_cfi",
5402 variant: "android_arm64_armv8-a_static_cfi_apex28",
5403 expectedOutputPaths: []string{
5404 "out/soong/.intermediates/foo_cfi/android_arm64_armv8-a_static_cfi_apex28/foo_cfi.a",
5405 },
5406 },
5407 {
5408 name: "foo_memtag_stack",
5409 variant: "android_arm64_armv8-a_static_apex28",
5410 expectedOutputPaths: []string{
5411 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5412 },
5413 },
5414 {
5415 name: "foo_memtag_heap",
5416 variant: "android_arm64_armv8-a_static_apex28",
5417 expectedOutputPaths: []string{
5418 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5419 },
5420 },
5421 {
5422 name: "foo_safestack",
5423 variant: "android_arm64_armv8-a_static_apex28",
5424 expectedOutputPaths: []string{
5425 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5426 },
5427 },
5428 {
5429 name: "foo_scudo",
5430 variant: "android_arm64_armv8-a_static_apex28",
5431 expectedOutputPaths: []string{
5432 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5433 },
5434 },
5435 }
5436
5437 ctx := android.GroupFixturePreparers(
5438 prepareForCcTest,
5439 prepareForAsanTest,
5440 android.FixtureRegisterWithContext(registerTestMutators),
5441 android.FixtureModifyConfig(func(config android.Config) {
5442 config.BazelContext = android.MockBazelContext{
5443 OutputBaseDir: "outputbase",
5444 LabelToCcInfo: map[string]cquery.CcInfo{
5445 "//foo_ubsan_minimal": {
5446 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5447 },
5448 "//foo": {
5449 RootStaticArchives: []string{"foo.a"},
5450 },
5451 "//foo_tsan": {
5452 RootStaticArchives: []string{"foo_tsan.a"},
5453 },
5454 "//foo_cfi": {
5455 RootStaticArchives: []string{"foo_cfi.a"},
5456 },
5457 "//foo_memtag_stack": {
5458 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5459 },
5460 "//foo_memtag_heap": {
5461 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5462 },
5463 "//foo_safestack": {
5464 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5465 },
5466 "//foo_scudo": {
5467 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5468 },
5469 },
5470 }
5471 }),
5472 ).RunTestWithBp(t, bp).TestContext
5473
5474 for _, tc := range testcases {
5475 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5476 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5477 if err != nil {
5478 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5479 }
5480 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5481 }
5482}