blob: 0d03b73e9f528346f68bacdc905ee196cca411ca [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,
3117 }
3118 cc_binary {
3119 name: "binary",
3120 bazel_module: { label: "//:binary" },
3121 static_libs: ["static_dep"],
3122 whole_static_libs: ["whole_static_dep"],
3123 shared_libs: ["shared_dep"],
3124 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003125 cc_library_headers {
3126 name: "lib_headers",
3127 bazel_module: { label: "//:lib_headers" },
3128 static_libs: ["static_dep"],
3129 whole_static_libs: ["whole_static_dep"],
3130 shared_libs: ["shared_dep"],
3131 }
3132 cc_prebuilt_library {
3133 name: "lib_prebuilt",
3134 bazel_module: { label: "//:lib_prebuilt" },
3135 static_libs: ["static_dep"],
3136 whole_static_libs: ["whole_static_dep"],
3137 shared_libs: ["shared_dep"],
3138 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003139 `
3140
3141 testCases := []struct {
3142 name string
3143 moduleName string
3144 variant string
3145 androidMkInfo cquery.CcAndroidMkInfo
3146 }{
3147 {
3148 name: "shared lib",
3149 moduleName: "lib",
3150 variant: "android_arm64_armv8-a_shared",
3151 androidMkInfo: cquery.CcAndroidMkInfo{
3152 LocalStaticLibs: []string{"static_dep"},
3153 LocalWholeStaticLibs: []string{"whole_static_dep"},
3154 LocalSharedLibs: []string{"shared_dep"},
3155 },
3156 },
3157 {
3158 name: "static lib",
3159 moduleName: "lib",
3160 variant: "android_arm64_armv8-a_static",
3161 androidMkInfo: cquery.CcAndroidMkInfo{
3162 LocalStaticLibs: []string{"static_dep"},
3163 LocalWholeStaticLibs: []string{"whole_static_dep"},
3164 LocalSharedLibs: []string{"shared_dep"},
3165 },
3166 },
3167 {
3168 name: "cc_test arm64",
3169 moduleName: "test",
3170 variant: "android_arm64_armv8-a",
3171 androidMkInfo: cquery.CcAndroidMkInfo{
3172 LocalStaticLibs: []string{"static_dep"},
3173 LocalWholeStaticLibs: []string{"whole_static_dep"},
3174 LocalSharedLibs: []string{"shared_dep"},
3175 },
3176 },
3177 {
3178 name: "cc_test arm",
3179 moduleName: "test",
3180 variant: "android_arm_armv7-a-neon",
3181 androidMkInfo: cquery.CcAndroidMkInfo{
3182 LocalStaticLibs: []string{"static_dep"},
3183 LocalWholeStaticLibs: []string{"whole_static_dep"},
3184 LocalSharedLibs: []string{"shared_dep"},
3185 },
3186 },
3187 {
3188 name: "cc_binary",
3189 moduleName: "binary",
3190 variant: "android_arm64_armv8-a",
3191 androidMkInfo: cquery.CcAndroidMkInfo{
3192 LocalStaticLibs: []string{"static_dep"},
3193 LocalWholeStaticLibs: []string{"whole_static_dep"},
3194 LocalSharedLibs: []string{"shared_dep"},
3195 },
3196 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003197 {
3198 name: "cc_library_headers",
3199 moduleName: "lib_headers",
3200 variant: "android_arm64_armv8-a",
3201 androidMkInfo: cquery.CcAndroidMkInfo{
3202 LocalStaticLibs: []string{"static_dep"},
3203 LocalWholeStaticLibs: []string{"whole_static_dep"},
3204 LocalSharedLibs: []string{"shared_dep"},
3205 },
3206 },
3207 {
3208 name: "prebuilt lib static",
3209 moduleName: "lib_prebuilt",
3210 variant: "android_arm64_armv8-a_static",
3211 androidMkInfo: cquery.CcAndroidMkInfo{
3212 LocalStaticLibs: []string{"static_dep"},
3213 LocalWholeStaticLibs: []string{"whole_static_dep"},
3214 LocalSharedLibs: []string{"shared_dep"},
3215 },
3216 },
3217 {
3218 name: "prebuilt lib shared",
3219 moduleName: "lib_prebuilt",
3220 variant: "android_arm64_armv8-a_shared",
3221 androidMkInfo: cquery.CcAndroidMkInfo{
3222 LocalStaticLibs: []string{"static_dep"},
3223 LocalWholeStaticLibs: []string{"whole_static_dep"},
3224 LocalSharedLibs: []string{"shared_dep"},
3225 },
3226 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003227 }
3228
3229 outputBaseDir := "out/bazel"
3230 for _, tc := range testCases {
3231 t.Run(tc.name, func(t *testing.T) {
3232 result := android.GroupFixturePreparers(
3233 prepareForCcTest,
3234 android.FixtureModifyConfig(func(config android.Config) {
3235 config.BazelContext = android.MockBazelContext{
3236 OutputBaseDir: outputBaseDir,
3237 LabelToCcInfo: map[string]cquery.CcInfo{
3238 "//:lib": cquery.CcInfo{
3239 CcAndroidMkInfo: tc.androidMkInfo,
3240 RootDynamicLibraries: []string{""},
3241 },
3242 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3243 CcAndroidMkInfo: tc.androidMkInfo,
3244 RootStaticArchives: []string{""},
3245 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003246 "//:lib_headers": cquery.CcInfo{
3247 CcAndroidMkInfo: tc.androidMkInfo,
3248 OutputFiles: []string{""},
3249 },
3250 "//:lib_prebuilt": cquery.CcInfo{
3251 CcAndroidMkInfo: tc.androidMkInfo,
3252 },
3253 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3254 CcAndroidMkInfo: tc.androidMkInfo,
3255 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003256 },
3257 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3258 "//:test": cquery.CcUnstrippedInfo{
3259 CcAndroidMkInfo: tc.androidMkInfo,
3260 },
3261 "//:binary": cquery.CcUnstrippedInfo{
3262 CcAndroidMkInfo: tc.androidMkInfo,
3263 },
3264 },
3265 }
3266 }),
3267 ).RunTestWithBp(t, bp)
3268 ctx := result.TestContext
3269
3270 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3271 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003272 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3273 t.Errorf("incorrect static_libs"+
3274 "\nactual: %v"+
3275 "\nexpected: %v",
3276 module.Properties.AndroidMkStaticLibs,
3277 tc.androidMkInfo.LocalStaticLibs,
3278 )
3279 }
3280 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3281 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3282 tc.androidMkInfo.LocalStaticLibs,
3283 )
3284 if staticDepsDiffer {
3285 t.Errorf(
3286 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3287 tc.androidMkInfo.LocalStaticLibs,
3288 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3289 missingStaticDeps,
3290 additionalStaticDeps,
3291 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003292 }
3293
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003294 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3295 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3296 tc.androidMkInfo.LocalWholeStaticLibs,
3297 module.Properties.AndroidMkWholeStaticLibs,
3298 )
3299 }
3300 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3301 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3302 tc.androidMkInfo.LocalWholeStaticLibs,
3303 )
3304 if wholeStaticDepsDiffer {
3305 t.Errorf(
3306 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3307 tc.androidMkInfo.LocalWholeStaticLibs,
3308 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3309 missingWholeStaticDeps,
3310 additionalWholeStaticDeps,
3311 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003312 }
3313
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003314 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3315 t.Errorf("incorrect shared_libs"+
3316 "\nactual: %v"+
3317 "\nexpected: %v",
3318 module.Properties.AndroidMkSharedLibs,
3319 tc.androidMkInfo.LocalSharedLibs,
3320 )
3321 }
3322 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3323 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3324 tc.androidMkInfo.LocalSharedLibs,
3325 )
3326 if sharedDepsDiffer {
3327 t.Errorf(
3328 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3329 tc.androidMkInfo.LocalSharedLibs,
3330 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3331 missingSharedDeps,
3332 additionalSharedDeps,
3333 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003334 }
3335 })
3336 }
3337}
3338
Jiyong Parkd08b6972017-09-26 10:50:54 +09003339var compilerFlagsTestCases = []struct {
3340 in string
3341 out bool
3342}{
3343 {
3344 in: "a",
3345 out: false,
3346 },
3347 {
3348 in: "-a",
3349 out: true,
3350 },
3351 {
3352 in: "-Ipath/to/something",
3353 out: false,
3354 },
3355 {
3356 in: "-isystempath/to/something",
3357 out: false,
3358 },
3359 {
3360 in: "--coverage",
3361 out: false,
3362 },
3363 {
3364 in: "-include a/b",
3365 out: true,
3366 },
3367 {
3368 in: "-include a/b c/d",
3369 out: false,
3370 },
3371 {
3372 in: "-DMACRO",
3373 out: true,
3374 },
3375 {
3376 in: "-DMAC RO",
3377 out: false,
3378 },
3379 {
3380 in: "-a -b",
3381 out: false,
3382 },
3383 {
3384 in: "-DMACRO=definition",
3385 out: true,
3386 },
3387 {
3388 in: "-DMACRO=defi nition",
3389 out: true, // TODO(jiyong): this should be false
3390 },
3391 {
3392 in: "-DMACRO(x)=x + 1",
3393 out: true,
3394 },
3395 {
3396 in: "-DMACRO=\"defi nition\"",
3397 out: true,
3398 },
3399}
3400
3401type mockContext struct {
3402 BaseModuleContext
3403 result bool
3404}
3405
3406func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3407 // CheckBadCompilerFlags calls this function when the flag should be rejected
3408 ctx.result = false
3409}
3410
3411func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003412 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003413 for _, testCase := range compilerFlagsTestCases {
3414 ctx := &mockContext{result: true}
3415 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3416 if ctx.result != testCase.out {
3417 t.Errorf("incorrect output:")
3418 t.Errorf(" input: %#v", testCase.in)
3419 t.Errorf(" expected: %#v", testCase.out)
3420 t.Errorf(" got: %#v", ctx.result)
3421 }
3422 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003423}
Jiyong Park374510b2018-03-19 18:23:01 +09003424
Jiyong Park37b25202018-07-11 10:49:27 +09003425func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003426 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003427 ctx := testCc(t, `
3428 cc_library_shared {
3429 name: "librecovery",
3430 recovery: true,
3431 }
3432 cc_library_shared {
3433 name: "librecovery32",
3434 recovery: true,
3435 compile_multilib:"32",
3436 }
Jiyong Park5baac542018-08-28 09:55:37 +09003437 cc_library_shared {
3438 name: "libHalInRecovery",
3439 recovery_available: true,
3440 vendor: true,
3441 }
Jiyong Park37b25202018-07-11 10:49:27 +09003442 `)
3443
3444 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003445 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003446 if len(variants) != 1 || !android.InList(arm64, variants) {
3447 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3448 }
3449
3450 variants = ctx.ModuleVariantsForTests("librecovery32")
3451 if android.InList(arm64, variants) {
3452 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3453 }
Jiyong Park5baac542018-08-28 09:55:37 +09003454
3455 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3456 if !recoveryModule.Platform() {
3457 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3458 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003459}
Jiyong Park5baac542018-08-28 09:55:37 +09003460
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003461func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003462 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003463 bp := `
3464 cc_prebuilt_test_library_shared {
3465 name: "test_lib",
3466 relative_install_path: "foo/bar/baz",
3467 srcs: ["srcpath/dontusethispath/baz.so"],
3468 }
3469
3470 cc_test {
3471 name: "main_test",
3472 data_libs: ["test_lib"],
3473 gtest: false,
3474 }
3475 `
3476
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003477 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003478 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003479 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003480 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3481
3482 ctx := testCcWithConfig(t, config)
3483 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3484 testBinary := module.(*Module).linker.(*testBinary)
3485 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3486 if err != nil {
3487 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3488 }
3489 if len(outputFiles) != 1 {
3490 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3491 }
3492 if len(testBinary.dataPaths()) != 1 {
3493 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3494 }
3495
3496 outputPath := outputFiles[0].String()
3497
3498 if !strings.HasSuffix(outputPath, "/main_test") {
3499 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3500 }
Colin Crossaa255532020-07-03 13:18:24 -07003501 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003502 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3503 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3504 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3505 }
3506}
3507
Jiyong Park7ed9de32018-10-15 22:25:07 +09003508func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003509 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003510 ctx := testCc(t, `
3511 cc_library_shared {
3512 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003513 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003514 stubs: {
3515 symbol_file: "foo.map.txt",
3516 versions: ["1", "2", "3"],
3517 },
3518 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003519
Jiyong Park7ed9de32018-10-15 22:25:07 +09003520 cc_library_shared {
3521 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003522 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003523 shared_libs: ["libFoo#1"],
3524 }`)
3525
3526 variants := ctx.ModuleVariantsForTests("libFoo")
3527 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003528 "android_arm64_armv8-a_shared",
3529 "android_arm64_armv8-a_shared_1",
3530 "android_arm64_armv8-a_shared_2",
3531 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003532 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003533 "android_arm_armv7-a-neon_shared",
3534 "android_arm_armv7-a-neon_shared_1",
3535 "android_arm_armv7-a-neon_shared_2",
3536 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003537 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003538 }
3539 variantsMismatch := false
3540 if len(variants) != len(expectedVariants) {
3541 variantsMismatch = true
3542 } else {
3543 for _, v := range expectedVariants {
3544 if !inList(v, variants) {
3545 variantsMismatch = false
3546 }
3547 }
3548 }
3549 if variantsMismatch {
3550 t.Errorf("variants of libFoo expected:\n")
3551 for _, v := range expectedVariants {
3552 t.Errorf("%q\n", v)
3553 }
3554 t.Errorf(", but got:\n")
3555 for _, v := range variants {
3556 t.Errorf("%q\n", v)
3557 }
3558 }
3559
Colin Cross7113d202019-11-20 16:39:12 -08003560 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003561 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003562 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003563 if !strings.Contains(libFlags, libFoo1StubPath) {
3564 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3565 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003566
Colin Cross7113d202019-11-20 16:39:12 -08003567 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003568 cFlags := libBarCompileRule.Args["cFlags"]
3569 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3570 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3571 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3572 }
Jiyong Park37b25202018-07-11 10:49:27 +09003573}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003574
Jooyung Hanb04a4992020-03-13 18:57:35 +09003575func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003576 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003577 for _, tc := range []struct{ moduleName, expected string }{
3578 {"libc", "__LIBC_API__"},
3579 {"libfoo", "__LIBFOO_API__"},
3580 {"libfoo@1", "__LIBFOO_1_API__"},
3581 {"libfoo-v1", "__LIBFOO_V1_API__"},
3582 {"libfoo.v1", "__LIBFOO_V1_API__"},
3583 } {
3584 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3585 }
3586}
3587
Liz Kammer83cf81b2022-09-22 08:24:20 -04003588func pathsToBase(paths android.Paths) []string {
3589 var ret []string
3590 for _, p := range paths {
3591 ret = append(ret, p.Base())
3592 }
3593 return ret
3594}
3595
3596func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003597 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003598 ctx := testCc(t, `
3599 cc_library_static {
3600 name: "foo",
3601 srcs: ["foo.c"],
3602 }
3603
3604 cc_library_static {
3605 name: "bar",
3606 srcs: ["bar.c"],
3607 }
3608
3609 cc_library_shared {
3610 name: "qux",
3611 srcs: ["qux.c"],
3612 }
3613
3614 cc_library_static {
3615 name: "baz",
3616 srcs: ["baz.c"],
3617 static_libs: ["foo"],
3618 shared_libs: ["qux"],
3619 whole_static_libs: ["bar"],
3620 }`)
3621
3622 variant := "android_arm64_armv8-a_static"
3623 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3624
3625 // For static libraries, the object files of a whole static dep are included in the archive
3626 // directly
3627 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3628 t.Errorf("Expected input objects %q, got %q", w, g)
3629 }
3630
3631 // non whole static dependencies are not linked into the archive
3632 if len(arRule.Implicits) > 0 {
3633 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3634 }
3635}
3636
3637func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003638 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003639 ctx := testCc(t, `
3640 cc_library_static {
3641 name: "foo",
3642 srcs: ["foo.c"],
3643 }
3644
3645 cc_library_static {
3646 name: "bar",
3647 srcs: ["bar.c"],
3648 }
3649
3650 cc_library_shared {
3651 name: "qux",
3652 srcs: ["qux.c"],
3653 }
3654
3655 cc_library_shared {
3656 name: "baz",
3657 srcs: ["baz.c"],
3658 static_libs: ["foo"],
3659 shared_libs: ["qux"],
3660 whole_static_libs: ["bar"],
3661 }`)
3662
3663 variant := "android_arm64_armv8-a_shared"
3664 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3665 libFlags := linkRule.Args["libFlags"]
3666 // When dynamically linking, we expect static dependencies to be found on the command line
3667 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3668 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3669 }
3670 // When dynamically linking, we expect whole static dependencies to be found on the command line
3671 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3672 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3673 }
3674
3675 // When dynamically linking, we expect shared dependencies to be found on the command line
3676 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3677 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3678 }
3679
3680 // We should only have the objects from the shared library srcs, not the whole static dependencies
3681 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3682 t.Errorf("Expected input objects %q, got %q", w, g)
3683 }
3684}
3685
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003686func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003687 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003688 ctx := testCc(t, `
3689 cc_binary {
3690 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003691 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003692 static_executable: true,
3693 }`)
3694
Colin Cross7113d202019-11-20 16:39:12 -08003695 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003696 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3697 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003698 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003699 for _, lib := range systemStaticLibs {
3700 if !strings.Contains(libFlags, lib) {
3701 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3702 }
3703 }
3704 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3705 for _, lib := range systemSharedLibs {
3706 if strings.Contains(libFlags, lib) {
3707 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3708 }
3709 }
3710}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003711
3712func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003713 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003714 ctx := testCc(t, `
3715 cc_binary {
3716 name: "mybin",
3717 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003718 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003719 static_executable: true,
3720 stl: "none",
3721 }
3722
3723 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003724 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003725 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003726 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003727 stl: "none",
3728 }
3729
3730 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003731 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003732 srcs: ["foo.c"],
3733 stl: "none",
3734 stubs: {
3735 versions: ["1"],
3736 },
3737 }`)
3738
Colin Cross0de8a1e2020-09-18 14:15:30 -07003739 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3740 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003741 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003742
3743 if !reflect.DeepEqual(actual, expected) {
3744 t.Errorf("staticDeps orderings were not propagated correctly"+
3745 "\nactual: %v"+
3746 "\nexpected: %v",
3747 actual,
3748 expected,
3749 )
3750 }
3751}
Jooyung Han38002912019-05-16 04:01:54 +09003752
Jooyung Hand48f3c32019-08-23 11:18:57 +09003753func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003754 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003755 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3756 cc_library {
3757 name: "libA",
3758 srcs: ["foo.c"],
3759 shared_libs: ["libB"],
3760 stl: "none",
3761 }
3762
3763 cc_library {
3764 name: "libB",
3765 srcs: ["foo.c"],
3766 enabled: false,
3767 stl: "none",
3768 }
3769 `)
3770}
3771
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003772func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3773 bp := `
3774 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003775 name: "test_afl_fuzz_target",
3776 srcs: ["foo.c"],
3777 host_supported: true,
3778 static_libs: [
3779 "afl_fuzz_static_lib",
3780 ],
3781 shared_libs: [
3782 "afl_fuzz_shared_lib",
3783 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003784 fuzzing_frameworks: {
3785 afl: true,
3786 libfuzzer: false,
3787 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003788 }
3789 cc_library {
3790 name: "afl_fuzz_static_lib",
3791 host_supported: true,
3792 srcs: ["static_file.c"],
3793 }
3794 cc_library {
3795 name: "libfuzzer_only_static_lib",
3796 host_supported: true,
3797 srcs: ["static_file.c"],
3798 }
3799 cc_library {
3800 name: "afl_fuzz_shared_lib",
3801 host_supported: true,
3802 srcs: ["shared_file.c"],
3803 static_libs: [
3804 "second_static_lib",
3805 ],
3806 }
3807 cc_library_headers {
3808 name: "libafl_headers",
3809 vendor_available: true,
3810 host_supported: true,
3811 export_include_dirs: [
3812 "include",
3813 "instrumentation",
3814 ],
3815 }
3816 cc_object {
3817 name: "afl-compiler-rt",
3818 vendor_available: true,
3819 host_supported: true,
3820 cflags: [
3821 "-fPIC",
3822 ],
3823 srcs: [
3824 "instrumentation/afl-compiler-rt.o.c",
3825 ],
3826 }
3827 cc_library {
3828 name: "second_static_lib",
3829 host_supported: true,
3830 srcs: ["second_file.c"],
3831 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003832 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003833 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003834 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003835 srcs: [
3836 "aflpp_driver.c",
3837 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003838 }`
3839
3840 testEnv := map[string]string{
3841 "FUZZ_FRAMEWORK": "AFL",
3842 }
3843
3844 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003845
3846 checkPcGuardFlag := func(
3847 modName string, variantName string, shouldHave bool) {
3848 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3849
3850 cFlags, ok := cc.Args["cFlags"]
3851 if !ok {
3852 t.Errorf("Could not find cFlags for module %s and variant %s",
3853 modName, variantName)
3854 }
3855
3856 if strings.Contains(
3857 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3858 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3859 "Test failed for module %s and variant %s",
3860 !shouldHave, shouldHave, modName, variantName)
3861 }
3862 }
3863
Cory Barkera1da26f2022-06-07 20:12:06 +00003864 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003865 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003866
3867 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003868 checkPcGuardFlag(moduleName, variant+"_static", false)
3869 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003870
3871 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003872 checkPcGuardFlag(moduleName, variant+"_static", false)
3873 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003874
3875 ctx.ModuleForTests("afl_fuzz_shared_lib",
3876 "android_arm64_armv8-a_shared").Rule("cc")
3877 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003878 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3879}
3880
3881func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003882 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003883 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3884}
3885
3886func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003887 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003888 if runtime.GOOS != "linux" {
3889 t.Skip("requires linux")
3890 }
3891
3892 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003893}
3894
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003895// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3896// correctly.
3897func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003898 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003899 ctx := testCc(t, `
3900 cc_fuzz {
3901 name: "fuzz_smoke_test",
3902 srcs: ["foo.c"],
3903 }`)
3904
Paul Duffin075c4172019-12-19 19:06:13 +00003905 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003906 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3907}
3908
Jooyung Han38002912019-05-16 04:01:54 +09003909func assertString(t *testing.T, got, expected string) {
3910 t.Helper()
3911 if got != expected {
3912 t.Errorf("expected %q got %q", expected, got)
3913 }
3914}
3915
3916func assertArrayString(t *testing.T, got, expected []string) {
3917 t.Helper()
3918 if len(got) != len(expected) {
3919 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3920 return
3921 }
3922 for i := range got {
3923 if got[i] != expected[i] {
3924 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3925 i, expected[i], expected, got[i], got)
3926 return
3927 }
3928 }
3929}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003930
Jooyung Han0302a842019-10-30 18:43:49 +09003931func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3932 t.Helper()
3933 assertArrayString(t, android.SortedStringKeys(m), expected)
3934}
3935
Colin Crosse1bb5d02019-09-24 14:55:04 -07003936func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003937 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003938 ctx := testCc(t, `
3939 cc_defaults {
3940 name: "defaults",
3941 srcs: ["foo.c"],
3942 static: {
3943 srcs: ["bar.c"],
3944 },
3945 shared: {
3946 srcs: ["baz.c"],
3947 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003948 bazel_module: {
3949 bp2build_available: true,
3950 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003951 }
3952
3953 cc_library_static {
3954 name: "libstatic",
3955 defaults: ["defaults"],
3956 }
3957
3958 cc_library_shared {
3959 name: "libshared",
3960 defaults: ["defaults"],
3961 }
3962
3963 cc_library {
3964 name: "libboth",
3965 defaults: ["defaults"],
3966 }
3967
3968 cc_binary {
3969 name: "binary",
3970 defaults: ["defaults"],
3971 }`)
3972
Colin Cross7113d202019-11-20 16:39:12 -08003973 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003974 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3975 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3976 }
Colin Cross7113d202019-11-20 16:39:12 -08003977 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003978 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3979 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3980 }
Colin Cross7113d202019-11-20 16:39:12 -08003981 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003982 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3983 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3984 }
3985
Colin Cross7113d202019-11-20 16:39:12 -08003986 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003987 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3988 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3989 }
Colin Cross7113d202019-11-20 16:39:12 -08003990 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003991 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3992 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3993 }
3994}
Colin Crosseabaedd2020-02-06 17:01:55 -08003995
3996func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003997 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003998 bp := `
3999 cc_defaults {
4000 name: "libfoo_defaults",
4001 srcs: ["foo.c"],
4002 cppflags: ["-DFOO"],
4003 product_variables: {
4004 debuggable: {
4005 cppflags: ["-DBAR"],
4006 },
4007 },
4008 }
4009
4010 cc_library {
4011 name: "libfoo",
4012 defaults: ["libfoo_defaults"],
4013 }
4014 `
4015
Paul Duffin8567f222021-03-23 00:02:06 +00004016 result := android.GroupFixturePreparers(
4017 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004018 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004019
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004020 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4021 variables.Debuggable = BoolPtr(true)
4022 }),
4023 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004024
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004025 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004026 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004027}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004028
4029func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4030 t.Parallel()
4031 bp := `
4032 cc_library_static {
4033 name: "libfoo",
4034 srcs: ["foo.c"],
4035 whole_static_libs: ["libbar"],
4036 }
4037
4038 cc_library_static {
4039 name: "libbar",
4040 whole_static_libs: ["libmissing"],
4041 }
4042 `
4043
Paul Duffin8567f222021-03-23 00:02:06 +00004044 result := android.GroupFixturePreparers(
4045 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004046 android.PrepareForTestWithAllowMissingDependencies,
4047 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004048
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004049 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004050 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004051
Paul Duffine84b1332021-03-12 11:59:43 +00004052 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004053
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004054 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004055 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004056}
Colin Crosse9fe2942020-11-10 18:12:15 -08004057
4058func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004059 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004060 bp := `
4061 cc_binary {
4062 name: "bin",
4063 host_supported: true,
4064 shared_libs: ["libshared"],
4065 runtime_libs: ["libruntime"],
4066 srcs: [":gen"],
4067 }
4068
4069 cc_library_shared {
4070 name: "libshared",
4071 host_supported: true,
4072 shared_libs: ["libtransitive"],
4073 }
4074
4075 cc_library_shared {
4076 name: "libtransitive",
4077 host_supported: true,
4078 }
4079
4080 cc_library_shared {
4081 name: "libruntime",
4082 host_supported: true,
4083 }
4084
4085 cc_binary_host {
4086 name: "tool",
4087 srcs: ["foo.cpp"],
4088 }
4089
4090 genrule {
4091 name: "gen",
4092 tools: ["tool"],
4093 out: ["gen.cpp"],
4094 cmd: "$(location tool) $(out)",
4095 }
4096 `
4097
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004098 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004099 ctx := testCcWithConfig(t, config)
4100
4101 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4102 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4103 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4104 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4105 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4106
4107 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4108 t.Errorf("expected host bin dependency %q, got %q", w, g)
4109 }
4110
4111 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4112 t.Errorf("expected host bin dependency %q, got %q", w, g)
4113 }
4114
4115 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4116 t.Errorf("expected host bin dependency %q, got %q", w, g)
4117 }
4118
4119 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4120 t.Errorf("expected host bin dependency %q, got %q", w, g)
4121 }
4122
4123 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4124 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4125 }
4126
4127 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4128 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4129 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4130 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4131
4132 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4133 t.Errorf("expected device bin dependency %q, got %q", w, g)
4134 }
4135
4136 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4137 t.Errorf("expected device bin dependency %q, got %q", w, g)
4138 }
4139
4140 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4141 t.Errorf("expected device bin dependency %q, got %q", w, g)
4142 }
4143
4144 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4145 t.Errorf("expected device bin dependency %q, got %q", w, g)
4146 }
4147
4148 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4149 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4150 }
4151
4152}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004153
4154func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004155 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004156 ctx := testCc(t, `
4157 cc_library_shared {
4158 name: "libclient",
4159 srcs: ["foo.c"],
4160 shared_libs: ["libfoo#1"],
4161 }
4162
4163 cc_library_shared {
4164 name: "libfoo",
4165 srcs: ["foo.c"],
4166 shared_libs: ["libbar"],
4167 export_shared_lib_headers: ["libbar"],
4168 stubs: {
4169 symbol_file: "foo.map.txt",
4170 versions: ["1", "2", "3"],
4171 },
4172 }
4173
4174 cc_library_shared {
4175 name: "libbar",
4176 export_include_dirs: ["include/libbar"],
4177 srcs: ["foo.c"],
4178 }`)
4179
4180 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4181
4182 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4183 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4184 }
4185}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004186
4187func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004188 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09004189 ctx := testCc(t, `
4190 cc_library {
4191 name: "libfoo",
4192 srcs: ["a/Foo.aidl"],
4193 aidl: { flags: ["-Werror"], },
4194 }
4195 `)
4196
4197 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4198 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4199 aidlCommand := manifest.Commands[0].GetCommand()
4200 expectedAidlFlag := "-Werror"
4201 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4202 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4203 }
4204}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004205
Jooyung Han07f70c02021-11-06 07:08:45 +09004206func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004207 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004208 for _, tc := range []struct {
4209 name string
4210 sdkVersion string
4211 variant string
4212 expected string
4213 }{
4214 {
4215 name: "default is current",
4216 sdkVersion: "",
4217 variant: "android_arm64_armv8-a_static",
4218 expected: "platform_apis",
4219 },
4220 {
4221 name: "use sdk_version",
4222 sdkVersion: `sdk_version: "29"`,
4223 variant: "android_arm64_armv8-a_static",
4224 expected: "platform_apis",
4225 },
4226 {
4227 name: "use sdk_version(sdk variant)",
4228 sdkVersion: `sdk_version: "29"`,
4229 variant: "android_arm64_armv8-a_sdk_static",
4230 expected: "29",
4231 },
4232 {
4233 name: "use min_sdk_version",
4234 sdkVersion: `min_sdk_version: "29"`,
4235 variant: "android_arm64_armv8-a_static",
4236 expected: "29",
4237 },
4238 } {
4239 t.Run(tc.name, func(t *testing.T) {
4240 ctx := testCc(t, `
4241 cc_library {
4242 name: "libfoo",
4243 stl: "none",
4244 srcs: ["a/Foo.aidl"],
4245 `+tc.sdkVersion+`
4246 }
4247 `)
4248 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4249 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4250 aidlCommand := manifest.Commands[0].GetCommand()
4251 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4252 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4253 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4254 }
4255 })
4256 }
4257}
4258
Jiyong Parka008fb02021-03-16 17:15:53 +09004259func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004260 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004261 ctx := testCc(t, `
4262 cc_library_shared {
4263 name: "libfoo",
4264 srcs: ["foo.c"],
4265 min_sdk_version: "29",
4266 }`)
4267
4268 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4269 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4270}
4271
Vinh Tranf1924742022-06-24 16:40:11 -04004272func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004273 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004274 bp := `
4275 cc_library_shared {
4276 name: "libfoo",
4277 srcs: ["foo.c"],
4278 min_sdk_version: "S",
4279 }
4280 `
4281 result := android.GroupFixturePreparers(
4282 prepareForCcTest,
4283 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4284 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4285 }),
4286 ).RunTestWithBp(t, bp)
4287 ctx := result.TestContext
4288 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4289 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4290}
4291
Paul Duffin3cb603e2021-02-19 13:57:10 +00004292func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004293 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004294
4295 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4296 // embedded newline characters alone.
4297 trimIndentingSpaces := func(s string) string {
4298 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4299 }
4300
4301 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4302 t.Helper()
4303 expected = trimIndentingSpaces(expected)
4304 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4305 if expected != actual {
4306 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4307 }
4308 }
4309
4310 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4311
4312 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4313 t.Helper()
4314 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4315 name := module.Name()
4316
4317 for _, checker := range checkers {
4318 checker(t, name, exported)
4319 }
4320 }
4321
4322 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4323 return func(t *testing.T, name string, exported FlagExporterInfo) {
4324 t.Helper()
4325 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4326 }
4327 }
4328
4329 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4330 return func(t *testing.T, name string, exported FlagExporterInfo) {
4331 t.Helper()
4332 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4333 }
4334 }
4335
4336 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4337 return func(t *testing.T, name string, exported FlagExporterInfo) {
4338 t.Helper()
4339 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4340 }
4341 }
4342
4343 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4344 return func(t *testing.T, name string, exported FlagExporterInfo) {
4345 t.Helper()
4346 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4347 }
4348 }
4349
4350 genRuleModules := `
4351 genrule {
4352 name: "genrule_foo",
4353 cmd: "generate-foo",
4354 out: [
4355 "generated_headers/foo/generated_header.h",
4356 ],
4357 export_include_dirs: [
4358 "generated_headers",
4359 ],
4360 }
4361
4362 genrule {
4363 name: "genrule_bar",
4364 cmd: "generate-bar",
4365 out: [
4366 "generated_headers/bar/generated_header.h",
4367 ],
4368 export_include_dirs: [
4369 "generated_headers",
4370 ],
4371 }
4372 `
4373
4374 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4375 ctx := testCc(t, genRuleModules+`
4376 cc_library {
4377 name: "libfoo",
4378 srcs: ["foo.c"],
4379 export_include_dirs: ["foo/standard"],
4380 export_system_include_dirs: ["foo/system"],
4381 generated_headers: ["genrule_foo"],
4382 export_generated_headers: ["genrule_foo"],
4383 }
4384
4385 cc_library {
4386 name: "libbar",
4387 srcs: ["bar.c"],
4388 shared_libs: ["libfoo"],
4389 export_include_dirs: ["bar/standard"],
4390 export_system_include_dirs: ["bar/system"],
4391 generated_headers: ["genrule_bar"],
4392 export_generated_headers: ["genrule_bar"],
4393 }
4394 `)
4395 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4396 checkIncludeDirs(t, ctx, foo,
4397 expectedIncludeDirs(`
4398 foo/standard
4399 .intermediates/genrule_foo/gen/generated_headers
4400 `),
4401 expectedSystemIncludeDirs(`foo/system`),
4402 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4403 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4404 )
4405
4406 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4407 checkIncludeDirs(t, ctx, bar,
4408 expectedIncludeDirs(`
4409 bar/standard
4410 .intermediates/genrule_bar/gen/generated_headers
4411 `),
4412 expectedSystemIncludeDirs(`bar/system`),
4413 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4414 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4415 )
4416 })
4417
4418 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4419 ctx := testCc(t, genRuleModules+`
4420 cc_library {
4421 name: "libfoo",
4422 srcs: ["foo.c"],
4423 export_include_dirs: ["foo/standard"],
4424 export_system_include_dirs: ["foo/system"],
4425 generated_headers: ["genrule_foo"],
4426 export_generated_headers: ["genrule_foo"],
4427 }
4428
4429 cc_library {
4430 name: "libbar",
4431 srcs: ["bar.c"],
4432 whole_static_libs: ["libfoo"],
4433 export_include_dirs: ["bar/standard"],
4434 export_system_include_dirs: ["bar/system"],
4435 generated_headers: ["genrule_bar"],
4436 export_generated_headers: ["genrule_bar"],
4437 }
4438 `)
4439 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4440 checkIncludeDirs(t, ctx, foo,
4441 expectedIncludeDirs(`
4442 foo/standard
4443 .intermediates/genrule_foo/gen/generated_headers
4444 `),
4445 expectedSystemIncludeDirs(`foo/system`),
4446 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4447 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4448 )
4449
4450 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4451 checkIncludeDirs(t, ctx, bar,
4452 expectedIncludeDirs(`
4453 bar/standard
4454 foo/standard
4455 .intermediates/genrule_foo/gen/generated_headers
4456 .intermediates/genrule_bar/gen/generated_headers
4457 `),
4458 expectedSystemIncludeDirs(`
4459 bar/system
4460 foo/system
4461 `),
4462 expectedGeneratedHeaders(`
4463 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4464 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4465 `),
4466 expectedOrderOnlyDeps(`
4467 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4468 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4469 `),
4470 )
4471 })
4472
Paul Duffin3cb603e2021-02-19 13:57:10 +00004473 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4474 ctx := testCc(t, genRuleModules+`
4475 cc_library_shared {
4476 name: "libfoo",
4477 srcs: [
4478 "foo.c",
4479 "b.aidl",
4480 "a.proto",
4481 ],
4482 aidl: {
4483 export_aidl_headers: true,
4484 }
4485 }
4486 `)
4487 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4488 checkIncludeDirs(t, ctx, foo,
4489 expectedIncludeDirs(`
4490 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4491 `),
4492 expectedSystemIncludeDirs(``),
4493 expectedGeneratedHeaders(`
4494 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4495 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4496 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004497 `),
4498 expectedOrderOnlyDeps(`
4499 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4500 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4501 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004502 `),
4503 )
4504 })
4505
Paul Duffin3cb603e2021-02-19 13:57:10 +00004506 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4507 ctx := testCc(t, genRuleModules+`
4508 cc_library_shared {
4509 name: "libfoo",
4510 srcs: [
4511 "foo.c",
4512 "b.aidl",
4513 "a.proto",
4514 ],
4515 proto: {
4516 export_proto_headers: true,
4517 }
4518 }
4519 `)
4520 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4521 checkIncludeDirs(t, ctx, foo,
4522 expectedIncludeDirs(`
4523 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4524 `),
4525 expectedSystemIncludeDirs(``),
4526 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004527 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4528 `),
4529 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004530 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4531 `),
4532 )
4533 })
4534
Paul Duffin33056e82021-02-19 13:49:08 +00004535 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004536 ctx := testCc(t, genRuleModules+`
4537 cc_library_shared {
4538 name: "libfoo",
4539 srcs: [
4540 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004541 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004542 "b.aidl",
4543 "a.proto",
4544 ],
4545 }
4546 `)
4547 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4548 checkIncludeDirs(t, ctx, foo,
4549 expectedIncludeDirs(`
4550 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4551 `),
4552 expectedSystemIncludeDirs(``),
4553 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004554 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004555 `),
4556 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004557 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4558 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004559 `),
4560 )
4561 })
4562}
Colin Crossae628182021-06-14 16:52:28 -07004563
4564func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004565 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004566 baseExpectedFlags := []string{
4567 "${config.ArmThumbCflags}",
4568 "${config.ArmCflags}",
4569 "${config.CommonGlobalCflags}",
4570 "${config.DeviceGlobalCflags}",
4571 "${config.ExternalCflags}",
4572 "${config.ArmToolchainCflags}",
4573 "${config.ArmArmv7ANeonCflags}",
4574 "${config.ArmGenericCflags}",
4575 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004576 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004577 }
4578
4579 expectedIncludes := []string{
4580 "external/foo/android_arm_export_include_dirs",
4581 "external/foo/lib32_export_include_dirs",
4582 "external/foo/arm_export_include_dirs",
4583 "external/foo/android_export_include_dirs",
4584 "external/foo/linux_export_include_dirs",
4585 "external/foo/export_include_dirs",
4586 "external/foo/android_arm_local_include_dirs",
4587 "external/foo/lib32_local_include_dirs",
4588 "external/foo/arm_local_include_dirs",
4589 "external/foo/android_local_include_dirs",
4590 "external/foo/linux_local_include_dirs",
4591 "external/foo/local_include_dirs",
4592 "external/foo",
4593 "external/foo/libheader1",
4594 "external/foo/libheader2",
4595 "external/foo/libwhole1",
4596 "external/foo/libwhole2",
4597 "external/foo/libstatic1",
4598 "external/foo/libstatic2",
4599 "external/foo/libshared1",
4600 "external/foo/libshared2",
4601 "external/foo/liblinux",
4602 "external/foo/libandroid",
4603 "external/foo/libarm",
4604 "external/foo/lib32",
4605 "external/foo/libandroid_arm",
4606 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004607 }
4608
4609 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4610 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4611
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004612 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004613 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004614 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004615
4616 lastIncludes := []string{
4617 "out/soong/ndk/sysroot/usr/include",
4618 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4619 }
4620
4621 combineSlices := func(slices ...[]string) []string {
4622 var ret []string
4623 for _, s := range slices {
4624 ret = append(ret, s...)
4625 }
4626 return ret
4627 }
4628
4629 testCases := []struct {
4630 name string
4631 src string
4632 expected []string
4633 }{
4634 {
4635 name: "c",
4636 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004637 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004638 },
4639 {
4640 name: "cc",
4641 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004642 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004643 },
4644 {
4645 name: "assemble",
4646 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004647 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004648 },
4649 }
4650
4651 for _, tc := range testCases {
4652 t.Run(tc.name, func(t *testing.T) {
4653 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004654 cc_library {
4655 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004656 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004657 cflags: ["-std=candcpp"],
4658 conlyflags: ["-std=conly"],
4659 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004660 local_include_dirs: ["local_include_dirs"],
4661 export_include_dirs: ["export_include_dirs"],
4662 export_system_include_dirs: ["export_system_include_dirs"],
4663 static_libs: ["libstatic1", "libstatic2"],
4664 whole_static_libs: ["libwhole1", "libwhole2"],
4665 shared_libs: ["libshared1", "libshared2"],
4666 header_libs: ["libheader1", "libheader2"],
4667 target: {
4668 android: {
4669 shared_libs: ["libandroid"],
4670 local_include_dirs: ["android_local_include_dirs"],
4671 export_include_dirs: ["android_export_include_dirs"],
4672 },
4673 android_arm: {
4674 shared_libs: ["libandroid_arm"],
4675 local_include_dirs: ["android_arm_local_include_dirs"],
4676 export_include_dirs: ["android_arm_export_include_dirs"],
4677 },
4678 linux: {
4679 shared_libs: ["liblinux"],
4680 local_include_dirs: ["linux_local_include_dirs"],
4681 export_include_dirs: ["linux_export_include_dirs"],
4682 },
4683 },
4684 multilib: {
4685 lib32: {
4686 shared_libs: ["lib32"],
4687 local_include_dirs: ["lib32_local_include_dirs"],
4688 export_include_dirs: ["lib32_export_include_dirs"],
4689 },
4690 },
4691 arch: {
4692 arm: {
4693 shared_libs: ["libarm"],
4694 local_include_dirs: ["arm_local_include_dirs"],
4695 export_include_dirs: ["arm_export_include_dirs"],
4696 },
4697 },
4698 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004699 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004700 }
4701
4702 cc_library_headers {
4703 name: "libheader1",
4704 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004705 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004706 stl: "none",
4707 }
4708
4709 cc_library_headers {
4710 name: "libheader2",
4711 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004712 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004713 stl: "none",
4714 }
Liz Kammer08572c62021-09-30 10:11:04 -04004715 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004716
Liz Kammer08572c62021-09-30 10:11:04 -04004717 libs := []string{
4718 "libstatic1",
4719 "libstatic2",
4720 "libwhole1",
4721 "libwhole2",
4722 "libshared1",
4723 "libshared2",
4724 "libandroid",
4725 "libandroid_arm",
4726 "liblinux",
4727 "lib32",
4728 "libarm",
4729 }
Colin Crossae628182021-06-14 16:52:28 -07004730
Liz Kammer08572c62021-09-30 10:11:04 -04004731 for _, lib := range libs {
4732 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004733 cc_library {
4734 name: "%s",
4735 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004736 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004737 stl: "none",
4738 }
4739 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004740 }
4741
4742 ctx := android.GroupFixturePreparers(
4743 PrepareForIntegrationTestWithCc,
4744 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4745 ).RunTest(t)
4746 // Use the arm variant instead of the arm64 variant so that it gets headers from
4747 // ndk_libandroid_support to test LateStaticLibs.
4748 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4749
4750 var includes []string
4751 flags := strings.Split(cflags, " ")
4752 for _, flag := range flags {
4753 if strings.HasPrefix(flag, "-I") {
4754 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4755 } else if flag == "-isystem" {
4756 // skip isystem, include next
4757 } else if len(flag) > 0 {
4758 includes = append(includes, flag)
4759 }
4760 }
4761
4762 android.AssertArrayString(t, "includes", tc.expected, includes)
4763 })
Colin Crossae628182021-06-14 16:52:28 -07004764 }
4765
Colin Crossae628182021-06-14 16:52:28 -07004766}
Alixb5f6d9e2022-04-20 23:00:58 +00004767
zijunzhao933e3802023-01-12 07:26:20 +00004768func TestAddnoOverride64GlobalCflags(t *testing.T) {
4769 t.Parallel()
4770 ctx := testCc(t, `
4771 cc_library_shared {
4772 name: "libclient",
4773 srcs: ["foo.c"],
4774 shared_libs: ["libfoo#1"],
4775 }
4776
4777 cc_library_shared {
4778 name: "libfoo",
4779 srcs: ["foo.c"],
4780 shared_libs: ["libbar"],
4781 export_shared_lib_headers: ["libbar"],
4782 stubs: {
4783 symbol_file: "foo.map.txt",
4784 versions: ["1", "2", "3"],
4785 },
4786 }
4787
4788 cc_library_shared {
4789 name: "libbar",
4790 export_include_dirs: ["include/libbar"],
4791 srcs: ["foo.c"],
4792 }`)
4793
4794 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4795
4796 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4797 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4798 }
4799}
4800
Alixb5f6d9e2022-04-20 23:00:58 +00004801func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004802 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004803 tests := []struct {
4804 name string
4805 clang bool
4806 BuildBrokenClangProperty bool
4807 err string
4808 }{
4809 {
4810 name: "error when clang is set to false",
4811 clang: false,
4812 err: "is no longer supported",
4813 },
4814 {
4815 name: "error when clang is set to true",
4816 clang: true,
4817 err: "property is deprecated, see Changes.md",
4818 },
4819 {
4820 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4821 clang: true,
4822 BuildBrokenClangProperty: true,
4823 },
4824 }
4825
4826 for _, test := range tests {
4827 t.Run(test.name, func(t *testing.T) {
4828 bp := fmt.Sprintf(`
4829 cc_library {
4830 name: "foo",
4831 clang: %t,
4832 }`, test.clang)
4833
4834 if test.err == "" {
4835 android.GroupFixturePreparers(
4836 prepareForCcTest,
4837 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4838 if test.BuildBrokenClangProperty {
4839 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4840 }
4841 }),
4842 ).RunTestWithBp(t, bp)
4843 } else {
4844 prepareForCcTest.
4845 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4846 RunTestWithBp(t, bp)
4847 }
4848 })
4849 }
4850}
Alix Espinoef47e542022-09-14 19:10:51 +00004851
4852func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004853 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004854 tests := []struct {
4855 name string
4856 clangAsFlags []string
4857 BuildBrokenClangAsFlags bool
4858 err string
4859 }{
4860 {
4861 name: "error when clang_asflags is set",
4862 clangAsFlags: []string{"-a", "-b"},
4863 err: "clang_asflags: property is deprecated",
4864 },
4865 {
4866 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4867 clangAsFlags: []string{"-a", "-b"},
4868 BuildBrokenClangAsFlags: true,
4869 },
4870 }
4871
4872 for _, test := range tests {
4873 t.Run(test.name, func(t *testing.T) {
4874 bp := fmt.Sprintf(`
4875 cc_library {
4876 name: "foo",
4877 clang_asflags: %s,
4878 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4879
4880 if test.err == "" {
4881 android.GroupFixturePreparers(
4882 prepareForCcTest,
4883 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4884 if test.BuildBrokenClangAsFlags {
4885 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4886 }
4887 }),
4888 ).RunTestWithBp(t, bp)
4889 } else {
4890 prepareForCcTest.
4891 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4892 RunTestWithBp(t, bp)
4893 }
4894 })
4895 }
4896}
4897
4898func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004899 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004900 tests := []struct {
4901 name string
4902 clangCFlags []string
4903 BuildBrokenClangCFlags bool
4904 err string
4905 }{
4906 {
4907 name: "error when clang_cflags is set",
4908 clangCFlags: []string{"-a", "-b"},
4909 err: "clang_cflags: property is deprecated",
4910 },
4911 {
4912 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4913 clangCFlags: []string{"-a", "-b"},
4914 BuildBrokenClangCFlags: true,
4915 },
4916 }
4917
4918 for _, test := range tests {
4919 t.Run(test.name, func(t *testing.T) {
4920 bp := fmt.Sprintf(`
4921 cc_library {
4922 name: "foo",
4923 clang_cflags: %s,
4924 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4925
4926 if test.err == "" {
4927 android.GroupFixturePreparers(
4928 prepareForCcTest,
4929 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4930 if test.BuildBrokenClangCFlags {
4931 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4932 }
4933 }),
4934 ).RunTestWithBp(t, bp)
4935 } else {
4936 prepareForCcTest.
4937 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4938 RunTestWithBp(t, bp)
4939 }
4940 })
4941 }
4942}
Yu Liue4312402023-01-18 09:15:31 -08004943
4944func TestDclaLibraryInApex(t *testing.T) {
4945 t.Parallel()
4946 bp := `
4947 cc_library_shared {
4948 name: "cc_lib_in_apex",
4949 srcs: ["foo.cc"],
4950 apex_available: ["myapex"],
4951 bazel_module: { label: "//foo/bar:bar" },
4952 }`
4953 label := "//foo/bar:bar"
4954 arch64 := "arm64_armv8-a"
4955 arch32 := "arm_armv7-a-neon"
4956 apexCfgKey := android.ApexConfigKey{
4957 WithinApex: true,
4958 ApexSdkVersion: "28",
4959 }
4960
4961 result := android.GroupFixturePreparers(
4962 prepareForCcTest,
4963 android.FixtureRegisterWithContext(registerTestMutators),
4964 android.FixtureModifyConfig(func(config android.Config) {
4965 config.BazelContext = android.MockBazelContext{
4966 OutputBaseDir: "outputbase",
4967 LabelToCcInfo: map[string]cquery.CcInfo{
4968 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
4969 RootDynamicLibraries: []string{"foo.so"},
4970 },
4971 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
4972 RootDynamicLibraries: []string{"foo.so"},
4973 },
4974 },
4975 BazelRequests: make(map[string]bool),
4976 }
4977 }),
4978 ).RunTestWithBp(t, bp)
4979 ctx := result.TestContext
4980
4981 // Test if the bazel request is queued correctly
4982 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
4983 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
4984 t.Errorf("Bazel request was not queued: %s", key)
4985 }
4986
4987 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
4988 producer := sharedFoo.(android.OutputFileProducer)
4989 outputFiles, err := producer.OutputFiles("")
4990 if err != nil {
4991 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
4992 }
4993 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
4994 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
4995}