blob: 830de40c03c3d49bd16c795c62e01ed7f6bff10e [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) {
Liz Kammera22c4b62023-03-27 10:04:58 -04003581 // TODO(b/275313114): Test exposes non-determinism which should be corrected and the test
3582 // reenabled.
3583 t.Skip()
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003584 t.Parallel()
3585 ctx := testCc(t, `
3586 cc_library_shared {
3587 name: "libFoo",
3588 srcs: ["foo.c"],
3589 stubs: {
3590 symbol_file: "foo.map.txt",
3591 versions: ["current"],
3592 },
3593 apex_available: ["bar", "a1"],
3594 }
3595
3596 cc_library_shared {
3597 name: "libBar",
3598 srcs: ["bar.c"],
3599 shared_libs: ["libFoo"],
3600 apex_available: ["a1"],
3601 }
3602
3603 cc_library_shared {
3604 name: "libA1",
3605 srcs: ["a1.c"],
3606 shared_libs: ["libFoo"],
3607 apex_available: ["a1"],
3608 }
3609
3610 cc_library_shared {
3611 name: "libBarA1",
3612 srcs: ["bara1.c"],
3613 shared_libs: ["libFoo"],
3614 apex_available: ["bar", "a1"],
3615 }
3616
3617 cc_library_shared {
3618 name: "libAnyApex",
3619 srcs: ["anyApex.c"],
3620 shared_libs: ["libFoo"],
3621 apex_available: ["//apex_available:anyapex"],
3622 }
3623
3624 cc_library_shared {
3625 name: "libBaz",
3626 srcs: ["baz.c"],
3627 shared_libs: ["libFoo"],
3628 apex_available: ["baz"],
3629 }
3630
3631 cc_library_shared {
3632 name: "libQux",
3633 srcs: ["qux.c"],
3634 shared_libs: ["libFoo"],
3635 apex_available: ["qux", "bar"],
3636 }`)
3637
3638 variants := ctx.ModuleVariantsForTests("libFoo")
3639 expectedVariants := []string{
3640 "android_arm64_armv8-a_shared",
3641 "android_arm64_armv8-a_shared_current",
3642 "android_arm_armv7-a-neon_shared",
3643 "android_arm_armv7-a-neon_shared_current",
3644 }
3645 variantsMismatch := false
3646 if len(variants) != len(expectedVariants) {
3647 variantsMismatch = true
3648 } else {
3649 for _, v := range expectedVariants {
3650 if !inList(v, variants) {
3651 variantsMismatch = false
3652 }
3653 }
3654 }
3655 if variantsMismatch {
3656 t.Errorf("variants of libFoo expected:\n")
3657 for _, v := range expectedVariants {
3658 t.Errorf("%q\n", v)
3659 }
3660 t.Errorf(", but got:\n")
3661 for _, v := range variants {
3662 t.Errorf("%q\n", v)
3663 }
3664 }
3665
3666 linkAgainstFoo := []string{"libBarA1"}
3667 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3668
3669 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3670 for _, lib := range linkAgainstFoo {
3671 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3672 libFlags := libLinkRule.Args["libFlags"]
3673 if !strings.Contains(libFlags, libFooPath) {
3674 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3675 }
3676 }
3677
3678 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3679 for _, lib := range linkAgainstFooStubs {
3680 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3681 libFlags := libLinkRule.Args["libFlags"]
3682 if !strings.Contains(libFlags, libFooStubPath) {
3683 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3684 }
3685 }
3686}
3687
Jooyung Hanb04a4992020-03-13 18:57:35 +09003688func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003689 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003690 for _, tc := range []struct{ moduleName, expected string }{
3691 {"libc", "__LIBC_API__"},
3692 {"libfoo", "__LIBFOO_API__"},
3693 {"libfoo@1", "__LIBFOO_1_API__"},
3694 {"libfoo-v1", "__LIBFOO_V1_API__"},
3695 {"libfoo.v1", "__LIBFOO_V1_API__"},
3696 } {
3697 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3698 }
3699}
3700
Liz Kammer83cf81b2022-09-22 08:24:20 -04003701func pathsToBase(paths android.Paths) []string {
3702 var ret []string
3703 for _, p := range paths {
3704 ret = append(ret, p.Base())
3705 }
3706 return ret
3707}
3708
3709func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003710 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003711 ctx := testCc(t, `
3712 cc_library_static {
3713 name: "foo",
3714 srcs: ["foo.c"],
3715 }
3716
3717 cc_library_static {
3718 name: "bar",
3719 srcs: ["bar.c"],
3720 }
3721
3722 cc_library_shared {
3723 name: "qux",
3724 srcs: ["qux.c"],
3725 }
3726
3727 cc_library_static {
3728 name: "baz",
3729 srcs: ["baz.c"],
3730 static_libs: ["foo"],
3731 shared_libs: ["qux"],
3732 whole_static_libs: ["bar"],
3733 }`)
3734
3735 variant := "android_arm64_armv8-a_static"
3736 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3737
3738 // For static libraries, the object files of a whole static dep are included in the archive
3739 // directly
3740 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3741 t.Errorf("Expected input objects %q, got %q", w, g)
3742 }
3743
3744 // non whole static dependencies are not linked into the archive
3745 if len(arRule.Implicits) > 0 {
3746 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3747 }
3748}
3749
3750func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003751 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003752 ctx := testCc(t, `
3753 cc_library_static {
3754 name: "foo",
3755 srcs: ["foo.c"],
3756 }
3757
3758 cc_library_static {
3759 name: "bar",
3760 srcs: ["bar.c"],
3761 }
3762
3763 cc_library_shared {
3764 name: "qux",
3765 srcs: ["qux.c"],
3766 }
3767
3768 cc_library_shared {
3769 name: "baz",
3770 srcs: ["baz.c"],
3771 static_libs: ["foo"],
3772 shared_libs: ["qux"],
3773 whole_static_libs: ["bar"],
3774 }`)
3775
3776 variant := "android_arm64_armv8-a_shared"
3777 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3778 libFlags := linkRule.Args["libFlags"]
3779 // When dynamically linking, we expect static dependencies to be found on the command line
3780 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3781 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3782 }
3783 // When dynamically linking, we expect whole static dependencies to be found on the command line
3784 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3785 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3786 }
3787
3788 // When dynamically linking, we expect shared dependencies to be found on the command line
3789 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3790 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3791 }
3792
3793 // We should only have the objects from the shared library srcs, not the whole static dependencies
3794 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3795 t.Errorf("Expected input objects %q, got %q", w, g)
3796 }
3797}
3798
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003799func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003800 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003801 ctx := testCc(t, `
3802 cc_binary {
3803 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003804 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003805 static_executable: true,
3806 }`)
3807
Colin Cross7113d202019-11-20 16:39:12 -08003808 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003809 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3810 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003811 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003812 for _, lib := range systemStaticLibs {
3813 if !strings.Contains(libFlags, lib) {
3814 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3815 }
3816 }
3817 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3818 for _, lib := range systemSharedLibs {
3819 if strings.Contains(libFlags, lib) {
3820 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3821 }
3822 }
3823}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003824
3825func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003826 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003827 ctx := testCc(t, `
3828 cc_binary {
3829 name: "mybin",
3830 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003831 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003832 static_executable: true,
3833 stl: "none",
3834 }
3835
3836 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003837 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003838 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003839 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003840 stl: "none",
3841 }
3842
3843 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003844 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003845 srcs: ["foo.c"],
3846 stl: "none",
3847 stubs: {
3848 versions: ["1"],
3849 },
3850 }`)
3851
Colin Cross0de8a1e2020-09-18 14:15:30 -07003852 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3853 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003854 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003855
3856 if !reflect.DeepEqual(actual, expected) {
3857 t.Errorf("staticDeps orderings were not propagated correctly"+
3858 "\nactual: %v"+
3859 "\nexpected: %v",
3860 actual,
3861 expected,
3862 )
3863 }
3864}
Jooyung Han38002912019-05-16 04:01:54 +09003865
Jooyung Hand48f3c32019-08-23 11:18:57 +09003866func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003867 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003868 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3869 cc_library {
3870 name: "libA",
3871 srcs: ["foo.c"],
3872 shared_libs: ["libB"],
3873 stl: "none",
3874 }
3875
3876 cc_library {
3877 name: "libB",
3878 srcs: ["foo.c"],
3879 enabled: false,
3880 stl: "none",
3881 }
3882 `)
3883}
3884
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003885func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3886 bp := `
3887 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003888 name: "test_afl_fuzz_target",
3889 srcs: ["foo.c"],
3890 host_supported: true,
3891 static_libs: [
3892 "afl_fuzz_static_lib",
3893 ],
3894 shared_libs: [
3895 "afl_fuzz_shared_lib",
3896 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003897 fuzzing_frameworks: {
3898 afl: true,
3899 libfuzzer: false,
3900 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003901 }
3902 cc_library {
3903 name: "afl_fuzz_static_lib",
3904 host_supported: true,
3905 srcs: ["static_file.c"],
3906 }
3907 cc_library {
3908 name: "libfuzzer_only_static_lib",
3909 host_supported: true,
3910 srcs: ["static_file.c"],
3911 }
3912 cc_library {
3913 name: "afl_fuzz_shared_lib",
3914 host_supported: true,
3915 srcs: ["shared_file.c"],
3916 static_libs: [
3917 "second_static_lib",
3918 ],
3919 }
3920 cc_library_headers {
3921 name: "libafl_headers",
3922 vendor_available: true,
3923 host_supported: true,
3924 export_include_dirs: [
3925 "include",
3926 "instrumentation",
3927 ],
3928 }
3929 cc_object {
3930 name: "afl-compiler-rt",
3931 vendor_available: true,
3932 host_supported: true,
3933 cflags: [
3934 "-fPIC",
3935 ],
3936 srcs: [
3937 "instrumentation/afl-compiler-rt.o.c",
3938 ],
3939 }
3940 cc_library {
3941 name: "second_static_lib",
3942 host_supported: true,
3943 srcs: ["second_file.c"],
3944 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003945 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003946 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003947 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003948 srcs: [
3949 "aflpp_driver.c",
3950 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003951 }`
3952
3953 testEnv := map[string]string{
3954 "FUZZ_FRAMEWORK": "AFL",
3955 }
3956
3957 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003958
3959 checkPcGuardFlag := func(
3960 modName string, variantName string, shouldHave bool) {
3961 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3962
3963 cFlags, ok := cc.Args["cFlags"]
3964 if !ok {
3965 t.Errorf("Could not find cFlags for module %s and variant %s",
3966 modName, variantName)
3967 }
3968
3969 if strings.Contains(
3970 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3971 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3972 "Test failed for module %s and variant %s",
3973 !shouldHave, shouldHave, modName, variantName)
3974 }
3975 }
3976
Cory Barkera1da26f2022-06-07 20:12:06 +00003977 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003978 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003979
3980 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003981 checkPcGuardFlag(moduleName, variant+"_static", false)
3982 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003983
3984 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003985 checkPcGuardFlag(moduleName, variant+"_static", false)
3986 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003987
3988 ctx.ModuleForTests("afl_fuzz_shared_lib",
3989 "android_arm64_armv8-a_shared").Rule("cc")
3990 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003991 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3992}
3993
3994func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003995 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003996 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3997}
3998
3999func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004000 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00004001 if runtime.GOOS != "linux" {
4002 t.Skip("requires linux")
4003 }
4004
4005 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00004006}
4007
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004008// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4009// correctly.
4010func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004011 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004012 ctx := testCc(t, `
4013 cc_fuzz {
4014 name: "fuzz_smoke_test",
4015 srcs: ["foo.c"],
4016 }`)
4017
Paul Duffin075c4172019-12-19 19:06:13 +00004018 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004019 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4020}
4021
Jooyung Han38002912019-05-16 04:01:54 +09004022func assertString(t *testing.T, got, expected string) {
4023 t.Helper()
4024 if got != expected {
4025 t.Errorf("expected %q got %q", expected, got)
4026 }
4027}
4028
4029func assertArrayString(t *testing.T, got, expected []string) {
4030 t.Helper()
4031 if len(got) != len(expected) {
4032 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4033 return
4034 }
4035 for i := range got {
4036 if got[i] != expected[i] {
4037 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4038 i, expected[i], expected, got[i], got)
4039 return
4040 }
4041 }
4042}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004043
Jooyung Han0302a842019-10-30 18:43:49 +09004044func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4045 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004046 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004047}
4048
Colin Crosse1bb5d02019-09-24 14:55:04 -07004049func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004050 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004051 ctx := testCc(t, `
4052 cc_defaults {
4053 name: "defaults",
4054 srcs: ["foo.c"],
4055 static: {
4056 srcs: ["bar.c"],
4057 },
4058 shared: {
4059 srcs: ["baz.c"],
4060 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004061 bazel_module: {
4062 bp2build_available: true,
4063 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004064 }
4065
4066 cc_library_static {
4067 name: "libstatic",
4068 defaults: ["defaults"],
4069 }
4070
4071 cc_library_shared {
4072 name: "libshared",
4073 defaults: ["defaults"],
4074 }
4075
4076 cc_library {
4077 name: "libboth",
4078 defaults: ["defaults"],
4079 }
4080
4081 cc_binary {
4082 name: "binary",
4083 defaults: ["defaults"],
4084 }`)
4085
Colin Cross7113d202019-11-20 16:39:12 -08004086 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004087 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4088 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4089 }
Colin Cross7113d202019-11-20 16:39:12 -08004090 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004091 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4092 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4093 }
Colin Cross7113d202019-11-20 16:39:12 -08004094 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004095 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4096 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4097 }
4098
Colin Cross7113d202019-11-20 16:39:12 -08004099 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004100 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4101 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4102 }
Colin Cross7113d202019-11-20 16:39:12 -08004103 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004104 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4105 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4106 }
4107}
Colin Crosseabaedd2020-02-06 17:01:55 -08004108
4109func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004110 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004111 bp := `
4112 cc_defaults {
4113 name: "libfoo_defaults",
4114 srcs: ["foo.c"],
4115 cppflags: ["-DFOO"],
4116 product_variables: {
4117 debuggable: {
4118 cppflags: ["-DBAR"],
4119 },
4120 },
4121 }
4122
4123 cc_library {
4124 name: "libfoo",
4125 defaults: ["libfoo_defaults"],
4126 }
4127 `
4128
Paul Duffin8567f222021-03-23 00:02:06 +00004129 result := android.GroupFixturePreparers(
4130 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004131 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004132
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004133 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4134 variables.Debuggable = BoolPtr(true)
4135 }),
4136 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004137
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004138 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004139 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004140}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004141
4142func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4143 t.Parallel()
4144 bp := `
4145 cc_library_static {
4146 name: "libfoo",
4147 srcs: ["foo.c"],
4148 whole_static_libs: ["libbar"],
4149 }
4150
4151 cc_library_static {
4152 name: "libbar",
4153 whole_static_libs: ["libmissing"],
4154 }
4155 `
4156
Paul Duffin8567f222021-03-23 00:02:06 +00004157 result := android.GroupFixturePreparers(
4158 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004159 android.PrepareForTestWithAllowMissingDependencies,
4160 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004161
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004162 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004163 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004164
Paul Duffine84b1332021-03-12 11:59:43 +00004165 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004166
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004167 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004168 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004169}
Colin Crosse9fe2942020-11-10 18:12:15 -08004170
4171func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004172 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004173 bp := `
4174 cc_binary {
4175 name: "bin",
4176 host_supported: true,
4177 shared_libs: ["libshared"],
4178 runtime_libs: ["libruntime"],
4179 srcs: [":gen"],
4180 }
4181
4182 cc_library_shared {
4183 name: "libshared",
4184 host_supported: true,
4185 shared_libs: ["libtransitive"],
4186 }
4187
4188 cc_library_shared {
4189 name: "libtransitive",
4190 host_supported: true,
4191 }
4192
4193 cc_library_shared {
4194 name: "libruntime",
4195 host_supported: true,
4196 }
4197
4198 cc_binary_host {
4199 name: "tool",
4200 srcs: ["foo.cpp"],
4201 }
4202
4203 genrule {
4204 name: "gen",
4205 tools: ["tool"],
4206 out: ["gen.cpp"],
4207 cmd: "$(location tool) $(out)",
4208 }
4209 `
4210
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004211 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004212 ctx := testCcWithConfig(t, config)
4213
4214 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4215 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4216 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4217 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4218 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4219
4220 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4221 t.Errorf("expected host bin dependency %q, got %q", w, g)
4222 }
4223
4224 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4225 t.Errorf("expected host bin dependency %q, got %q", w, g)
4226 }
4227
4228 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4229 t.Errorf("expected host bin dependency %q, got %q", w, g)
4230 }
4231
4232 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4233 t.Errorf("expected host bin dependency %q, got %q", w, g)
4234 }
4235
4236 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4237 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4238 }
4239
4240 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4241 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4242 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4243 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4244
4245 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4246 t.Errorf("expected device bin dependency %q, got %q", w, g)
4247 }
4248
4249 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4250 t.Errorf("expected device bin dependency %q, got %q", w, g)
4251 }
4252
4253 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4254 t.Errorf("expected device bin dependency %q, got %q", w, g)
4255 }
4256
4257 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4258 t.Errorf("expected device bin dependency %q, got %q", w, g)
4259 }
4260
4261 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4262 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4263 }
4264
4265}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004266
4267func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004268 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004269 ctx := testCc(t, `
4270 cc_library_shared {
4271 name: "libclient",
4272 srcs: ["foo.c"],
4273 shared_libs: ["libfoo#1"],
4274 }
4275
4276 cc_library_shared {
4277 name: "libfoo",
4278 srcs: ["foo.c"],
4279 shared_libs: ["libbar"],
4280 export_shared_lib_headers: ["libbar"],
4281 stubs: {
4282 symbol_file: "foo.map.txt",
4283 versions: ["1", "2", "3"],
4284 },
4285 }
4286
4287 cc_library_shared {
4288 name: "libbar",
4289 export_include_dirs: ["include/libbar"],
4290 srcs: ["foo.c"],
4291 }`)
4292
4293 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4294
4295 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4296 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4297 }
4298}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004299
4300func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004301 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09004302 ctx := testCc(t, `
4303 cc_library {
4304 name: "libfoo",
4305 srcs: ["a/Foo.aidl"],
4306 aidl: { flags: ["-Werror"], },
4307 }
4308 `)
4309
4310 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4311 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4312 aidlCommand := manifest.Commands[0].GetCommand()
4313 expectedAidlFlag := "-Werror"
4314 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4315 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4316 }
4317}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004318
Jooyung Han07f70c02021-11-06 07:08:45 +09004319func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004320 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004321 for _, tc := range []struct {
4322 name string
4323 sdkVersion string
4324 variant string
4325 expected string
4326 }{
4327 {
4328 name: "default is current",
4329 sdkVersion: "",
4330 variant: "android_arm64_armv8-a_static",
4331 expected: "platform_apis",
4332 },
4333 {
4334 name: "use sdk_version",
4335 sdkVersion: `sdk_version: "29"`,
4336 variant: "android_arm64_armv8-a_static",
4337 expected: "platform_apis",
4338 },
4339 {
4340 name: "use sdk_version(sdk variant)",
4341 sdkVersion: `sdk_version: "29"`,
4342 variant: "android_arm64_armv8-a_sdk_static",
4343 expected: "29",
4344 },
4345 {
4346 name: "use min_sdk_version",
4347 sdkVersion: `min_sdk_version: "29"`,
4348 variant: "android_arm64_armv8-a_static",
4349 expected: "29",
4350 },
4351 } {
4352 t.Run(tc.name, func(t *testing.T) {
4353 ctx := testCc(t, `
4354 cc_library {
4355 name: "libfoo",
4356 stl: "none",
4357 srcs: ["a/Foo.aidl"],
4358 `+tc.sdkVersion+`
4359 }
4360 `)
4361 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4362 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4363 aidlCommand := manifest.Commands[0].GetCommand()
4364 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4365 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4366 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4367 }
4368 })
4369 }
4370}
4371
Jiyong Parka008fb02021-03-16 17:15:53 +09004372func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004373 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004374 ctx := testCc(t, `
4375 cc_library_shared {
4376 name: "libfoo",
4377 srcs: ["foo.c"],
4378 min_sdk_version: "29",
4379 }`)
4380
4381 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4382 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4383}
4384
Vinh Tranf1924742022-06-24 16:40:11 -04004385func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004386 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004387 bp := `
4388 cc_library_shared {
4389 name: "libfoo",
4390 srcs: ["foo.c"],
4391 min_sdk_version: "S",
4392 }
4393 `
4394 result := android.GroupFixturePreparers(
4395 prepareForCcTest,
4396 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4397 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4398 }),
4399 ).RunTestWithBp(t, bp)
4400 ctx := result.TestContext
4401 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4402 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4403}
4404
Paul Duffin3cb603e2021-02-19 13:57:10 +00004405func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004406 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004407
4408 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4409 // embedded newline characters alone.
4410 trimIndentingSpaces := func(s string) string {
4411 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4412 }
4413
4414 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4415 t.Helper()
4416 expected = trimIndentingSpaces(expected)
4417 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4418 if expected != actual {
4419 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4420 }
4421 }
4422
4423 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4424
4425 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4426 t.Helper()
4427 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4428 name := module.Name()
4429
4430 for _, checker := range checkers {
4431 checker(t, name, exported)
4432 }
4433 }
4434
4435 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4436 return func(t *testing.T, name string, exported FlagExporterInfo) {
4437 t.Helper()
4438 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4439 }
4440 }
4441
4442 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4443 return func(t *testing.T, name string, exported FlagExporterInfo) {
4444 t.Helper()
4445 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4446 }
4447 }
4448
4449 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4450 return func(t *testing.T, name string, exported FlagExporterInfo) {
4451 t.Helper()
4452 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4453 }
4454 }
4455
4456 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4457 return func(t *testing.T, name string, exported FlagExporterInfo) {
4458 t.Helper()
4459 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4460 }
4461 }
4462
4463 genRuleModules := `
4464 genrule {
4465 name: "genrule_foo",
4466 cmd: "generate-foo",
4467 out: [
4468 "generated_headers/foo/generated_header.h",
4469 ],
4470 export_include_dirs: [
4471 "generated_headers",
4472 ],
4473 }
4474
4475 genrule {
4476 name: "genrule_bar",
4477 cmd: "generate-bar",
4478 out: [
4479 "generated_headers/bar/generated_header.h",
4480 ],
4481 export_include_dirs: [
4482 "generated_headers",
4483 ],
4484 }
4485 `
4486
4487 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4488 ctx := testCc(t, genRuleModules+`
4489 cc_library {
4490 name: "libfoo",
4491 srcs: ["foo.c"],
4492 export_include_dirs: ["foo/standard"],
4493 export_system_include_dirs: ["foo/system"],
4494 generated_headers: ["genrule_foo"],
4495 export_generated_headers: ["genrule_foo"],
4496 }
4497
4498 cc_library {
4499 name: "libbar",
4500 srcs: ["bar.c"],
4501 shared_libs: ["libfoo"],
4502 export_include_dirs: ["bar/standard"],
4503 export_system_include_dirs: ["bar/system"],
4504 generated_headers: ["genrule_bar"],
4505 export_generated_headers: ["genrule_bar"],
4506 }
4507 `)
4508 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4509 checkIncludeDirs(t, ctx, foo,
4510 expectedIncludeDirs(`
4511 foo/standard
4512 .intermediates/genrule_foo/gen/generated_headers
4513 `),
4514 expectedSystemIncludeDirs(`foo/system`),
4515 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4516 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4517 )
4518
4519 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4520 checkIncludeDirs(t, ctx, bar,
4521 expectedIncludeDirs(`
4522 bar/standard
4523 .intermediates/genrule_bar/gen/generated_headers
4524 `),
4525 expectedSystemIncludeDirs(`bar/system`),
4526 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4527 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4528 )
4529 })
4530
4531 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4532 ctx := testCc(t, genRuleModules+`
4533 cc_library {
4534 name: "libfoo",
4535 srcs: ["foo.c"],
4536 export_include_dirs: ["foo/standard"],
4537 export_system_include_dirs: ["foo/system"],
4538 generated_headers: ["genrule_foo"],
4539 export_generated_headers: ["genrule_foo"],
4540 }
4541
4542 cc_library {
4543 name: "libbar",
4544 srcs: ["bar.c"],
4545 whole_static_libs: ["libfoo"],
4546 export_include_dirs: ["bar/standard"],
4547 export_system_include_dirs: ["bar/system"],
4548 generated_headers: ["genrule_bar"],
4549 export_generated_headers: ["genrule_bar"],
4550 }
4551 `)
4552 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4553 checkIncludeDirs(t, ctx, foo,
4554 expectedIncludeDirs(`
4555 foo/standard
4556 .intermediates/genrule_foo/gen/generated_headers
4557 `),
4558 expectedSystemIncludeDirs(`foo/system`),
4559 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4560 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4561 )
4562
4563 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4564 checkIncludeDirs(t, ctx, bar,
4565 expectedIncludeDirs(`
4566 bar/standard
4567 foo/standard
4568 .intermediates/genrule_foo/gen/generated_headers
4569 .intermediates/genrule_bar/gen/generated_headers
4570 `),
4571 expectedSystemIncludeDirs(`
4572 bar/system
4573 foo/system
4574 `),
4575 expectedGeneratedHeaders(`
4576 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4577 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4578 `),
4579 expectedOrderOnlyDeps(`
4580 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4581 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4582 `),
4583 )
4584 })
4585
Paul Duffin3cb603e2021-02-19 13:57:10 +00004586 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4587 ctx := testCc(t, genRuleModules+`
4588 cc_library_shared {
4589 name: "libfoo",
4590 srcs: [
4591 "foo.c",
4592 "b.aidl",
4593 "a.proto",
4594 ],
4595 aidl: {
4596 export_aidl_headers: true,
4597 }
4598 }
4599 `)
4600 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4601 checkIncludeDirs(t, ctx, foo,
4602 expectedIncludeDirs(`
4603 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4604 `),
4605 expectedSystemIncludeDirs(``),
4606 expectedGeneratedHeaders(`
4607 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4608 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4609 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004610 `),
4611 expectedOrderOnlyDeps(`
4612 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4613 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4614 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004615 `),
4616 )
4617 })
4618
Paul Duffin3cb603e2021-02-19 13:57:10 +00004619 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4620 ctx := testCc(t, genRuleModules+`
4621 cc_library_shared {
4622 name: "libfoo",
4623 srcs: [
4624 "foo.c",
4625 "b.aidl",
4626 "a.proto",
4627 ],
4628 proto: {
4629 export_proto_headers: true,
4630 }
4631 }
4632 `)
4633 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4634 checkIncludeDirs(t, ctx, foo,
4635 expectedIncludeDirs(`
4636 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4637 `),
4638 expectedSystemIncludeDirs(``),
4639 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004640 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4641 `),
4642 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004643 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4644 `),
4645 )
4646 })
4647
Paul Duffin33056e82021-02-19 13:49:08 +00004648 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004649 ctx := testCc(t, genRuleModules+`
4650 cc_library_shared {
4651 name: "libfoo",
4652 srcs: [
4653 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004654 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004655 "b.aidl",
4656 "a.proto",
4657 ],
4658 }
4659 `)
4660 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4661 checkIncludeDirs(t, ctx, foo,
4662 expectedIncludeDirs(`
4663 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4664 `),
4665 expectedSystemIncludeDirs(``),
4666 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004667 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004668 `),
4669 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004670 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4671 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004672 `),
4673 )
4674 })
4675}
Colin Crossae628182021-06-14 16:52:28 -07004676
4677func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004678 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004679 baseExpectedFlags := []string{
4680 "${config.ArmThumbCflags}",
4681 "${config.ArmCflags}",
4682 "${config.CommonGlobalCflags}",
4683 "${config.DeviceGlobalCflags}",
4684 "${config.ExternalCflags}",
4685 "${config.ArmToolchainCflags}",
4686 "${config.ArmArmv7ANeonCflags}",
4687 "${config.ArmGenericCflags}",
4688 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004689 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004690 }
4691
4692 expectedIncludes := []string{
4693 "external/foo/android_arm_export_include_dirs",
4694 "external/foo/lib32_export_include_dirs",
4695 "external/foo/arm_export_include_dirs",
4696 "external/foo/android_export_include_dirs",
4697 "external/foo/linux_export_include_dirs",
4698 "external/foo/export_include_dirs",
4699 "external/foo/android_arm_local_include_dirs",
4700 "external/foo/lib32_local_include_dirs",
4701 "external/foo/arm_local_include_dirs",
4702 "external/foo/android_local_include_dirs",
4703 "external/foo/linux_local_include_dirs",
4704 "external/foo/local_include_dirs",
4705 "external/foo",
4706 "external/foo/libheader1",
4707 "external/foo/libheader2",
4708 "external/foo/libwhole1",
4709 "external/foo/libwhole2",
4710 "external/foo/libstatic1",
4711 "external/foo/libstatic2",
4712 "external/foo/libshared1",
4713 "external/foo/libshared2",
4714 "external/foo/liblinux",
4715 "external/foo/libandroid",
4716 "external/foo/libarm",
4717 "external/foo/lib32",
4718 "external/foo/libandroid_arm",
4719 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004720 }
4721
4722 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4723 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4724
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004725 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004726 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004727 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004728
4729 lastIncludes := []string{
4730 "out/soong/ndk/sysroot/usr/include",
4731 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4732 }
4733
4734 combineSlices := func(slices ...[]string) []string {
4735 var ret []string
4736 for _, s := range slices {
4737 ret = append(ret, s...)
4738 }
4739 return ret
4740 }
4741
4742 testCases := []struct {
4743 name string
4744 src string
4745 expected []string
4746 }{
4747 {
4748 name: "c",
4749 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004750 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004751 },
4752 {
4753 name: "cc",
4754 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004755 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004756 },
4757 {
4758 name: "assemble",
4759 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004760 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004761 },
4762 }
4763
4764 for _, tc := range testCases {
4765 t.Run(tc.name, func(t *testing.T) {
4766 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004767 cc_library {
4768 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004769 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004770 cflags: ["-std=candcpp"],
4771 conlyflags: ["-std=conly"],
4772 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004773 local_include_dirs: ["local_include_dirs"],
4774 export_include_dirs: ["export_include_dirs"],
4775 export_system_include_dirs: ["export_system_include_dirs"],
4776 static_libs: ["libstatic1", "libstatic2"],
4777 whole_static_libs: ["libwhole1", "libwhole2"],
4778 shared_libs: ["libshared1", "libshared2"],
4779 header_libs: ["libheader1", "libheader2"],
4780 target: {
4781 android: {
4782 shared_libs: ["libandroid"],
4783 local_include_dirs: ["android_local_include_dirs"],
4784 export_include_dirs: ["android_export_include_dirs"],
4785 },
4786 android_arm: {
4787 shared_libs: ["libandroid_arm"],
4788 local_include_dirs: ["android_arm_local_include_dirs"],
4789 export_include_dirs: ["android_arm_export_include_dirs"],
4790 },
4791 linux: {
4792 shared_libs: ["liblinux"],
4793 local_include_dirs: ["linux_local_include_dirs"],
4794 export_include_dirs: ["linux_export_include_dirs"],
4795 },
4796 },
4797 multilib: {
4798 lib32: {
4799 shared_libs: ["lib32"],
4800 local_include_dirs: ["lib32_local_include_dirs"],
4801 export_include_dirs: ["lib32_export_include_dirs"],
4802 },
4803 },
4804 arch: {
4805 arm: {
4806 shared_libs: ["libarm"],
4807 local_include_dirs: ["arm_local_include_dirs"],
4808 export_include_dirs: ["arm_export_include_dirs"],
4809 },
4810 },
4811 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004812 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004813 }
4814
4815 cc_library_headers {
4816 name: "libheader1",
4817 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004818 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004819 stl: "none",
4820 }
4821
4822 cc_library_headers {
4823 name: "libheader2",
4824 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004825 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004826 stl: "none",
4827 }
Liz Kammer08572c62021-09-30 10:11:04 -04004828 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004829
Liz Kammer08572c62021-09-30 10:11:04 -04004830 libs := []string{
4831 "libstatic1",
4832 "libstatic2",
4833 "libwhole1",
4834 "libwhole2",
4835 "libshared1",
4836 "libshared2",
4837 "libandroid",
4838 "libandroid_arm",
4839 "liblinux",
4840 "lib32",
4841 "libarm",
4842 }
Colin Crossae628182021-06-14 16:52:28 -07004843
Liz Kammer08572c62021-09-30 10:11:04 -04004844 for _, lib := range libs {
4845 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004846 cc_library {
4847 name: "%s",
4848 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004849 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004850 stl: "none",
4851 }
4852 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004853 }
4854
4855 ctx := android.GroupFixturePreparers(
4856 PrepareForIntegrationTestWithCc,
4857 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4858 ).RunTest(t)
4859 // Use the arm variant instead of the arm64 variant so that it gets headers from
4860 // ndk_libandroid_support to test LateStaticLibs.
4861 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4862
4863 var includes []string
4864 flags := strings.Split(cflags, " ")
4865 for _, flag := range flags {
4866 if strings.HasPrefix(flag, "-I") {
4867 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4868 } else if flag == "-isystem" {
4869 // skip isystem, include next
4870 } else if len(flag) > 0 {
4871 includes = append(includes, flag)
4872 }
4873 }
4874
4875 android.AssertArrayString(t, "includes", tc.expected, includes)
4876 })
Colin Crossae628182021-06-14 16:52:28 -07004877 }
4878
Colin Crossae628182021-06-14 16:52:28 -07004879}
Alixb5f6d9e2022-04-20 23:00:58 +00004880
zijunzhao933e3802023-01-12 07:26:20 +00004881func TestAddnoOverride64GlobalCflags(t *testing.T) {
4882 t.Parallel()
4883 ctx := testCc(t, `
4884 cc_library_shared {
4885 name: "libclient",
4886 srcs: ["foo.c"],
4887 shared_libs: ["libfoo#1"],
4888 }
4889
4890 cc_library_shared {
4891 name: "libfoo",
4892 srcs: ["foo.c"],
4893 shared_libs: ["libbar"],
4894 export_shared_lib_headers: ["libbar"],
4895 stubs: {
4896 symbol_file: "foo.map.txt",
4897 versions: ["1", "2", "3"],
4898 },
4899 }
4900
4901 cc_library_shared {
4902 name: "libbar",
4903 export_include_dirs: ["include/libbar"],
4904 srcs: ["foo.c"],
4905 }`)
4906
4907 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4908
4909 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4910 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4911 }
4912}
4913
Alixb5f6d9e2022-04-20 23:00:58 +00004914func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004915 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004916 tests := []struct {
4917 name string
4918 clang bool
4919 BuildBrokenClangProperty bool
4920 err string
4921 }{
4922 {
4923 name: "error when clang is set to false",
4924 clang: false,
4925 err: "is no longer supported",
4926 },
4927 {
4928 name: "error when clang is set to true",
4929 clang: true,
4930 err: "property is deprecated, see Changes.md",
4931 },
4932 {
4933 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4934 clang: true,
4935 BuildBrokenClangProperty: true,
4936 },
4937 }
4938
4939 for _, test := range tests {
4940 t.Run(test.name, func(t *testing.T) {
4941 bp := fmt.Sprintf(`
4942 cc_library {
4943 name: "foo",
4944 clang: %t,
4945 }`, test.clang)
4946
4947 if test.err == "" {
4948 android.GroupFixturePreparers(
4949 prepareForCcTest,
4950 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4951 if test.BuildBrokenClangProperty {
4952 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4953 }
4954 }),
4955 ).RunTestWithBp(t, bp)
4956 } else {
4957 prepareForCcTest.
4958 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4959 RunTestWithBp(t, bp)
4960 }
4961 })
4962 }
4963}
Alix Espinoef47e542022-09-14 19:10:51 +00004964
4965func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004966 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004967 tests := []struct {
4968 name string
4969 clangAsFlags []string
4970 BuildBrokenClangAsFlags bool
4971 err string
4972 }{
4973 {
4974 name: "error when clang_asflags is set",
4975 clangAsFlags: []string{"-a", "-b"},
4976 err: "clang_asflags: property is deprecated",
4977 },
4978 {
4979 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4980 clangAsFlags: []string{"-a", "-b"},
4981 BuildBrokenClangAsFlags: true,
4982 },
4983 }
4984
4985 for _, test := range tests {
4986 t.Run(test.name, func(t *testing.T) {
4987 bp := fmt.Sprintf(`
4988 cc_library {
4989 name: "foo",
4990 clang_asflags: %s,
4991 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4992
4993 if test.err == "" {
4994 android.GroupFixturePreparers(
4995 prepareForCcTest,
4996 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4997 if test.BuildBrokenClangAsFlags {
4998 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4999 }
5000 }),
5001 ).RunTestWithBp(t, bp)
5002 } else {
5003 prepareForCcTest.
5004 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5005 RunTestWithBp(t, bp)
5006 }
5007 })
5008 }
5009}
5010
5011func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005012 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005013 tests := []struct {
5014 name string
5015 clangCFlags []string
5016 BuildBrokenClangCFlags bool
5017 err string
5018 }{
5019 {
5020 name: "error when clang_cflags is set",
5021 clangCFlags: []string{"-a", "-b"},
5022 err: "clang_cflags: property is deprecated",
5023 },
5024 {
5025 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5026 clangCFlags: []string{"-a", "-b"},
5027 BuildBrokenClangCFlags: true,
5028 },
5029 }
5030
5031 for _, test := range tests {
5032 t.Run(test.name, func(t *testing.T) {
5033 bp := fmt.Sprintf(`
5034 cc_library {
5035 name: "foo",
5036 clang_cflags: %s,
5037 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5038
5039 if test.err == "" {
5040 android.GroupFixturePreparers(
5041 prepareForCcTest,
5042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5043 if test.BuildBrokenClangCFlags {
5044 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5045 }
5046 }),
5047 ).RunTestWithBp(t, bp)
5048 } else {
5049 prepareForCcTest.
5050 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5051 RunTestWithBp(t, bp)
5052 }
5053 })
5054 }
5055}
Yu Liue4312402023-01-18 09:15:31 -08005056
5057func TestDclaLibraryInApex(t *testing.T) {
5058 t.Parallel()
5059 bp := `
5060 cc_library_shared {
5061 name: "cc_lib_in_apex",
5062 srcs: ["foo.cc"],
5063 apex_available: ["myapex"],
5064 bazel_module: { label: "//foo/bar:bar" },
5065 }`
5066 label := "//foo/bar:bar"
5067 arch64 := "arm64_armv8-a"
5068 arch32 := "arm_armv7-a-neon"
5069 apexCfgKey := android.ApexConfigKey{
5070 WithinApex: true,
5071 ApexSdkVersion: "28",
5072 }
5073
5074 result := android.GroupFixturePreparers(
5075 prepareForCcTest,
5076 android.FixtureRegisterWithContext(registerTestMutators),
5077 android.FixtureModifyConfig(func(config android.Config) {
5078 config.BazelContext = android.MockBazelContext{
5079 OutputBaseDir: "outputbase",
5080 LabelToCcInfo: map[string]cquery.CcInfo{
5081 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5082 RootDynamicLibraries: []string{"foo.so"},
5083 },
5084 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5085 RootDynamicLibraries: []string{"foo.so"},
5086 },
5087 },
5088 BazelRequests: make(map[string]bool),
5089 }
5090 }),
5091 ).RunTestWithBp(t, bp)
5092 ctx := result.TestContext
5093
5094 // Test if the bazel request is queued correctly
5095 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5096 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5097 t.Errorf("Bazel request was not queued: %s", key)
5098 }
5099
5100 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5101 producer := sharedFoo.(android.OutputFileProducer)
5102 outputFiles, err := producer.OutputFiles("")
5103 if err != nil {
5104 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5105 }
5106 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5107 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5108}
Sam Delmericoef69d472023-04-18 17:32:43 -04005109
5110func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5111 t.Parallel()
5112 bp := `
5113 cc_library_static {
5114 name: "foo_ubsan_minimal",
5115 srcs: ["foo.cc"],
5116 bazel_module: { label: "//foo_ubsan_minimal" },
5117 sanitize: {
5118 all_undefined: true,
5119 integer_overflow: true,
5120 },
5121 }
5122 cc_library_static {
5123 name: "foo",
5124 srcs: ["foo.cc"],
5125 bazel_module: { label: "//foo" },
5126 sanitize: {
5127 address: true,
5128 hwaddress: true,
5129 fuzzer: true,
5130 integer_overflow: true,
5131 scs: true,
5132 },
5133 }
5134 cc_library_static {
5135 name: "foo_tsan",
5136 srcs: ["foo.cc"],
5137 bazel_module: { label: "//foo_tsan" },
5138 sanitize: {
5139 thread: true,
5140 },
5141 }
5142 cc_library_static {
5143 name: "foo_cfi",
5144 srcs: ["foo.cc"],
5145 bazel_module: { label: "//foo_cfi" },
5146 sanitize: {
5147 cfi: true,
5148 },
5149 }
5150 cc_library_static {
5151 name: "foo_memtag_stack",
5152 srcs: ["foo.cc"],
5153 bazel_module: { label: "//foo_memtag_stack" },
5154 sanitize: {
5155 memtag_stack: true,
5156 },
5157 }
5158 cc_library_static {
5159 name: "foo_memtag_heap",
5160 srcs: ["foo.cc"],
5161 bazel_module: { label: "//foo_memtag_heap" },
5162 sanitize: {
5163 memtag_heap: true,
5164 },
5165 }
5166 cc_library_static {
5167 name: "foo_safestack",
5168 srcs: ["foo.cc"],
5169 bazel_module: { label: "//foo_safestack" },
5170 sanitize: {
5171 safestack: true,
5172 },
5173 }
5174 cc_library_static {
5175 name: "foo_scudo",
5176 srcs: ["foo.cc"],
5177 bazel_module: { label: "//foo_scudo" },
5178 sanitize: {
5179 scudo: true,
5180 },
5181 }
5182 `
5183 testcases := []struct {
5184 name string
5185 variant string
5186 expectedOutputPaths []string
5187 }{
5188 {
5189 name: "foo_ubsan_minimal",
5190 variant: "android_arm64_armv8-a_static_apex28",
5191 expectedOutputPaths: []string{
5192 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5193 },
5194 },
5195 {
5196 name: "foo",
5197 variant: "android_arm64_armv8-a_static_apex28",
5198 expectedOutputPaths: []string{
5199 "outputbase/execroot/__main__/foo.a",
5200 },
5201 },
5202 {
5203 name: "foo",
5204 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5205 expectedOutputPaths: []string{
5206 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5207 },
5208 },
5209 {
5210 name: "foo",
5211 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5212 expectedOutputPaths: []string{
5213 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5214 },
5215 },
5216 {
5217 name: "foo",
5218 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5219 expectedOutputPaths: []string{
5220 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5221 },
5222 },
5223 {
5224 name: "foo",
5225 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5226 expectedOutputPaths: []string{
5227 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5228 },
5229 },
5230 {
5231 name: "foo",
5232 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5233 expectedOutputPaths: []string{
5234 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5235 },
5236 },
5237 {
5238 name: "foo",
5239 variant: "android_arm64_armv8-a_static_scs_apex28",
5240 expectedOutputPaths: []string{
5241 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5242 },
5243 },
5244 {
5245 name: "foo",
5246 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5247 expectedOutputPaths: []string{
5248 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5249 },
5250 },
5251 {
5252 name: "foo",
5253 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5254 expectedOutputPaths: []string{
5255 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5256 },
5257 },
5258 {
5259 name: "foo_tsan",
5260 variant: "android_arm64_armv8-a_static_apex28",
5261 expectedOutputPaths: []string{
5262 "outputbase/execroot/__main__/foo_tsan.a",
5263 },
5264 },
5265 {
5266 name: "foo_tsan",
5267 variant: "android_arm64_armv8-a_static_tsan_apex28",
5268 expectedOutputPaths: []string{
5269 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5270 },
5271 },
5272 {
5273 name: "foo_cfi",
5274 variant: "android_arm64_armv8-a_static_apex28",
5275 expectedOutputPaths: []string{
5276 "outputbase/execroot/__main__/foo_cfi.a",
5277 },
5278 },
5279 {
5280 name: "foo_cfi",
5281 variant: "android_arm64_armv8-a_static_cfi_apex28",
5282 expectedOutputPaths: []string{
5283 "out/soong/.intermediates/foo_cfi/android_arm64_armv8-a_static_cfi_apex28/foo_cfi.a",
5284 },
5285 },
5286 {
5287 name: "foo_memtag_stack",
5288 variant: "android_arm64_armv8-a_static_apex28",
5289 expectedOutputPaths: []string{
5290 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5291 },
5292 },
5293 {
5294 name: "foo_memtag_heap",
5295 variant: "android_arm64_armv8-a_static_apex28",
5296 expectedOutputPaths: []string{
5297 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5298 },
5299 },
5300 {
5301 name: "foo_safestack",
5302 variant: "android_arm64_armv8-a_static_apex28",
5303 expectedOutputPaths: []string{
5304 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5305 },
5306 },
5307 {
5308 name: "foo_scudo",
5309 variant: "android_arm64_armv8-a_static_apex28",
5310 expectedOutputPaths: []string{
5311 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5312 },
5313 },
5314 }
5315
5316 ctx := android.GroupFixturePreparers(
5317 prepareForCcTest,
5318 prepareForAsanTest,
5319 android.FixtureRegisterWithContext(registerTestMutators),
5320 android.FixtureModifyConfig(func(config android.Config) {
5321 config.BazelContext = android.MockBazelContext{
5322 OutputBaseDir: "outputbase",
5323 LabelToCcInfo: map[string]cquery.CcInfo{
5324 "//foo_ubsan_minimal": {
5325 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5326 },
5327 "//foo": {
5328 RootStaticArchives: []string{"foo.a"},
5329 },
5330 "//foo_tsan": {
5331 RootStaticArchives: []string{"foo_tsan.a"},
5332 },
5333 "//foo_cfi": {
5334 RootStaticArchives: []string{"foo_cfi.a"},
5335 },
5336 "//foo_memtag_stack": {
5337 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5338 },
5339 "//foo_memtag_heap": {
5340 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5341 },
5342 "//foo_safestack": {
5343 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5344 },
5345 "//foo_scudo": {
5346 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5347 },
5348 },
5349 }
5350 }),
5351 ).RunTestWithBp(t, bp).TestContext
5352
5353 for _, tc := range testcases {
5354 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5355 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5356 if err != nil {
5357 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5358 }
5359 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5360 }
5361}