blob: f7eb8d2708e0c10085e0c8345053d38e03c2c116 [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
Vinh Tran367d89d2023-04-28 11:21:25 -040027 "android/soong/aidl_library"
Colin Crosse1bb5d02019-09-24 14:55:04 -070028 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050029 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070030)
31
Yu Liue4312402023-01-18 09:15:31 -080032func init() {
33 registerTestMutators(android.InitRegistrationContext)
34}
35
Jiyong Park6a43f042017-10-12 23:05:00 +090036func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000037 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090038}
39
Paul Duffin2e6f90e2021-03-22 23:20:25 +000040var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000041 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
43 variables.DeviceVndkVersion = StringPtr("current")
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 {
MarkDacekf47e1422023-04-19 16:47:36 +000062 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080063 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// testCcErrorWithConfig 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
Justin Yun5f7f7e82019-11-18 19:52:14 +0900111func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800112 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800113
Paul Duffin8567f222021-03-23 00:02:06 +0000114 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000115 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
116 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800117}
118
Paul Duffin8567f222021-03-23 00:02:06 +0000119// testCcError 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 Yun5f7f7e82019-11-18 19:52:14 +0900124func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900125 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000126 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900127 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900128 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129 testCcErrorWithConfig(t, pattern, config)
130 return
131}
132
Paul Duffin8567f222021-03-23 00:02:06 +0000133// testCcErrorProductVndk 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 testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900139 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000140 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900141 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900142 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900143 testCcErrorWithConfig(t, pattern, config)
144 return
145}
146
Logan Chienf3511742017-10-31 18:04:35 +0800147const (
Colin Cross7113d202019-11-20 16:39:12 -0800148 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900149 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
150 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800151 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800152)
153
Paul Duffindb462dd2021-03-21 22:01:55 +0000154// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
155// running it in a fixture that requires all source files to exist.
156func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
157 android.GroupFixturePreparers(
158 PrepareForTestWithCcDefaultModules,
159 android.PrepareForTestDisallowNonExistentPaths,
160 ).RunTest(t)
161}
162
Jiyong Park6a43f042017-10-12 23:05:00 +0900163func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400164 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900165 ctx := testCc(t, `
166 cc_library {
167 name: "libTest",
168 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700169 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800170 nocrt: true,
171 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900172 vendor_available: true,
173 target: {
174 vendor: {
175 srcs: ["bar.c"],
176 },
177 },
178 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900179 `)
180
Logan Chienf3511742017-10-31 18:04:35 +0800181 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900182 var objs []string
183 for _, o := range ld.Inputs {
184 objs = append(objs, o.Base())
185 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800186 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900187 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
188 }
189}
190
Justin Yun7f99ec72021-04-12 13:19:28 +0900191func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
192 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
193 partitionDefined := false
194 checkPartition := func(specific bool, partition string) {
195 if specific {
196 if expected != partition && !partitionDefined {
197 // The variant is installed to the 'partition'
198 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
199 }
200 partitionDefined = true
201 } else {
202 // The variant is not installed to the 'partition'
203 if expected == partition {
204 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
205 }
206 }
207 }
208 socSpecific := func(m *Module) bool {
209 return m.SocSpecific() || m.socSpecificModuleContext()
210 }
211 deviceSpecific := func(m *Module) bool {
212 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
213 }
214 productSpecific := func(m *Module) bool {
215 return m.ProductSpecific() || m.productSpecificModuleContext()
216 }
217 systemExtSpecific := func(m *Module) bool {
218 return m.SystemExtSpecific()
219 }
220 checkPartition(socSpecific(mod), "vendor")
221 checkPartition(deviceSpecific(mod), "odm")
222 checkPartition(productSpecific(mod), "product")
223 checkPartition(systemExtSpecific(mod), "system_ext")
224 if !partitionDefined && expected != "system" {
225 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
226 " but installed to system partition", variant, name, expected)
227 }
228}
229
230func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400231 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900232 t.Helper()
233 ctx := prepareForCcTest.RunTestWithBp(t, `
234 cc_library {
235 name: "libsystem",
236 }
237 cc_library {
238 name: "libsystem_ext",
239 system_ext_specific: true,
240 }
241 cc_library {
242 name: "libproduct",
243 product_specific: true,
244 }
245 cc_library {
246 name: "libvendor",
247 vendor: true,
248 }
249 cc_library {
250 name: "libodm",
251 device_specific: true,
252 }
253 cc_library {
254 name: "liball_available",
255 vendor_available: true,
256 product_available: true,
257 }
258 cc_library {
259 name: "libsystem_ext_all_available",
260 system_ext_specific: true,
261 vendor_available: true,
262 product_available: true,
263 }
264 cc_library {
265 name: "liball_available_odm",
266 odm_available: true,
267 product_available: true,
268 }
269 cc_library {
270 name: "libproduct_vendoravailable",
271 product_specific: true,
272 vendor_available: true,
273 }
274 cc_library {
275 name: "libproduct_odmavailable",
276 product_specific: true,
277 odm_available: true,
278 }
279 `).TestContext
280
281 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
282 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
283 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
284 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
285 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
286
287 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
288 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
289 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
290
291 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
292 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
293 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
294
295 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
296 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
297 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
298
299 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
300 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
301
302 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
303 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
304}
305
Logan Chienf3511742017-10-31 18:04:35 +0800306func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900307 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800308
Logan Chiend3c59a22018-03-29 14:08:15 +0800309 t.Helper()
310
Justin Yun0ecf0b22020-02-28 15:07:59 +0900311 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800312
313 // Check library properties.
314 lib, ok := mod.compiler.(*libraryDecorator)
315 if !ok {
316 t.Errorf("%q must have libraryDecorator", name)
317 } else if lib.baseInstaller.subDir != subDir {
318 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
319 lib.baseInstaller.subDir)
320 }
321
322 // Check VNDK properties.
323 if mod.vndkdep == nil {
324 t.Fatalf("%q must have `vndkdep`", name)
325 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700326 if !mod.IsVndk() {
327 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800328 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400329 if mod.IsVndkSp() != isVndkSp {
330 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800331 }
332
333 // Check VNDK extension properties.
334 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500335 if mod.IsVndkExt() != isVndkExt {
336 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800337 }
338
339 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
340 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
341 }
342}
343
Jooyung Han2216fb12019-11-06 16:46:15 +0900344func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
345 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800346 content := android.ContentFromFileRuleForTests(t, params)
347 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900348 assertArrayString(t, actual, expected)
349}
350
Jooyung Han097087b2019-10-22 19:32:18 +0900351func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
352 t.Helper()
353 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900354 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
355}
356
357func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
358 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800359 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800360 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900361}
362
Logan Chienf3511742017-10-31 18:04:35 +0800363func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400364 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800365 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800366 cc_library {
367 name: "libvndk",
368 vendor_available: true,
369 vndk: {
370 enabled: true,
371 },
372 nocrt: true,
373 }
374
375 cc_library {
376 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900377 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800378 vndk: {
379 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900380 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800381 },
382 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900383 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800384 }
385
386 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900387 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800388 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900389 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800390 vndk: {
391 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900392 },
393 nocrt: true,
394 target: {
395 vendor: {
396 cflags: ["-DTEST"],
397 },
398 product: {
399 cflags: ["-DTEST"],
400 },
401 },
402 }
403
404 cc_library {
405 name: "libvndk_sp",
406 vendor_available: true,
407 vndk: {
408 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800409 support_system_process: true,
410 },
411 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900412 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800413 }
414
415 cc_library {
416 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900417 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800418 vndk: {
419 enabled: true,
420 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900421 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800422 },
423 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900424 target: {
425 vendor: {
426 suffix: "-x",
427 },
428 },
Logan Chienf3511742017-10-31 18:04:35 +0800429 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900430
431 cc_library {
432 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900433 vendor_available: true,
434 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900435 vndk: {
436 enabled: true,
437 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900438 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900439 },
440 nocrt: true,
441 target: {
442 vendor: {
443 suffix: "-x",
444 },
445 product: {
446 suffix: "-x",
447 },
448 },
449 }
450
Justin Yun450ae722021-04-16 19:58:18 +0900451 cc_library {
452 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700453 llndk: {
454 symbol_file: "libllndk.map.txt",
455 export_llndk_headers: ["libllndk_headers"],
456 }
Justin Yun450ae722021-04-16 19:58:18 +0900457 }
458
Justin Yun611e8862021-05-24 18:17:33 +0900459 cc_library {
460 name: "libclang_rt.hwasan-llndk",
461 llndk: {
462 symbol_file: "libclang_rt.hwasan.map.txt",
463 }
464 }
465
Colin Cross627280f2021-04-26 16:53:58 -0700466 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900467 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700468 llndk: {
469 llndk_headers: true,
470 },
Justin Yun450ae722021-04-16 19:58:18 +0900471 export_include_dirs: ["include"],
472 }
473
Colin Crosse4e44bc2020-12-28 13:50:21 -0800474 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900475 name: "llndk.libraries.txt",
476 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800477 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900478 name: "vndkcore.libraries.txt",
479 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800480 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900481 name: "vndksp.libraries.txt",
482 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800483 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900484 name: "vndkprivate.libraries.txt",
485 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800486 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900487 name: "vndkproduct.libraries.txt",
488 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800489 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900490 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800491 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900492 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800493 `
494
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000495 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800496 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900497 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800498
499 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800500
Jooyung Han261e1582020-10-20 18:54:21 +0900501 // subdir == "" because VNDK libs are not supposed to be installed separately.
502 // They are installed as part of VNDK APEX instead.
503 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
504 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900505 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900506 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
507 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900508 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900509
Justin Yun6977e8a2020-10-29 18:24:11 +0900510 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
511 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900512
Inseob Kim1f086e22019-05-09 13:29:15 +0900513 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900514 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000515 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900516
517 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
518 "arm64", "armv8-a"))
519 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
520 "arm", "armv7-a-neon"))
521
522 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
523 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900524 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
525
Inseob Kim1f086e22019-05-09 13:29:15 +0900526 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
527 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900528 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900529
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900530 variant := "android_vendor.29_arm64_armv8-a_shared"
531 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900532
Inseob Kim7f283f42020-06-01 21:53:49 +0900533 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
534
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400535 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
536 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
537 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
538 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
539 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
540 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
541 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
542 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900543
Jooyung Han39edb6c2019-11-06 16:53:07 +0900544 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800545 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
546 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
547 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
548 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
549 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900550
Jooyung Han097087b2019-10-22 19:32:18 +0900551 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
552 "LLNDK: libc.so",
553 "LLNDK: libdl.so",
554 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900555 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900556 "LLNDK: libm.so",
557 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900558 "VNDK-SP: libvndk_sp-x.so",
559 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900560 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900561 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900562 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900563 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900564 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900565 "VNDK-private: libvndk-private.so",
566 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900567 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900568 "VNDK-product: libc++.so",
569 "VNDK-product: libvndk_product.so",
570 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900571 })
Justin Yun611e8862021-05-24 18:17:33 +0900572 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 +0900573 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
574 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
575 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 +0900576 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 +0900577 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
578}
579
Yo Chiangbba545e2020-06-09 16:15:37 +0800580func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400581 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800582 ctx := testCc(t, `
583 cc_library {
584 name: "libvndk_host_supported",
585 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900586 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800587 vndk: {
588 enabled: true,
589 },
590 host_supported: true,
591 }
592
593 cc_library {
594 name: "libvndk_host_supported_but_disabled_on_device",
595 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900596 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800597 vndk: {
598 enabled: true,
599 },
600 host_supported: true,
601 enabled: false,
602 target: {
603 host: {
604 enabled: true,
605 }
606 }
607 }
608
Colin Crosse4e44bc2020-12-28 13:50:21 -0800609 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800610 name: "vndkcore.libraries.txt",
611 }
612 `)
613
614 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
615}
616
Jooyung Han2216fb12019-11-06 16:46:15 +0900617func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400618 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800619 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800620 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900621 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800622 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800623 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000624 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800625 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900626 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900627 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800628 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900629
Colin Cross45bce852021-11-11 22:47:54 -0800630 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700631 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900632 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900633}
634
635func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400636 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800637 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900638 cc_library {
639 name: "libvndk",
640 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900641 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900642 vndk: {
643 enabled: true,
644 },
645 nocrt: true,
646 }
647
648 cc_library {
649 name: "libvndk_sp",
650 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900651 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900652 vndk: {
653 enabled: true,
654 support_system_process: true,
655 },
656 nocrt: true,
657 }
658
659 cc_library {
660 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900661 vendor_available: true,
662 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900663 vndk: {
664 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900665 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900666 },
667 nocrt: true,
668 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900669
Colin Crosse4e44bc2020-12-28 13:50:21 -0800670 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900671 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800672 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900673 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800674 `
675
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000676 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800677 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900678 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800679 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
680
681 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
682
683 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900684
Jooyung Han2216fb12019-11-06 16:46:15 +0900685 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900686}
687
Chris Parsons79d66a52020-06-05 17:26:16 -0400688func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400689 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400690 bp := `
691 cc_test_library {
692 name: "test_lib",
693 srcs: ["test_lib.cpp"],
694 gtest: false,
695 }
696
697 cc_test {
698 name: "main_test",
699 data_libs: ["test_lib"],
700 gtest: false,
701 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400702 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400703
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000704 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400705 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900706 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400707 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
708
709 ctx := testCcWithConfig(t, config)
710 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
711 testBinary := module.(*Module).linker.(*testBinary)
712 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
713 if err != nil {
714 t.Errorf("Expected cc_test to produce output files, error: %s", err)
715 return
716 }
717 if len(outputFiles) != 1 {
718 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
719 return
720 }
721 if len(testBinary.dataPaths()) != 1 {
722 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
723 return
724 }
725
726 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400727 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400728
729 if !strings.HasSuffix(outputPath, "/main_test") {
730 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
731 return
732 }
733 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
734 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
735 return
736 }
737}
738
Chris Parsons216e10a2020-07-09 17:12:52 -0400739func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400740 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400741 bp := `
742 cc_test_library {
743 name: "test_lib",
744 srcs: ["test_lib.cpp"],
745 relative_install_path: "foo/bar/baz",
746 gtest: false,
747 }
748
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400749 cc_binary {
750 name: "test_bin",
751 relative_install_path: "foo/bar/baz",
752 compile_multilib: "both",
753 }
754
Chris Parsons216e10a2020-07-09 17:12:52 -0400755 cc_test {
756 name: "main_test",
757 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400758 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400759 gtest: false,
760 }
761 `
762
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000763 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400764 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900765 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400766 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
767
768 ctx := testCcWithConfig(t, config)
769 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
770 testBinary := module.(*Module).linker.(*testBinary)
771 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
772 if err != nil {
773 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
774 }
775 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400776 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400777 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400778 if len(testBinary.dataPaths()) != 2 {
779 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400780 }
781
782 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400783
784 if !strings.HasSuffix(outputPath, "/main_test") {
785 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
786 }
Colin Crossaa255532020-07-03 13:18:24 -0700787 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400788 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
789 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400790 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400791 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400792 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
793 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
794 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
795 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400796}
797
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000798func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400799 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000800 bp := `
801 cc_test {
802 name: "main_test",
803 srcs: ["main_test.cpp"],
804 test_suites: [
805 "suite_1",
806 "suite_2",
807 ],
808 gtest: false,
809 }
810 `
811
812 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
813 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
814
815 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
816 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
817 if len(compatEntries) != 2 {
818 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
819 }
820 if compatEntries[0] != "suite_1" {
821 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
822 " but was '%s'", compatEntries[0])
823 }
824 if compatEntries[1] != "suite_2" {
825 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
826 " but was '%s'", compatEntries[1])
827 }
828}
829
830func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400831 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000832 bp := `
833 cc_test_library {
834 name: "main_test_lib",
835 srcs: ["main_test_lib.cpp"],
836 test_suites: [
837 "suite_1",
838 "suite_2",
839 ],
840 gtest: false,
841 }
842 `
843
844 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
845 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
846
847 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
848 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
849 if len(compatEntries) != 2 {
850 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
851 }
852 if compatEntries[0] != "suite_1" {
853 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
854 " but was '%s'", compatEntries[0])
855 }
856 if compatEntries[1] != "suite_2" {
857 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
858 " but was '%s'", compatEntries[1])
859 }
860}
861
Justin Yun63e9ec72020-10-29 16:49:43 +0900862func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400863 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900864 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900865 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900866 cc_library {
867 name: "libvndk",
868 vndk: {
869 enabled: true,
870 },
871 nocrt: true,
872 }
873 `)
874
Justin Yunc0d8c492021-01-07 17:45:31 +0900875 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900876 cc_library {
877 name: "libvndk",
878 product_available: true,
879 vndk: {
880 enabled: true,
881 },
882 nocrt: true,
883 }
884 `)
885
Justin Yun6977e8a2020-10-29 18:24:11 +0900886 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
887 cc_library {
888 name: "libvndkprop",
889 vendor_available: true,
890 product_available: true,
891 vndk: {
892 enabled: true,
893 },
894 nocrt: true,
895 target: {
896 vendor: {
897 cflags: ["-DTEST",],
898 },
899 },
900 }
901 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900902}
903
Logan Chiend3c59a22018-03-29 14:08:15 +0800904func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400905 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800906 // Check whether an error is emitted when a VNDK lib depends on a system lib.
907 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
908 cc_library {
909 name: "libvndk",
910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900911 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800912 vndk: {
913 enabled: true,
914 },
915 shared_libs: ["libfwk"], // Cause error
916 nocrt: true,
917 }
918
919 cc_library {
920 name: "libfwk",
921 nocrt: true,
922 }
923 `)
924
925 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
926 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
927 cc_library {
928 name: "libvndk",
929 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900930 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800931 vndk: {
932 enabled: true,
933 },
934 shared_libs: ["libvendor"], // Cause error
935 nocrt: true,
936 }
937
938 cc_library {
939 name: "libvendor",
940 vendor: true,
941 nocrt: true,
942 }
943 `)
944
945 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
946 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
947 cc_library {
948 name: "libvndk_sp",
949 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900950 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800951 vndk: {
952 enabled: true,
953 support_system_process: true,
954 },
955 shared_libs: ["libfwk"], // Cause error
956 nocrt: true,
957 }
958
959 cc_library {
960 name: "libfwk",
961 nocrt: true,
962 }
963 `)
964
965 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
966 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
967 cc_library {
968 name: "libvndk_sp",
969 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900970 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800971 vndk: {
972 enabled: true,
973 support_system_process: true,
974 },
975 shared_libs: ["libvendor"], // Cause error
976 nocrt: true,
977 }
978
979 cc_library {
980 name: "libvendor",
981 vendor: true,
982 nocrt: true,
983 }
984 `)
985
986 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
987 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
988 cc_library {
989 name: "libvndk_sp",
990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900991 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800992 vndk: {
993 enabled: true,
994 support_system_process: true,
995 },
996 shared_libs: ["libvndk"], // Cause error
997 nocrt: true,
998 }
999
1000 cc_library {
1001 name: "libvndk",
1002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001003 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001004 vndk: {
1005 enabled: true,
1006 },
1007 nocrt: true,
1008 }
1009 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001010
1011 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1012 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1013 cc_library {
1014 name: "libvndk",
1015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001016 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001017 vndk: {
1018 enabled: true,
1019 },
1020 shared_libs: ["libnonvndk"],
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libnonvndk",
1026 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001027 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001028 nocrt: true,
1029 }
1030 `)
1031
1032 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1033 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1034 cc_library {
1035 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001036 vendor_available: true,
1037 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001038 vndk: {
1039 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001040 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001041 },
1042 shared_libs: ["libnonvndk"],
1043 nocrt: true,
1044 }
1045
1046 cc_library {
1047 name: "libnonvndk",
1048 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001049 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001050 nocrt: true,
1051 }
1052 `)
1053
1054 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1055 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1056 cc_library {
1057 name: "libvndksp",
1058 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001059 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001060 vndk: {
1061 enabled: true,
1062 support_system_process: true,
1063 },
1064 shared_libs: ["libnonvndk"],
1065 nocrt: true,
1066 }
1067
1068 cc_library {
1069 name: "libnonvndk",
1070 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001071 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001072 nocrt: true,
1073 }
1074 `)
1075
1076 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1077 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1078 cc_library {
1079 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001080 vendor_available: true,
1081 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 vndk: {
1083 enabled: true,
1084 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001085 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001086 },
1087 shared_libs: ["libnonvndk"],
1088 nocrt: true,
1089 }
1090
1091 cc_library {
1092 name: "libnonvndk",
1093 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001094 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001095 nocrt: true,
1096 }
1097 `)
1098}
1099
1100func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001101 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 // okay to link : LLNDK -> double_loadable VNDK
1103 testCc(t, `
1104 cc_library {
1105 name: "libllndk",
1106 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001107 llndk: {
1108 symbol_file: "libllndk.map.txt",
1109 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001110 }
1111
1112 cc_library {
1113 name: "libdoubleloadable",
1114 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001115 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001116 vndk: {
1117 enabled: true,
1118 },
1119 double_loadable: true,
1120 }
1121 `)
1122 // okay to link : LLNDK -> VNDK-SP
1123 testCc(t, `
1124 cc_library {
1125 name: "libllndk",
1126 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001127 llndk: {
1128 symbol_file: "libllndk.map.txt",
1129 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001130 }
1131
1132 cc_library {
1133 name: "libvndksp",
1134 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001135 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001136 vndk: {
1137 enabled: true,
1138 support_system_process: true,
1139 },
1140 }
1141 `)
1142 // okay to link : double_loadable -> double_loadable
1143 testCc(t, `
1144 cc_library {
1145 name: "libdoubleloadable1",
1146 shared_libs: ["libdoubleloadable2"],
1147 vendor_available: true,
1148 double_loadable: true,
1149 }
1150
1151 cc_library {
1152 name: "libdoubleloadable2",
1153 vendor_available: true,
1154 double_loadable: true,
1155 }
1156 `)
1157 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1158 testCc(t, `
1159 cc_library {
1160 name: "libdoubleloadable",
1161 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001162 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001163 vndk: {
1164 enabled: true,
1165 },
1166 double_loadable: true,
1167 shared_libs: ["libnondoubleloadable"],
1168 }
1169
1170 cc_library {
1171 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001172 vendor_available: true,
1173 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001174 vndk: {
1175 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001176 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001177 },
1178 double_loadable: true,
1179 }
1180 `)
1181 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1182 testCc(t, `
1183 cc_library {
1184 name: "libllndk",
1185 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001186 llndk: {
1187 symbol_file: "libllndk.map.txt",
1188 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001189 }
1190
1191 cc_library {
1192 name: "libcoreonly",
1193 shared_libs: ["libvendoravailable"],
1194 }
1195
1196 // indirect dependency of LLNDK
1197 cc_library {
1198 name: "libvendoravailable",
1199 vendor_available: true,
1200 double_loadable: true,
1201 }
1202 `)
1203}
1204
1205func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001206 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001207 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1208 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1209 cc_library {
1210 name: "libllndk",
1211 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001212 llndk: {
1213 symbol_file: "libllndk.map.txt",
1214 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001215 }
1216
1217 cc_library {
1218 name: "libnondoubleloadable",
1219 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001220 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 vndk: {
1222 enabled: true,
1223 },
1224 }
1225 `)
1226
1227 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1228 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1229 cc_library {
1230 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001231 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001232 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001233 llndk: {
1234 symbol_file: "libllndk.map.txt",
1235 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001236 }
1237
1238 cc_library {
1239 name: "libnondoubleloadable",
1240 vendor_available: true,
1241 }
1242 `)
1243
Jooyung Hana70f0672019-01-18 15:20:43 +09001244 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1245 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1246 cc_library {
1247 name: "libllndk",
1248 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001249 llndk: {
1250 symbol_file: "libllndk.map.txt",
1251 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001252 }
1253
1254 cc_library {
1255 name: "libcoreonly",
1256 shared_libs: ["libvendoravailable"],
1257 }
1258
1259 // indirect dependency of LLNDK
1260 cc_library {
1261 name: "libvendoravailable",
1262 vendor_available: true,
1263 }
1264 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001265
1266 // The error is not from 'client' but from 'libllndk'
1267 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1268 cc_library {
1269 name: "client",
1270 vendor_available: true,
1271 double_loadable: true,
1272 shared_libs: ["libllndk"],
1273 }
1274 cc_library {
1275 name: "libllndk",
1276 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001277 llndk: {
1278 symbol_file: "libllndk.map.txt",
1279 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001280 }
1281 cc_library {
1282 name: "libnondoubleloadable",
1283 vendor_available: true,
1284 }
1285 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001286}
1287
Jooyung Han479ca172020-10-19 18:51:07 +09001288func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001289 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001290 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1291 cc_library {
1292 name: "libvndksp",
1293 shared_libs: ["libanothervndksp"],
1294 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001295 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001296 vndk: {
1297 enabled: true,
1298 support_system_process: true,
1299 }
1300 }
1301
1302 cc_library {
1303 name: "libllndk",
1304 shared_libs: ["libanothervndksp"],
1305 }
1306
Jooyung Han479ca172020-10-19 18:51:07 +09001307 cc_library {
1308 name: "libanothervndksp",
1309 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001310 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001311 }
1312 `)
1313}
1314
Logan Chienf3511742017-10-31 18:04:35 +08001315func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001316 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001317 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001318 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001319 cc_library {
1320 name: "libvndk",
1321 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001322 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001323 vndk: {
1324 enabled: true,
1325 },
1326 nocrt: true,
1327 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001328 cc_library {
1329 name: "libvndk2",
1330 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001331 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001332 vndk: {
1333 enabled: true,
1334 },
1335 target: {
1336 vendor: {
1337 suffix: "-suffix",
1338 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001339 product: {
1340 suffix: "-suffix",
1341 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001342 },
1343 nocrt: true,
1344 }
Logan Chienf3511742017-10-31 18:04:35 +08001345
1346 cc_library {
1347 name: "libvndk_ext",
1348 vendor: true,
1349 vndk: {
1350 enabled: true,
1351 extends: "libvndk",
1352 },
1353 nocrt: true,
1354 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001355
1356 cc_library {
1357 name: "libvndk2_ext",
1358 vendor: true,
1359 vndk: {
1360 enabled: true,
1361 extends: "libvndk2",
1362 },
1363 nocrt: true,
1364 }
Logan Chienf3511742017-10-31 18:04:35 +08001365
Justin Yun0ecf0b22020-02-28 15:07:59 +09001366 cc_library {
1367 name: "libvndk_ext_product",
1368 product_specific: true,
1369 vndk: {
1370 enabled: true,
1371 extends: "libvndk",
1372 },
1373 nocrt: true,
1374 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001375
Justin Yun0ecf0b22020-02-28 15:07:59 +09001376 cc_library {
1377 name: "libvndk2_ext_product",
1378 product_specific: true,
1379 vndk: {
1380 enabled: true,
1381 extends: "libvndk2",
1382 },
1383 nocrt: true,
1384 }
1385 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001386 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001387 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001388 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001389
1390 ctx := testCcWithConfig(t, config)
1391
1392 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1393 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1394
1395 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1396 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1397
1398 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1399 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001400}
1401
Logan Chienf3511742017-10-31 18:04:35 +08001402func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001403 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001404 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001405 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001406 cc_library {
1407 name: "libvndk",
1408 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001409 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001410 vndk: {
1411 enabled: true,
1412 },
1413 nocrt: true,
1414 }
1415
1416 cc_library {
1417 name: "libvndk_ext",
1418 vndk: {
1419 enabled: true,
1420 extends: "libvndk",
1421 },
1422 nocrt: true,
1423 }
1424 `)
1425
1426 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1427 cc_library {
1428 name: "libvndk",
1429 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001430 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001431 vndk: {
1432 enabled: true,
1433 },
1434 nocrt: true,
1435 }
1436
1437 cc_library {
1438 name: "libvndk_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 },
1443 nocrt: true,
1444 }
1445 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001446
1447 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1448 cc_library {
1449 name: "libvndk",
1450 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001451 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001452 vndk: {
1453 enabled: true,
1454 },
1455 nocrt: true,
1456 }
1457
1458 cc_library {
1459 name: "libvndk_ext_product",
1460 product_specific: true,
1461 vndk: {
1462 enabled: true,
1463 },
1464 nocrt: true,
1465 }
1466 `)
1467
1468 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1469 cc_library {
1470 name: "libvndk",
1471 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001472 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001473 vndk: {
1474 enabled: true,
1475 },
1476 nocrt: true,
1477 }
1478
1479 cc_library {
1480 name: "libvndk_ext_product",
1481 product_specific: true,
1482 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001483 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001484 vndk: {
1485 enabled: true,
1486 extends: "libvndk",
1487 },
1488 nocrt: true,
1489 }
1490 `)
Logan Chienf3511742017-10-31 18:04:35 +08001491}
1492
1493func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001494 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001495 // This test ensures an error is emitted for inconsistent support_system_process.
1496 testCcError(t, "module \".*\" with mismatched support_system_process", `
1497 cc_library {
1498 name: "libvndk",
1499 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001500 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001501 vndk: {
1502 enabled: true,
1503 },
1504 nocrt: true,
1505 }
1506
1507 cc_library {
1508 name: "libvndk_sp_ext",
1509 vendor: true,
1510 vndk: {
1511 enabled: true,
1512 extends: "libvndk",
1513 support_system_process: true,
1514 },
1515 nocrt: true,
1516 }
1517 `)
1518
1519 testCcError(t, "module \".*\" with mismatched support_system_process", `
1520 cc_library {
1521 name: "libvndk_sp",
1522 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001523 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001524 vndk: {
1525 enabled: true,
1526 support_system_process: true,
1527 },
1528 nocrt: true,
1529 }
1530
1531 cc_library {
1532 name: "libvndk_ext",
1533 vendor: true,
1534 vndk: {
1535 enabled: true,
1536 extends: "libvndk_sp",
1537 },
1538 nocrt: true,
1539 }
1540 `)
1541}
1542
1543func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001544 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001545 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001546 // with `private: true`.
1547 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001548 cc_library {
1549 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001550 vendor_available: true,
1551 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001552 vndk: {
1553 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001554 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001555 },
1556 nocrt: true,
1557 }
1558
1559 cc_library {
1560 name: "libvndk_ext",
1561 vendor: true,
1562 vndk: {
1563 enabled: true,
1564 extends: "libvndk",
1565 },
1566 nocrt: true,
1567 }
1568 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001569
Justin Yunfd9e8042020-12-23 18:23:14 +09001570 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001571 cc_library {
1572 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001573 vendor_available: true,
1574 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001575 vndk: {
1576 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001577 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001578 },
1579 nocrt: true,
1580 }
1581
1582 cc_library {
1583 name: "libvndk_ext_product",
1584 product_specific: true,
1585 vndk: {
1586 enabled: true,
1587 extends: "libvndk",
1588 },
1589 nocrt: true,
1590 }
1591 `)
Logan Chienf3511742017-10-31 18:04:35 +08001592}
1593
Logan Chiend3c59a22018-03-29 14:08:15 +08001594func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001595 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001596 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001597 testCc(t, `
1598 cc_library {
1599 name: "libvndk",
1600 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001601 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001602 vndk: {
1603 enabled: true,
1604 },
1605 nocrt: true,
1606 }
1607
1608 cc_library {
1609 name: "libvndk_ext",
1610 vendor: true,
1611 vndk: {
1612 enabled: true,
1613 extends: "libvndk",
1614 },
1615 nocrt: true,
1616 }
1617
1618 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001619 name: "libvndk_sp",
1620 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001621 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001622 vndk: {
1623 enabled: true,
1624 support_system_process: true,
1625 },
1626 nocrt: true,
1627 }
1628
1629 cc_library {
1630 name: "libvndk_sp_ext",
1631 vendor: true,
1632 vndk: {
1633 enabled: true,
1634 extends: "libvndk_sp",
1635 support_system_process: true,
1636 },
1637 nocrt: true,
1638 }
1639
1640 cc_library {
1641 name: "libvendor",
1642 vendor: true,
1643 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1644 nocrt: true,
1645 }
1646 `)
1647}
1648
Logan Chiend3c59a22018-03-29 14:08:15 +08001649func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001650 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001651 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001652 testCc(t, `
1653 cc_library {
1654 name: "libvndk",
1655 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001656 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001657 vndk: {
1658 enabled: true,
1659 },
1660 nocrt: true,
1661 }
1662
1663 cc_library {
1664 name: "libvndk_ext",
1665 vendor: true,
1666 vndk: {
1667 enabled: true,
1668 extends: "libvndk",
1669 },
1670 shared_libs: ["libvendor"],
1671 nocrt: true,
1672 }
1673
1674 cc_library {
1675 name: "libvendor",
1676 vendor: true,
1677 nocrt: true,
1678 }
1679 `)
Logan Chienf3511742017-10-31 18:04:35 +08001680
Logan Chiend3c59a22018-03-29 14:08:15 +08001681 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1682 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001683 cc_library {
1684 name: "libvndk_sp",
1685 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001686 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001687 vndk: {
1688 enabled: true,
1689 support_system_process: true,
1690 },
1691 nocrt: true,
1692 }
1693
1694 cc_library {
1695 name: "libvndk_sp_ext",
1696 vendor: true,
1697 vndk: {
1698 enabled: true,
1699 extends: "libvndk_sp",
1700 support_system_process: true,
1701 },
1702 shared_libs: ["libvendor"], // Cause an error
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvendor",
1708 vendor: true,
1709 nocrt: true,
1710 }
1711 `)
1712}
1713
Justin Yun0ecf0b22020-02-28 15:07:59 +09001714func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001715 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716 bp := `
1717 cc_library {
1718 name: "libvndk",
1719 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001720 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001721 vndk: {
1722 enabled: true,
1723 },
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 shared_libs: ["libproduct_for_vndklibs"],
1735 nocrt: true,
1736 }
1737
1738 cc_library {
1739 name: "libvndk_sp",
1740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001741 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001742 vndk: {
1743 enabled: true,
1744 support_system_process: true,
1745 },
1746 nocrt: true,
1747 }
1748
1749 cc_library {
1750 name: "libvndk_sp_ext_product",
1751 product_specific: true,
1752 vndk: {
1753 enabled: true,
1754 extends: "libvndk_sp",
1755 support_system_process: true,
1756 },
1757 shared_libs: ["libproduct_for_vndklibs"],
1758 nocrt: true,
1759 }
1760
1761 cc_library {
1762 name: "libproduct",
1763 product_specific: true,
1764 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1765 nocrt: true,
1766 }
1767
1768 cc_library {
1769 name: "libproduct_for_vndklibs",
1770 product_specific: true,
1771 nocrt: true,
1772 }
1773 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001774 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001775 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001776 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001777
1778 testCcWithConfig(t, config)
1779}
1780
Logan Chiend3c59a22018-03-29 14:08:15 +08001781func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001782 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001783 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1784 // library.
1785 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1786 cc_library {
1787 name: "libvndk",
1788 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001789 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001790 vndk: {
1791 enabled: true,
1792 },
1793 nocrt: true,
1794 }
1795
1796 cc_library {
1797 name: "libvndk_sp",
1798 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001799 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001800 vndk: {
1801 enabled: true,
1802 support_system_process: true,
1803 },
1804 nocrt: true,
1805 }
1806
1807 cc_library {
1808 name: "libvndk_sp_ext",
1809 vendor: true,
1810 vndk: {
1811 enabled: true,
1812 extends: "libvndk_sp",
1813 support_system_process: true,
1814 },
1815 shared_libs: ["libvndk"], // Cause an error
1816 nocrt: true,
1817 }
1818 `)
1819
1820 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1821 // library.
1822 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1823 cc_library {
1824 name: "libvndk",
1825 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001826 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001827 vndk: {
1828 enabled: true,
1829 },
1830 nocrt: true,
1831 }
1832
1833 cc_library {
1834 name: "libvndk_ext",
1835 vendor: true,
1836 vndk: {
1837 enabled: true,
1838 extends: "libvndk",
1839 },
1840 nocrt: true,
1841 }
1842
1843 cc_library {
1844 name: "libvndk_sp",
1845 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001846 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001847 vndk: {
1848 enabled: true,
1849 support_system_process: true,
1850 },
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk_sp_ext",
1856 vendor: true,
1857 vndk: {
1858 enabled: true,
1859 extends: "libvndk_sp",
1860 support_system_process: true,
1861 },
1862 shared_libs: ["libvndk_ext"], // Cause an error
1863 nocrt: true,
1864 }
1865 `)
1866}
1867
1868func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001869 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001870 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1871 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001872 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1873 cc_library {
1874 name: "libvndk",
1875 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001876 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001877 vndk: {
1878 enabled: true,
1879 },
1880 nocrt: true,
1881 }
1882
1883 cc_library {
1884 name: "libvndk_ext",
1885 vendor: true,
1886 vndk: {
1887 enabled: true,
1888 extends: "libvndk",
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk2",
1895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001896 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001897 vndk: {
1898 enabled: true,
1899 },
1900 shared_libs: ["libvndk_ext"],
1901 nocrt: true,
1902 }
1903 `)
1904
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001905 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001906 cc_library {
1907 name: "libvndk",
1908 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001909 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001910 vndk: {
1911 enabled: true,
1912 },
1913 nocrt: true,
1914 }
1915
1916 cc_library {
1917 name: "libvndk_ext",
1918 vendor: true,
1919 vndk: {
1920 enabled: true,
1921 extends: "libvndk",
1922 },
1923 nocrt: true,
1924 }
1925
1926 cc_library {
1927 name: "libvndk2",
1928 vendor_available: true,
1929 vndk: {
1930 enabled: true,
1931 },
1932 target: {
1933 vendor: {
1934 shared_libs: ["libvndk_ext"],
1935 },
1936 },
1937 nocrt: true,
1938 }
1939 `)
1940
1941 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1942 cc_library {
1943 name: "libvndk_sp",
1944 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001945 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +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 nocrt: true,
1962 }
1963
1964 cc_library {
1965 name: "libvndk_sp_2",
1966 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001967 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001968 vndk: {
1969 enabled: true,
1970 support_system_process: true,
1971 },
1972 shared_libs: ["libvndk_sp_ext"],
1973 nocrt: true,
1974 }
1975 `)
1976
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001977 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001978 cc_library {
1979 name: "libvndk_sp",
1980 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001981 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001982 vndk: {
1983 enabled: true,
1984 },
1985 nocrt: true,
1986 }
1987
1988 cc_library {
1989 name: "libvndk_sp_ext",
1990 vendor: true,
1991 vndk: {
1992 enabled: true,
1993 extends: "libvndk_sp",
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp2",
2000 vendor_available: true,
2001 vndk: {
2002 enabled: true,
2003 },
2004 target: {
2005 vendor: {
2006 shared_libs: ["libvndk_sp_ext"],
2007 },
2008 },
2009 nocrt: true,
2010 }
2011 `)
2012}
2013
Justin Yun5f7f7e82019-11-18 19:52:14 +09002014func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002015 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002016 bp := `
2017 cc_library {
2018 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002019 llndk: {
2020 symbol_file: "libllndk.map.txt",
2021 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002022 }
2023 cc_library {
2024 name: "libvndk",
2025 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002026 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002027 vndk: {
2028 enabled: true,
2029 },
2030 nocrt: true,
2031 }
2032 cc_library {
2033 name: "libvndk_sp",
2034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002035 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002036 vndk: {
2037 enabled: true,
2038 support_system_process: true,
2039 },
2040 nocrt: true,
2041 }
2042 cc_library {
2043 name: "libva",
2044 vendor_available: true,
2045 nocrt: true,
2046 }
2047 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002048 name: "libpa",
2049 product_available: true,
2050 nocrt: true,
2051 }
2052 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002053 name: "libboth_available",
2054 vendor_available: true,
2055 product_available: true,
2056 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002057 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002058 target: {
2059 vendor: {
2060 suffix: "-vendor",
2061 },
2062 product: {
2063 suffix: "-product",
2064 },
2065 }
2066 }
2067 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002068 name: "libproduct_va",
2069 product_specific: true,
2070 vendor_available: true,
2071 nocrt: true,
2072 }
2073 cc_library {
2074 name: "libprod",
2075 product_specific: true,
2076 shared_libs: [
2077 "libllndk",
2078 "libvndk",
2079 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002080 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002081 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002082 "libproduct_va",
2083 ],
2084 nocrt: true,
2085 }
2086 cc_library {
2087 name: "libvendor",
2088 vendor: true,
2089 shared_libs: [
2090 "libllndk",
2091 "libvndk",
2092 "libvndk_sp",
2093 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002094 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002095 "libproduct_va",
2096 ],
2097 nocrt: true,
2098 }
2099 `
2100
Paul Duffin8567f222021-03-23 00:02:06 +00002101 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002102
Jooyung Han261e1582020-10-20 18:54:21 +09002103 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2104 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002105
2106 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2107 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2108
2109 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2110 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002111
2112 ensureStringContains := func(t *testing.T, str string, substr string) {
2113 t.Helper()
2114 if !strings.Contains(str, substr) {
2115 t.Errorf("%q is not found in %v", substr, str)
2116 }
2117 }
2118 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2119 t.Helper()
2120 if strings.Contains(str, substr) {
2121 t.Errorf("%q is found in %v", substr, str)
2122 }
2123 }
2124
2125 // _static variant is used since _shared reuses *.o from the static variant
2126 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2127 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2128
2129 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2130 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2131 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2132 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2133
2134 product_cflags := product_static.Rule("cc").Args["cFlags"]
2135 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2136 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2137 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002138}
2139
2140func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002141 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002142 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002143 cc_library {
2144 name: "libprod",
2145 product_specific: true,
2146 shared_libs: [
2147 "libvendor",
2148 ],
2149 nocrt: true,
2150 }
2151 cc_library {
2152 name: "libvendor",
2153 vendor: true,
2154 nocrt: true,
2155 }
2156 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002157 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002158 cc_library {
2159 name: "libprod",
2160 product_specific: true,
2161 shared_libs: [
2162 "libsystem",
2163 ],
2164 nocrt: true,
2165 }
2166 cc_library {
2167 name: "libsystem",
2168 nocrt: true,
2169 }
2170 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002171 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002172 cc_library {
2173 name: "libprod",
2174 product_specific: true,
2175 shared_libs: [
2176 "libva",
2177 ],
2178 nocrt: true,
2179 }
2180 cc_library {
2181 name: "libva",
2182 vendor_available: true,
2183 nocrt: true,
2184 }
2185 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002186 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002187 cc_library {
2188 name: "libprod",
2189 product_specific: true,
2190 shared_libs: [
2191 "libvndk_private",
2192 ],
2193 nocrt: true,
2194 }
2195 cc_library {
2196 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002197 vendor_available: true,
2198 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002199 vndk: {
2200 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002201 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002202 },
2203 nocrt: true,
2204 }
2205 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002206 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002207 cc_library {
2208 name: "libprod",
2209 product_specific: true,
2210 shared_libs: [
2211 "libsystem_ext",
2212 ],
2213 nocrt: true,
2214 }
2215 cc_library {
2216 name: "libsystem_ext",
2217 system_ext_specific: true,
2218 nocrt: true,
2219 }
2220 `)
2221 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2222 cc_library {
2223 name: "libsystem",
2224 shared_libs: [
2225 "libproduct_va",
2226 ],
2227 nocrt: true,
2228 }
2229 cc_library {
2230 name: "libproduct_va",
2231 product_specific: true,
2232 vendor_available: true,
2233 nocrt: true,
2234 }
2235 `)
2236}
2237
Jooyung Han38002912019-05-16 04:01:54 +09002238func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002239 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002240 bp := `
2241 cc_library {
2242 name: "libvndk",
2243 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002244 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002245 vndk: {
2246 enabled: true,
2247 },
2248 }
2249 cc_library {
2250 name: "libvndksp",
2251 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002252 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002253 vndk: {
2254 enabled: true,
2255 support_system_process: true,
2256 },
2257 }
2258 cc_library {
2259 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002260 vendor_available: true,
2261 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002262 vndk: {
2263 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002264 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002265 },
2266 }
2267 cc_library {
2268 name: "libvendor",
2269 vendor: true,
2270 }
2271 cc_library {
2272 name: "libvndkext",
2273 vendor: true,
2274 vndk: {
2275 enabled: true,
2276 extends: "libvndk",
2277 },
2278 }
2279 vndk_prebuilt_shared {
2280 name: "prevndk",
2281 version: "27",
2282 target_arch: "arm",
2283 binder32bit: true,
2284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002285 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002286 vndk: {
2287 enabled: true,
2288 },
2289 arch: {
2290 arm: {
2291 srcs: ["liba.so"],
2292 },
2293 },
2294 }
2295 cc_library {
2296 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002297 llndk: {
2298 symbol_file: "libllndk.map.txt",
2299 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002300 }
2301 cc_library {
2302 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002303 llndk: {
2304 symbol_file: "libllndkprivate.map.txt",
2305 private: true,
2306 }
Colin Cross78212242021-01-06 14:51:30 -08002307 }
2308
2309 llndk_libraries_txt {
2310 name: "llndk.libraries.txt",
2311 }
2312 vndkcore_libraries_txt {
2313 name: "vndkcore.libraries.txt",
2314 }
2315 vndksp_libraries_txt {
2316 name: "vndksp.libraries.txt",
2317 }
2318 vndkprivate_libraries_txt {
2319 name: "vndkprivate.libraries.txt",
2320 }
2321 vndkcorevariant_libraries_txt {
2322 name: "vndkcorevariant.libraries.txt",
2323 insert_vndk_version: false,
2324 }
2325 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002326
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002327 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002328 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002329 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002330 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002331 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002332
Colin Cross78212242021-01-06 14:51:30 -08002333 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2334 []string{"libvndk.so", "libvndkprivate.so"})
2335 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2336 []string{"libc++.so", "libvndksp.so"})
2337 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2338 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2339 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2340 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002341
Colin Crossfb0c16e2019-11-20 17:12:35 -08002342 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002343
Jooyung Han38002912019-05-16 04:01:54 +09002344 tests := []struct {
2345 variant string
2346 name string
2347 expected string
2348 }{
2349 {vendorVariant, "libvndk", "native:vndk"},
2350 {vendorVariant, "libvndksp", "native:vndk"},
2351 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2352 {vendorVariant, "libvendor", "native:vendor"},
2353 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002354 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002355 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002356 {coreVariant, "libvndk", "native:platform"},
2357 {coreVariant, "libvndkprivate", "native:platform"},
2358 {coreVariant, "libllndk", "native:platform"},
2359 }
2360 for _, test := range tests {
2361 t.Run(test.name, func(t *testing.T) {
2362 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2363 assertString(t, module.makeLinkType, test.expected)
2364 })
2365 }
2366}
2367
Jeff Gaston294356f2017-09-27 17:05:30 -07002368var staticLinkDepOrderTestCases = []struct {
2369 // This is a string representation of a map[moduleName][]moduleDependency .
2370 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002371 inStatic string
2372
2373 // This is a string representation of a map[moduleName][]moduleDependency .
2374 // It models the dependencies declared in an Android.bp file.
2375 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002376
2377 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2378 // The keys of allOrdered specify which modules we would like to check.
2379 // The values of allOrdered specify the expected result (of the transitive closure of all
2380 // dependencies) for each module to test
2381 allOrdered string
2382
2383 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2384 // The keys of outOrdered specify which modules we would like to check.
2385 // The values of outOrdered specify the expected result (of the ordered linker command line)
2386 // for each module to test.
2387 outOrdered string
2388}{
2389 // Simple tests
2390 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002391 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002392 outOrdered: "",
2393 },
2394 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002395 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002396 outOrdered: "a:",
2397 },
2398 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002399 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002400 outOrdered: "a:b; b:",
2401 },
2402 // Tests of reordering
2403 {
2404 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002405 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002406 outOrdered: "a:b,c,d; b:d; c:d; d:",
2407 },
2408 {
2409 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002410 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002411 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2412 },
2413 {
2414 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002415 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002416 outOrdered: "a:d,b,e,c; d:b; e:c",
2417 },
2418 {
2419 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002420 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002421 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2422 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2423 },
2424 {
2425 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002426 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 -07002427 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2428 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2429 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002430 // shared dependencies
2431 {
2432 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2433 // So, we don't actually have to check that a shared dependency of c will change the order
2434 // of a library that depends statically on b and on c. We only need to check that if c has
2435 // a shared dependency on b, that that shows up in allOrdered.
2436 inShared: "c:b",
2437 allOrdered: "c:b",
2438 outOrdered: "c:",
2439 },
2440 {
2441 // This test doesn't actually include any shared dependencies but it's a reminder of what
2442 // the second phase of the above test would look like
2443 inStatic: "a:b,c; c:b",
2444 allOrdered: "a:c,b; c:b",
2445 outOrdered: "a:c,b; c:b",
2446 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002447 // tiebreakers for when two modules specifying different orderings and there is no dependency
2448 // to dictate an order
2449 {
2450 // 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 -08002451 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002452 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2453 },
2454 {
2455 // 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 -08002456 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 -07002457 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2458 },
2459 // Tests involving duplicate dependencies
2460 {
2461 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002462 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002463 outOrdered: "a:c,b",
2464 },
2465 {
2466 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002467 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002468 outOrdered: "a:d,c,b",
2469 },
2470 // Tests to confirm the nonexistence of infinite loops.
2471 // These cases should never happen, so as long as the test terminates and the
2472 // result is deterministic then that should be fine.
2473 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002474 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002475 outOrdered: "a:a",
2476 },
2477 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002478 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002479 allOrdered: "a:b,c; b:c,a; c:a,b",
2480 outOrdered: "a:b; b:c; c:a",
2481 },
2482 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002483 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002484 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2485 outOrdered: "a:c,b; b:a,c; c:b,a",
2486 },
2487}
2488
2489// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2490func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2491 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2492 strippedText := strings.Replace(text, " ", "", -1)
2493 if len(strippedText) < 1 {
2494 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2495 }
2496 allDeps = make(map[android.Path][]android.Path, 0)
2497
2498 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2499 moduleTexts := strings.Split(strippedText, ";")
2500
2501 outputForModuleName := func(moduleName string) android.Path {
2502 return android.PathForTesting(moduleName)
2503 }
2504
2505 for _, moduleText := range moduleTexts {
2506 // convert from "a:b,c" to ["a", "b,c"]
2507 components := strings.Split(moduleText, ":")
2508 if len(components) != 2 {
2509 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2510 }
2511 moduleName := components[0]
2512 moduleOutput := outputForModuleName(moduleName)
2513 modulesInOrder = append(modulesInOrder, moduleOutput)
2514
2515 depString := components[1]
2516 // convert from "b,c" to ["b", "c"]
2517 depNames := strings.Split(depString, ",")
2518 if len(depString) < 1 {
2519 depNames = []string{}
2520 }
2521 var deps []android.Path
2522 for _, depName := range depNames {
2523 deps = append(deps, outputForModuleName(depName))
2524 }
2525 allDeps[moduleOutput] = deps
2526 }
2527 return modulesInOrder, allDeps
2528}
2529
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002530func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002531 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 ctx := testCc(t, `
2533 cc_library {
2534 name: "a",
2535 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002536 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002537 }
2538 cc_library {
2539 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002540 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002541 }
2542 cc_library {
2543 name: "c",
2544 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002545 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 }
2547 cc_library {
2548 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002549 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002550 }
2551
2552 `)
2553
Colin Cross7113d202019-11-20 16:39:12 -08002554 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002555 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002556 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2557 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002558 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002559
2560 if !reflect.DeepEqual(actual, expected) {
2561 t.Errorf("staticDeps orderings were not propagated correctly"+
2562 "\nactual: %v"+
2563 "\nexpected: %v",
2564 actual,
2565 expected,
2566 )
2567 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002568}
Jeff Gaston294356f2017-09-27 17:05:30 -07002569
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002570func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002571 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002572 ctx := testCc(t, `
2573 cc_library {
2574 name: "a",
2575 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002576 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002577 }
2578 cc_library {
2579 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002580 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002581 }
2582 cc_library {
2583 name: "c",
2584 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002585 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002586 }
2587
2588 `)
2589
Colin Cross7113d202019-11-20 16:39:12 -08002590 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002591 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002592 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2593 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002594 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002595
2596 if !reflect.DeepEqual(actual, expected) {
2597 t.Errorf("staticDeps orderings did not account for shared libs"+
2598 "\nactual: %v"+
2599 "\nexpected: %v",
2600 actual,
2601 expected,
2602 )
2603 }
2604}
2605
Jooyung Hanb04a4992020-03-13 18:57:35 +09002606func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002607 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002608 if !reflect.DeepEqual(actual, expected) {
2609 t.Errorf(message+
2610 "\nactual: %v"+
2611 "\nexpected: %v",
2612 actual,
2613 expected,
2614 )
2615 }
2616}
2617
Jooyung Han61b66e92020-03-21 14:21:46 +00002618func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002619 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002620 result := prepareForCcTest.RunTestWithBp(t, `
2621 cc_library {
2622 name: "libllndk",
2623 stubs: { versions: ["1", "2"] },
2624 llndk: {
2625 symbol_file: "libllndk.map.txt",
2626 },
2627 export_include_dirs: ["include"],
2628 }
2629
2630 cc_prebuilt_library_shared {
2631 name: "libllndkprebuilt",
2632 stubs: { versions: ["1", "2"] },
2633 llndk: {
2634 symbol_file: "libllndkprebuilt.map.txt",
2635 },
2636 }
2637
2638 cc_library {
2639 name: "libllndk_with_external_headers",
2640 stubs: { versions: ["1", "2"] },
2641 llndk: {
2642 symbol_file: "libllndk.map.txt",
2643 export_llndk_headers: ["libexternal_llndk_headers"],
2644 },
2645 header_libs: ["libexternal_headers"],
2646 export_header_lib_headers: ["libexternal_headers"],
2647 }
2648 cc_library_headers {
2649 name: "libexternal_headers",
2650 export_include_dirs: ["include"],
2651 vendor_available: true,
2652 }
2653 cc_library_headers {
2654 name: "libexternal_llndk_headers",
2655 export_include_dirs: ["include_llndk"],
2656 llndk: {
2657 symbol_file: "libllndk.map.txt",
2658 },
2659 vendor_available: true,
2660 }
2661
2662 cc_library {
2663 name: "libllndk_with_override_headers",
2664 stubs: { versions: ["1", "2"] },
2665 llndk: {
2666 symbol_file: "libllndk.map.txt",
2667 override_export_include_dirs: ["include_llndk"],
2668 },
2669 export_include_dirs: ["include"],
2670 }
2671 `)
2672 actual := result.ModuleVariantsForTests("libllndk")
2673 for i := 0; i < len(actual); i++ {
2674 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2675 actual = append(actual[:i], actual[i+1:]...)
2676 i--
2677 }
2678 }
2679 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002680 "android_vendor.29_arm64_armv8-a_shared_current",
2681 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002682 "android_vendor.29_arm_armv7-a-neon_shared_current",
2683 "android_vendor.29_arm_armv7-a-neon_shared",
2684 }
2685 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2686
2687 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2688 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2689
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002690 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2691 t.Helper()
2692 m := result.ModuleForTests(module, variant).Module()
2693 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2694 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2695 expectedDirs, f.IncludeDirs)
2696 }
2697
2698 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2699 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2700 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2701 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2702 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2703 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2704}
2705
Jiyong Parka46a4d52017-12-14 19:54:34 +09002706func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002707 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002708 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002709 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710 name: "libllndk_headers",
2711 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002712 llndk: {
2713 llndk_headers: true,
2714 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002715 }
2716 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002717 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002718 llndk: {
2719 symbol_file: "libllndk.map.txt",
2720 export_llndk_headers: ["libllndk_headers"],
2721 }
Colin Cross0477b422020-10-13 18:43:54 -07002722 }
2723
2724 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002725 name: "libvendor",
2726 shared_libs: ["libllndk"],
2727 vendor: true,
2728 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002729 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002730 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002731 }
2732 `)
2733
2734 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002735 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002736 cflags := cc.Args["cFlags"]
2737 if !strings.Contains(cflags, "-Imy_include") {
2738 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2739 }
2740}
2741
Logan Chien43d34c32017-12-20 01:17:32 +08002742func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2743 actual := module.Properties.AndroidMkRuntimeLibs
2744 if !reflect.DeepEqual(actual, expected) {
2745 t.Errorf("incorrect runtime_libs for shared libs"+
2746 "\nactual: %v"+
2747 "\nexpected: %v",
2748 actual,
2749 expected,
2750 )
2751 }
2752}
2753
2754const runtimeLibAndroidBp = `
2755 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002756 name: "liball_available",
2757 vendor_available: true,
2758 product_available: true,
2759 no_libcrt : true,
2760 nocrt : true,
2761 system_shared_libs : [],
2762 }
2763 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002764 name: "libvendor_available1",
2765 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002766 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002767 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002768 nocrt : true,
2769 system_shared_libs : [],
2770 }
2771 cc_library {
2772 name: "libvendor_available2",
2773 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002774 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002775 target: {
2776 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002777 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002778 }
2779 },
Yi Konge7fe9912019-06-02 00:53:50 -07002780 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002781 nocrt : true,
2782 system_shared_libs : [],
2783 }
2784 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002785 name: "libproduct_vendor",
2786 product_specific: true,
2787 vendor_available: true,
2788 no_libcrt : true,
2789 nocrt : true,
2790 system_shared_libs : [],
2791 }
2792 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002793 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002794 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002795 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002796 nocrt : true,
2797 system_shared_libs : [],
2798 }
2799 cc_library {
2800 name: "libvendor1",
2801 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002802 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002803 nocrt : true,
2804 system_shared_libs : [],
2805 }
2806 cc_library {
2807 name: "libvendor2",
2808 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002809 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002810 no_libcrt : true,
2811 nocrt : true,
2812 system_shared_libs : [],
2813 }
2814 cc_library {
2815 name: "libproduct_available1",
2816 product_available: true,
2817 runtime_libs: ["liball_available"],
2818 no_libcrt : true,
2819 nocrt : true,
2820 system_shared_libs : [],
2821 }
2822 cc_library {
2823 name: "libproduct1",
2824 product_specific: true,
2825 no_libcrt : true,
2826 nocrt : true,
2827 system_shared_libs : [],
2828 }
2829 cc_library {
2830 name: "libproduct2",
2831 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002832 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002833 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002834 nocrt : true,
2835 system_shared_libs : [],
2836 }
2837`
2838
2839func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002840 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002841 ctx := testCc(t, runtimeLibAndroidBp)
2842
2843 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002844 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002845
Justin Yun8a2600c2020-12-07 12:44:03 +09002846 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2847 checkRuntimeLibs(t, []string{"liball_available"}, module)
2848
2849 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2850 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002851
2852 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002853 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002854
2855 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2856 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002857 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002858
Justin Yun8a2600c2020-12-07 12:44:03 +09002859 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2860 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002861
2862 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002863 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002864
2865 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2866 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002867 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002868
2869 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2870 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2871
2872 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002873 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002874}
2875
2876func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002877 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002878 ctx := testCc(t, runtimeLibAndroidBp)
2879
Colin Cross7113d202019-11-20 16:39:12 -08002880 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002881 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2882 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002883
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002884 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002885 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002886 checkRuntimeLibs(t, nil, module)
2887}
2888
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002889func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002890 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002891 actual := module.Properties.AndroidMkStaticLibs
2892 if !reflect.DeepEqual(actual, expected) {
2893 t.Errorf("incorrect static_libs"+
2894 "\nactual: %v"+
2895 "\nexpected: %v",
2896 actual,
2897 expected,
2898 )
2899 }
2900}
2901
2902const staticLibAndroidBp = `
2903 cc_library {
2904 name: "lib1",
2905 }
2906 cc_library {
2907 name: "lib2",
2908 static_libs: ["lib1"],
2909 }
2910`
2911
2912func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002913 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002914 ctx := testCc(t, staticLibAndroidBp)
2915
2916 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002917 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002918 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002919 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002920
2921 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002922 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002923 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2924 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002925 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002926}
2927
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002928func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
2929 bp := `
2930 cc_library {
2931 name: "static_dep",
2932 }
2933 cc_library {
2934 name: "whole_static_dep",
2935 }
2936 cc_library {
2937 name: "shared_dep",
2938 }
2939 cc_library {
2940 name: "lib",
2941 bazel_module: { label: "//:lib" },
2942 static_libs: ["static_dep"],
2943 whole_static_libs: ["whole_static_dep"],
2944 shared_libs: ["shared_dep"],
2945 }
2946 cc_test {
2947 name: "test",
2948 bazel_module: { label: "//:test" },
2949 static_libs: ["static_dep"],
2950 whole_static_libs: ["whole_static_dep"],
2951 shared_libs: ["shared_dep"],
2952 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04002953 sanitize: {
2954 // cc_test modules default to memtag_heap: true,
2955 // but this adds extra dependencies that we don't care about
2956 never: true,
2957 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002958 }
2959 cc_binary {
2960 name: "binary",
2961 bazel_module: { label: "//:binary" },
2962 static_libs: ["static_dep"],
2963 whole_static_libs: ["whole_static_dep"],
2964 shared_libs: ["shared_dep"],
2965 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05002966 cc_library_headers {
2967 name: "lib_headers",
2968 bazel_module: { label: "//:lib_headers" },
2969 static_libs: ["static_dep"],
2970 whole_static_libs: ["whole_static_dep"],
2971 shared_libs: ["shared_dep"],
2972 }
2973 cc_prebuilt_library {
2974 name: "lib_prebuilt",
2975 bazel_module: { label: "//:lib_prebuilt" },
2976 static_libs: ["static_dep"],
2977 whole_static_libs: ["whole_static_dep"],
2978 shared_libs: ["shared_dep"],
2979 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002980 `
2981
2982 testCases := []struct {
2983 name string
2984 moduleName string
2985 variant string
2986 androidMkInfo cquery.CcAndroidMkInfo
2987 }{
2988 {
2989 name: "shared lib",
2990 moduleName: "lib",
2991 variant: "android_arm64_armv8-a_shared",
2992 androidMkInfo: cquery.CcAndroidMkInfo{
2993 LocalStaticLibs: []string{"static_dep"},
2994 LocalWholeStaticLibs: []string{"whole_static_dep"},
2995 LocalSharedLibs: []string{"shared_dep"},
2996 },
2997 },
2998 {
2999 name: "static lib",
3000 moduleName: "lib",
3001 variant: "android_arm64_armv8-a_static",
3002 androidMkInfo: cquery.CcAndroidMkInfo{
3003 LocalStaticLibs: []string{"static_dep"},
3004 LocalWholeStaticLibs: []string{"whole_static_dep"},
3005 LocalSharedLibs: []string{"shared_dep"},
3006 },
3007 },
3008 {
3009 name: "cc_test arm64",
3010 moduleName: "test",
3011 variant: "android_arm64_armv8-a",
3012 androidMkInfo: cquery.CcAndroidMkInfo{
3013 LocalStaticLibs: []string{"static_dep"},
3014 LocalWholeStaticLibs: []string{"whole_static_dep"},
3015 LocalSharedLibs: []string{"shared_dep"},
3016 },
3017 },
3018 {
3019 name: "cc_test arm",
3020 moduleName: "test",
3021 variant: "android_arm_armv7-a-neon",
3022 androidMkInfo: cquery.CcAndroidMkInfo{
3023 LocalStaticLibs: []string{"static_dep"},
3024 LocalWholeStaticLibs: []string{"whole_static_dep"},
3025 LocalSharedLibs: []string{"shared_dep"},
3026 },
3027 },
3028 {
3029 name: "cc_binary",
3030 moduleName: "binary",
3031 variant: "android_arm64_armv8-a",
3032 androidMkInfo: cquery.CcAndroidMkInfo{
3033 LocalStaticLibs: []string{"static_dep"},
3034 LocalWholeStaticLibs: []string{"whole_static_dep"},
3035 LocalSharedLibs: []string{"shared_dep"},
3036 },
3037 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003038 {
3039 name: "cc_library_headers",
3040 moduleName: "lib_headers",
3041 variant: "android_arm64_armv8-a",
3042 androidMkInfo: cquery.CcAndroidMkInfo{
3043 LocalStaticLibs: []string{"static_dep"},
3044 LocalWholeStaticLibs: []string{"whole_static_dep"},
3045 LocalSharedLibs: []string{"shared_dep"},
3046 },
3047 },
3048 {
3049 name: "prebuilt lib static",
3050 moduleName: "lib_prebuilt",
3051 variant: "android_arm64_armv8-a_static",
3052 androidMkInfo: cquery.CcAndroidMkInfo{
3053 LocalStaticLibs: []string{"static_dep"},
3054 LocalWholeStaticLibs: []string{"whole_static_dep"},
3055 LocalSharedLibs: []string{"shared_dep"},
3056 },
3057 },
3058 {
3059 name: "prebuilt lib shared",
3060 moduleName: "lib_prebuilt",
3061 variant: "android_arm64_armv8-a_shared",
3062 androidMkInfo: cquery.CcAndroidMkInfo{
3063 LocalStaticLibs: []string{"static_dep"},
3064 LocalWholeStaticLibs: []string{"whole_static_dep"},
3065 LocalSharedLibs: []string{"shared_dep"},
3066 },
3067 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003068 }
3069
3070 outputBaseDir := "out/bazel"
3071 for _, tc := range testCases {
3072 t.Run(tc.name, func(t *testing.T) {
3073 result := android.GroupFixturePreparers(
3074 prepareForCcTest,
3075 android.FixtureModifyConfig(func(config android.Config) {
3076 config.BazelContext = android.MockBazelContext{
3077 OutputBaseDir: outputBaseDir,
3078 LabelToCcInfo: map[string]cquery.CcInfo{
3079 "//:lib": cquery.CcInfo{
3080 CcAndroidMkInfo: tc.androidMkInfo,
3081 RootDynamicLibraries: []string{""},
3082 },
3083 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3084 CcAndroidMkInfo: tc.androidMkInfo,
3085 RootStaticArchives: []string{""},
3086 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003087 "//:lib_headers": cquery.CcInfo{
3088 CcAndroidMkInfo: tc.androidMkInfo,
3089 OutputFiles: []string{""},
3090 },
3091 "//:lib_prebuilt": cquery.CcInfo{
3092 CcAndroidMkInfo: tc.androidMkInfo,
3093 },
3094 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3095 CcAndroidMkInfo: tc.androidMkInfo,
3096 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003097 },
3098 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003099 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003100 CcAndroidMkInfo: tc.androidMkInfo,
3101 },
3102 "//:binary": cquery.CcUnstrippedInfo{
3103 CcAndroidMkInfo: tc.androidMkInfo,
3104 },
3105 },
3106 }
3107 }),
3108 ).RunTestWithBp(t, bp)
3109 ctx := result.TestContext
3110
3111 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3112 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003113 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3114 t.Errorf("incorrect static_libs"+
3115 "\nactual: %v"+
3116 "\nexpected: %v",
3117 module.Properties.AndroidMkStaticLibs,
3118 tc.androidMkInfo.LocalStaticLibs,
3119 )
3120 }
3121 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3122 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3123 tc.androidMkInfo.LocalStaticLibs,
3124 )
3125 if staticDepsDiffer {
3126 t.Errorf(
3127 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3128 tc.androidMkInfo.LocalStaticLibs,
3129 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3130 missingStaticDeps,
3131 additionalStaticDeps,
3132 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003133 }
3134
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003135 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3136 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3137 tc.androidMkInfo.LocalWholeStaticLibs,
3138 module.Properties.AndroidMkWholeStaticLibs,
3139 )
3140 }
3141 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3142 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3143 tc.androidMkInfo.LocalWholeStaticLibs,
3144 )
3145 if wholeStaticDepsDiffer {
3146 t.Errorf(
3147 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3148 tc.androidMkInfo.LocalWholeStaticLibs,
3149 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3150 missingWholeStaticDeps,
3151 additionalWholeStaticDeps,
3152 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003153 }
3154
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003155 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3156 t.Errorf("incorrect shared_libs"+
3157 "\nactual: %v"+
3158 "\nexpected: %v",
3159 module.Properties.AndroidMkSharedLibs,
3160 tc.androidMkInfo.LocalSharedLibs,
3161 )
3162 }
3163 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3164 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3165 tc.androidMkInfo.LocalSharedLibs,
3166 )
3167 if sharedDepsDiffer {
3168 t.Errorf(
3169 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3170 tc.androidMkInfo.LocalSharedLibs,
3171 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3172 missingSharedDeps,
3173 additionalSharedDeps,
3174 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003175 }
3176 })
3177 }
3178}
3179
Jiyong Parkd08b6972017-09-26 10:50:54 +09003180var compilerFlagsTestCases = []struct {
3181 in string
3182 out bool
3183}{
3184 {
3185 in: "a",
3186 out: false,
3187 },
3188 {
3189 in: "-a",
3190 out: true,
3191 },
3192 {
3193 in: "-Ipath/to/something",
3194 out: false,
3195 },
3196 {
3197 in: "-isystempath/to/something",
3198 out: false,
3199 },
3200 {
3201 in: "--coverage",
3202 out: false,
3203 },
3204 {
3205 in: "-include a/b",
3206 out: true,
3207 },
3208 {
3209 in: "-include a/b c/d",
3210 out: false,
3211 },
3212 {
3213 in: "-DMACRO",
3214 out: true,
3215 },
3216 {
3217 in: "-DMAC RO",
3218 out: false,
3219 },
3220 {
3221 in: "-a -b",
3222 out: false,
3223 },
3224 {
3225 in: "-DMACRO=definition",
3226 out: true,
3227 },
3228 {
3229 in: "-DMACRO=defi nition",
3230 out: true, // TODO(jiyong): this should be false
3231 },
3232 {
3233 in: "-DMACRO(x)=x + 1",
3234 out: true,
3235 },
3236 {
3237 in: "-DMACRO=\"defi nition\"",
3238 out: true,
3239 },
3240}
3241
3242type mockContext struct {
3243 BaseModuleContext
3244 result bool
3245}
3246
3247func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3248 // CheckBadCompilerFlags calls this function when the flag should be rejected
3249 ctx.result = false
3250}
3251
3252func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003253 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003254 for _, testCase := range compilerFlagsTestCases {
3255 ctx := &mockContext{result: true}
3256 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3257 if ctx.result != testCase.out {
3258 t.Errorf("incorrect output:")
3259 t.Errorf(" input: %#v", testCase.in)
3260 t.Errorf(" expected: %#v", testCase.out)
3261 t.Errorf(" got: %#v", ctx.result)
3262 }
3263 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003264}
Jiyong Park374510b2018-03-19 18:23:01 +09003265
Jiyong Park37b25202018-07-11 10:49:27 +09003266func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003267 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003268 ctx := testCc(t, `
3269 cc_library_shared {
3270 name: "librecovery",
3271 recovery: true,
3272 }
3273 cc_library_shared {
3274 name: "librecovery32",
3275 recovery: true,
3276 compile_multilib:"32",
3277 }
Jiyong Park5baac542018-08-28 09:55:37 +09003278 cc_library_shared {
3279 name: "libHalInRecovery",
3280 recovery_available: true,
3281 vendor: true,
3282 }
Jiyong Park37b25202018-07-11 10:49:27 +09003283 `)
3284
3285 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003286 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003287 if len(variants) != 1 || !android.InList(arm64, variants) {
3288 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3289 }
3290
3291 variants = ctx.ModuleVariantsForTests("librecovery32")
3292 if android.InList(arm64, variants) {
3293 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3294 }
Jiyong Park5baac542018-08-28 09:55:37 +09003295
3296 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3297 if !recoveryModule.Platform() {
3298 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3299 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003300}
Jiyong Park5baac542018-08-28 09:55:37 +09003301
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003302func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003303 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003304 bp := `
3305 cc_prebuilt_test_library_shared {
3306 name: "test_lib",
3307 relative_install_path: "foo/bar/baz",
3308 srcs: ["srcpath/dontusethispath/baz.so"],
3309 }
3310
3311 cc_test {
3312 name: "main_test",
3313 data_libs: ["test_lib"],
3314 gtest: false,
3315 }
3316 `
3317
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003318 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003319 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003320 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003321 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3322
3323 ctx := testCcWithConfig(t, config)
3324 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3325 testBinary := module.(*Module).linker.(*testBinary)
3326 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3327 if err != nil {
3328 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3329 }
3330 if len(outputFiles) != 1 {
3331 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3332 }
3333 if len(testBinary.dataPaths()) != 1 {
3334 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3335 }
3336
3337 outputPath := outputFiles[0].String()
3338
3339 if !strings.HasSuffix(outputPath, "/main_test") {
3340 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3341 }
Colin Crossaa255532020-07-03 13:18:24 -07003342 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003343 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3344 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3345 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3346 }
3347}
3348
Jiyong Park7ed9de32018-10-15 22:25:07 +09003349func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003350 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003351 ctx := testCc(t, `
3352 cc_library_shared {
3353 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003354 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003355 stubs: {
3356 symbol_file: "foo.map.txt",
3357 versions: ["1", "2", "3"],
3358 },
3359 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003360
Jiyong Park7ed9de32018-10-15 22:25:07 +09003361 cc_library_shared {
3362 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003363 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003364 shared_libs: ["libFoo#1"],
3365 }`)
3366
3367 variants := ctx.ModuleVariantsForTests("libFoo")
3368 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003369 "android_arm64_armv8-a_shared",
3370 "android_arm64_armv8-a_shared_1",
3371 "android_arm64_armv8-a_shared_2",
3372 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003373 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003374 "android_arm_armv7-a-neon_shared",
3375 "android_arm_armv7-a-neon_shared_1",
3376 "android_arm_armv7-a-neon_shared_2",
3377 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003378 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003379 }
3380 variantsMismatch := false
3381 if len(variants) != len(expectedVariants) {
3382 variantsMismatch = true
3383 } else {
3384 for _, v := range expectedVariants {
3385 if !inList(v, variants) {
3386 variantsMismatch = false
3387 }
3388 }
3389 }
3390 if variantsMismatch {
3391 t.Errorf("variants of libFoo expected:\n")
3392 for _, v := range expectedVariants {
3393 t.Errorf("%q\n", v)
3394 }
3395 t.Errorf(", but got:\n")
3396 for _, v := range variants {
3397 t.Errorf("%q\n", v)
3398 }
3399 }
3400
Colin Cross7113d202019-11-20 16:39:12 -08003401 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003402 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003403 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003404 if !strings.Contains(libFlags, libFoo1StubPath) {
3405 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3406 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003407
Colin Cross7113d202019-11-20 16:39:12 -08003408 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003409 cFlags := libBarCompileRule.Args["cFlags"]
3410 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3411 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3412 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3413 }
Jiyong Park37b25202018-07-11 10:49:27 +09003414}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003415
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003416func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3417 t.Parallel()
3418 ctx := testCc(t, `
3419 cc_library_shared {
3420 name: "libFoo",
3421 srcs: ["foo.c"],
3422 stubs: {
3423 symbol_file: "foo.map.txt",
3424 versions: ["current"],
3425 },
3426 apex_available: ["bar", "a1"],
3427 }
3428
3429 cc_library_shared {
3430 name: "libBar",
3431 srcs: ["bar.c"],
3432 shared_libs: ["libFoo"],
3433 apex_available: ["a1"],
3434 }
3435
3436 cc_library_shared {
3437 name: "libA1",
3438 srcs: ["a1.c"],
3439 shared_libs: ["libFoo"],
3440 apex_available: ["a1"],
3441 }
3442
3443 cc_library_shared {
3444 name: "libBarA1",
3445 srcs: ["bara1.c"],
3446 shared_libs: ["libFoo"],
3447 apex_available: ["bar", "a1"],
3448 }
3449
3450 cc_library_shared {
3451 name: "libAnyApex",
3452 srcs: ["anyApex.c"],
3453 shared_libs: ["libFoo"],
3454 apex_available: ["//apex_available:anyapex"],
3455 }
3456
3457 cc_library_shared {
3458 name: "libBaz",
3459 srcs: ["baz.c"],
3460 shared_libs: ["libFoo"],
3461 apex_available: ["baz"],
3462 }
3463
3464 cc_library_shared {
3465 name: "libQux",
3466 srcs: ["qux.c"],
3467 shared_libs: ["libFoo"],
3468 apex_available: ["qux", "bar"],
3469 }`)
3470
3471 variants := ctx.ModuleVariantsForTests("libFoo")
3472 expectedVariants := []string{
3473 "android_arm64_armv8-a_shared",
3474 "android_arm64_armv8-a_shared_current",
3475 "android_arm_armv7-a-neon_shared",
3476 "android_arm_armv7-a-neon_shared_current",
3477 }
3478 variantsMismatch := false
3479 if len(variants) != len(expectedVariants) {
3480 variantsMismatch = true
3481 } else {
3482 for _, v := range expectedVariants {
3483 if !inList(v, variants) {
3484 variantsMismatch = false
3485 }
3486 }
3487 }
3488 if variantsMismatch {
3489 t.Errorf("variants of libFoo expected:\n")
3490 for _, v := range expectedVariants {
3491 t.Errorf("%q\n", v)
3492 }
3493 t.Errorf(", but got:\n")
3494 for _, v := range variants {
3495 t.Errorf("%q\n", v)
3496 }
3497 }
3498
3499 linkAgainstFoo := []string{"libBarA1"}
3500 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3501
3502 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3503 for _, lib := range linkAgainstFoo {
3504 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3505 libFlags := libLinkRule.Args["libFlags"]
3506 if !strings.Contains(libFlags, libFooPath) {
3507 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3508 }
3509 }
3510
3511 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3512 for _, lib := range linkAgainstFooStubs {
3513 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3514 libFlags := libLinkRule.Args["libFlags"]
3515 if !strings.Contains(libFlags, libFooStubPath) {
3516 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3517 }
3518 }
3519}
3520
Sam Delmerico75dbca22023-04-20 13:13:25 +00003521func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003522 t.Parallel()
3523 bp := `
3524 cc_library_shared {
3525 name: "libFoo",
3526 bazel_module: { label: "//:libFoo" },
3527 srcs: ["foo.c"],
3528 stubs: {
3529 symbol_file: "foo.map.txt",
3530 versions: ["current"],
3531 },
3532 apex_available: ["bar", "a1"],
3533 }
3534
3535 cc_library_shared {
3536 name: "libBar",
3537 srcs: ["bar.c"],
3538 shared_libs: ["libFoo"],
3539 apex_available: ["a1"],
3540 }
3541
3542 cc_library_shared {
3543 name: "libA1",
3544 srcs: ["a1.c"],
3545 shared_libs: ["libFoo"],
3546 apex_available: ["a1"],
3547 }
3548
3549 cc_library_shared {
3550 name: "libBarA1",
3551 srcs: ["bara1.c"],
3552 shared_libs: ["libFoo"],
3553 apex_available: ["bar", "a1"],
3554 }
3555
3556 cc_library_shared {
3557 name: "libAnyApex",
3558 srcs: ["anyApex.c"],
3559 shared_libs: ["libFoo"],
3560 apex_available: ["//apex_available:anyapex"],
3561 }
3562
3563 cc_library_shared {
3564 name: "libBaz",
3565 srcs: ["baz.c"],
3566 shared_libs: ["libFoo"],
3567 apex_available: ["baz"],
3568 }
3569
3570 cc_library_shared {
3571 name: "libQux",
3572 srcs: ["qux.c"],
3573 shared_libs: ["libFoo"],
3574 apex_available: ["qux", "bar"],
3575 }`
3576
3577 result := android.GroupFixturePreparers(
3578 prepareForCcTest,
3579 android.FixtureModifyConfig(func(config android.Config) {
3580 config.BazelContext = android.MockBazelContext{
3581 OutputBaseDir: "out/bazel",
3582 LabelToCcInfo: map[string]cquery.CcInfo{
3583 "//:libFoo": {
3584 RootDynamicLibraries: []string{"libFoo.so"},
3585 },
3586 "//:libFoo_stub_libs-current": {
3587 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3588 },
3589 },
3590 }
3591 }),
3592 ).RunTestWithBp(t, bp)
3593 ctx := result.TestContext
3594
3595 variants := ctx.ModuleVariantsForTests("libFoo")
3596 expectedVariants := []string{
3597 "android_arm64_armv8-a_shared",
3598 "android_arm64_armv8-a_shared_current",
3599 "android_arm_armv7-a-neon_shared",
3600 "android_arm_armv7-a-neon_shared_current",
3601 }
3602 variantsMismatch := false
3603 if len(variants) != len(expectedVariants) {
3604 variantsMismatch = true
3605 } else {
3606 for _, v := range expectedVariants {
3607 if !inList(v, variants) {
3608 variantsMismatch = false
3609 }
3610 }
3611 }
3612 if variantsMismatch {
3613 t.Errorf("variants of libFoo expected:\n")
3614 for _, v := range expectedVariants {
3615 t.Errorf("%q\n", v)
3616 }
3617 t.Errorf(", but got:\n")
3618 for _, v := range variants {
3619 t.Errorf("%q\n", v)
3620 }
3621 }
3622
3623 linkAgainstFoo := []string{"libBarA1"}
3624 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3625
3626 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3627 for _, lib := range linkAgainstFoo {
3628 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3629 libFlags := libLinkRule.Args["libFlags"]
3630 if !strings.Contains(libFlags, libFooPath) {
3631 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3632 }
3633 }
3634
3635 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3636 for _, lib := range linkAgainstFooStubs {
3637 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3638 libFlags := libLinkRule.Args["libFlags"]
3639 if !strings.Contains(libFlags, libFooStubPath) {
3640 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3641 }
3642 }
3643}
3644
Jooyung Hanb04a4992020-03-13 18:57:35 +09003645func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003646 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003647 for _, tc := range []struct{ moduleName, expected string }{
3648 {"libc", "__LIBC_API__"},
3649 {"libfoo", "__LIBFOO_API__"},
3650 {"libfoo@1", "__LIBFOO_1_API__"},
3651 {"libfoo-v1", "__LIBFOO_V1_API__"},
3652 {"libfoo.v1", "__LIBFOO_V1_API__"},
3653 } {
3654 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3655 }
3656}
3657
Liz Kammer83cf81b2022-09-22 08:24:20 -04003658func pathsToBase(paths android.Paths) []string {
3659 var ret []string
3660 for _, p := range paths {
3661 ret = append(ret, p.Base())
3662 }
3663 return ret
3664}
3665
3666func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003667 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003668 ctx := testCc(t, `
3669 cc_library_static {
3670 name: "foo",
3671 srcs: ["foo.c"],
3672 }
3673
3674 cc_library_static {
3675 name: "bar",
3676 srcs: ["bar.c"],
3677 }
3678
3679 cc_library_shared {
3680 name: "qux",
3681 srcs: ["qux.c"],
3682 }
3683
3684 cc_library_static {
3685 name: "baz",
3686 srcs: ["baz.c"],
3687 static_libs: ["foo"],
3688 shared_libs: ["qux"],
3689 whole_static_libs: ["bar"],
3690 }`)
3691
3692 variant := "android_arm64_armv8-a_static"
3693 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3694
3695 // For static libraries, the object files of a whole static dep are included in the archive
3696 // directly
3697 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3698 t.Errorf("Expected input objects %q, got %q", w, g)
3699 }
3700
3701 // non whole static dependencies are not linked into the archive
3702 if len(arRule.Implicits) > 0 {
3703 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3704 }
3705}
3706
3707func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003708 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003709 ctx := testCc(t, `
3710 cc_library_static {
3711 name: "foo",
3712 srcs: ["foo.c"],
3713 }
3714
3715 cc_library_static {
3716 name: "bar",
3717 srcs: ["bar.c"],
3718 }
3719
3720 cc_library_shared {
3721 name: "qux",
3722 srcs: ["qux.c"],
3723 }
3724
3725 cc_library_shared {
3726 name: "baz",
3727 srcs: ["baz.c"],
3728 static_libs: ["foo"],
3729 shared_libs: ["qux"],
3730 whole_static_libs: ["bar"],
3731 }`)
3732
3733 variant := "android_arm64_armv8-a_shared"
3734 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3735 libFlags := linkRule.Args["libFlags"]
3736 // When dynamically linking, we expect static dependencies to be found on the command line
3737 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3738 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3739 }
3740 // When dynamically linking, we expect whole static dependencies to be found on the command line
3741 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3742 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3743 }
3744
3745 // When dynamically linking, we expect shared dependencies to be found on the command line
3746 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3747 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3748 }
3749
3750 // We should only have the objects from the shared library srcs, not the whole static dependencies
3751 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3752 t.Errorf("Expected input objects %q, got %q", w, g)
3753 }
3754}
3755
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003756func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003757 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003758 ctx := testCc(t, `
3759 cc_binary {
3760 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003761 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003762 static_executable: true,
3763 }`)
3764
Colin Cross7113d202019-11-20 16:39:12 -08003765 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003766 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3767 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003768 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003769 for _, lib := range systemStaticLibs {
3770 if !strings.Contains(libFlags, lib) {
3771 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3772 }
3773 }
3774 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3775 for _, lib := range systemSharedLibs {
3776 if strings.Contains(libFlags, lib) {
3777 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3778 }
3779 }
3780}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003781
3782func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003783 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003784 ctx := testCc(t, `
3785 cc_binary {
3786 name: "mybin",
3787 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003788 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003789 static_executable: true,
3790 stl: "none",
3791 }
3792
3793 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003794 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003795 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003796 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003797 stl: "none",
3798 }
3799
3800 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003801 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003802 srcs: ["foo.c"],
3803 stl: "none",
3804 stubs: {
3805 versions: ["1"],
3806 },
3807 }`)
3808
Colin Cross0de8a1e2020-09-18 14:15:30 -07003809 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3810 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003811 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003812
3813 if !reflect.DeepEqual(actual, expected) {
3814 t.Errorf("staticDeps orderings were not propagated correctly"+
3815 "\nactual: %v"+
3816 "\nexpected: %v",
3817 actual,
3818 expected,
3819 )
3820 }
3821}
Jooyung Han38002912019-05-16 04:01:54 +09003822
Jooyung Hand48f3c32019-08-23 11:18:57 +09003823func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003824 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003825 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3826 cc_library {
3827 name: "libA",
3828 srcs: ["foo.c"],
3829 shared_libs: ["libB"],
3830 stl: "none",
3831 }
3832
3833 cc_library {
3834 name: "libB",
3835 srcs: ["foo.c"],
3836 enabled: false,
3837 stl: "none",
3838 }
3839 `)
3840}
3841
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003842func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3843 bp := `
3844 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003845 name: "test_afl_fuzz_target",
3846 srcs: ["foo.c"],
3847 host_supported: true,
3848 static_libs: [
3849 "afl_fuzz_static_lib",
3850 ],
3851 shared_libs: [
3852 "afl_fuzz_shared_lib",
3853 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003854 fuzzing_frameworks: {
3855 afl: true,
3856 libfuzzer: false,
3857 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003858 }
3859 cc_library {
3860 name: "afl_fuzz_static_lib",
3861 host_supported: true,
3862 srcs: ["static_file.c"],
3863 }
3864 cc_library {
3865 name: "libfuzzer_only_static_lib",
3866 host_supported: true,
3867 srcs: ["static_file.c"],
3868 }
3869 cc_library {
3870 name: "afl_fuzz_shared_lib",
3871 host_supported: true,
3872 srcs: ["shared_file.c"],
3873 static_libs: [
3874 "second_static_lib",
3875 ],
3876 }
3877 cc_library_headers {
3878 name: "libafl_headers",
3879 vendor_available: true,
3880 host_supported: true,
3881 export_include_dirs: [
3882 "include",
3883 "instrumentation",
3884 ],
3885 }
3886 cc_object {
3887 name: "afl-compiler-rt",
3888 vendor_available: true,
3889 host_supported: true,
3890 cflags: [
3891 "-fPIC",
3892 ],
3893 srcs: [
3894 "instrumentation/afl-compiler-rt.o.c",
3895 ],
3896 }
3897 cc_library {
3898 name: "second_static_lib",
3899 host_supported: true,
3900 srcs: ["second_file.c"],
3901 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003902 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003903 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003904 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003905 srcs: [
3906 "aflpp_driver.c",
3907 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003908 }`
3909
3910 testEnv := map[string]string{
3911 "FUZZ_FRAMEWORK": "AFL",
3912 }
3913
3914 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003915
3916 checkPcGuardFlag := func(
3917 modName string, variantName string, shouldHave bool) {
3918 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3919
3920 cFlags, ok := cc.Args["cFlags"]
3921 if !ok {
3922 t.Errorf("Could not find cFlags for module %s and variant %s",
3923 modName, variantName)
3924 }
3925
3926 if strings.Contains(
3927 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3928 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3929 "Test failed for module %s and variant %s",
3930 !shouldHave, shouldHave, modName, variantName)
3931 }
3932 }
3933
Cory Barkera1da26f2022-06-07 20:12:06 +00003934 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003935 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003936
3937 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003938 checkPcGuardFlag(moduleName, variant+"_static", false)
3939 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003940
3941 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003942 checkPcGuardFlag(moduleName, variant+"_static", false)
3943 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003944
3945 ctx.ModuleForTests("afl_fuzz_shared_lib",
3946 "android_arm64_armv8-a_shared").Rule("cc")
3947 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003948 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3949}
3950
3951func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003952 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003953 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3954}
3955
3956func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003957 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003958 if runtime.GOOS != "linux" {
3959 t.Skip("requires linux")
3960 }
3961
3962 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003963}
3964
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003965// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3966// correctly.
3967func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003968 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003969 ctx := testCc(t, `
3970 cc_fuzz {
3971 name: "fuzz_smoke_test",
3972 srcs: ["foo.c"],
3973 }`)
3974
Paul Duffin075c4172019-12-19 19:06:13 +00003975 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003976 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3977}
3978
Jooyung Han38002912019-05-16 04:01:54 +09003979func assertString(t *testing.T, got, expected string) {
3980 t.Helper()
3981 if got != expected {
3982 t.Errorf("expected %q got %q", expected, got)
3983 }
3984}
3985
3986func assertArrayString(t *testing.T, got, expected []string) {
3987 t.Helper()
3988 if len(got) != len(expected) {
3989 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3990 return
3991 }
3992 for i := range got {
3993 if got[i] != expected[i] {
3994 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3995 i, expected[i], expected, got[i], got)
3996 return
3997 }
3998 }
3999}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004000
Jooyung Han0302a842019-10-30 18:43:49 +09004001func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4002 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004003 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004004}
4005
Colin Crosse1bb5d02019-09-24 14:55:04 -07004006func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004007 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004008 ctx := testCc(t, `
4009 cc_defaults {
4010 name: "defaults",
4011 srcs: ["foo.c"],
4012 static: {
4013 srcs: ["bar.c"],
4014 },
4015 shared: {
4016 srcs: ["baz.c"],
4017 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004018 bazel_module: {
4019 bp2build_available: true,
4020 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004021 }
4022
4023 cc_library_static {
4024 name: "libstatic",
4025 defaults: ["defaults"],
4026 }
4027
4028 cc_library_shared {
4029 name: "libshared",
4030 defaults: ["defaults"],
4031 }
4032
4033 cc_library {
4034 name: "libboth",
4035 defaults: ["defaults"],
4036 }
4037
4038 cc_binary {
4039 name: "binary",
4040 defaults: ["defaults"],
4041 }`)
4042
Colin Cross7113d202019-11-20 16:39:12 -08004043 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004044 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4045 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4046 }
Colin Cross7113d202019-11-20 16:39:12 -08004047 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004048 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4049 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4050 }
Colin Cross7113d202019-11-20 16:39:12 -08004051 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004052 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4053 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4054 }
4055
Colin Cross7113d202019-11-20 16:39:12 -08004056 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004057 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4058 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4059 }
Colin Cross7113d202019-11-20 16:39:12 -08004060 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004061 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4062 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4063 }
4064}
Colin Crosseabaedd2020-02-06 17:01:55 -08004065
4066func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004067 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004068 bp := `
4069 cc_defaults {
4070 name: "libfoo_defaults",
4071 srcs: ["foo.c"],
4072 cppflags: ["-DFOO"],
4073 product_variables: {
4074 debuggable: {
4075 cppflags: ["-DBAR"],
4076 },
4077 },
4078 }
4079
4080 cc_library {
4081 name: "libfoo",
4082 defaults: ["libfoo_defaults"],
4083 }
4084 `
4085
Paul Duffin8567f222021-03-23 00:02:06 +00004086 result := android.GroupFixturePreparers(
4087 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004088 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004089
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004090 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4091 variables.Debuggable = BoolPtr(true)
4092 }),
4093 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004094
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004095 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004096 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004097}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004098
4099func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4100 t.Parallel()
4101 bp := `
4102 cc_library_static {
4103 name: "libfoo",
4104 srcs: ["foo.c"],
4105 whole_static_libs: ["libbar"],
4106 }
4107
4108 cc_library_static {
4109 name: "libbar",
4110 whole_static_libs: ["libmissing"],
4111 }
4112 `
4113
Paul Duffin8567f222021-03-23 00:02:06 +00004114 result := android.GroupFixturePreparers(
4115 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004116 android.PrepareForTestWithAllowMissingDependencies,
4117 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004118
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004119 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004120 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004121
Paul Duffine84b1332021-03-12 11:59:43 +00004122 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004123
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004124 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004125 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004126}
Colin Crosse9fe2942020-11-10 18:12:15 -08004127
4128func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004129 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004130 bp := `
4131 cc_binary {
4132 name: "bin",
4133 host_supported: true,
4134 shared_libs: ["libshared"],
4135 runtime_libs: ["libruntime"],
4136 srcs: [":gen"],
4137 }
4138
4139 cc_library_shared {
4140 name: "libshared",
4141 host_supported: true,
4142 shared_libs: ["libtransitive"],
4143 }
4144
4145 cc_library_shared {
4146 name: "libtransitive",
4147 host_supported: true,
4148 }
4149
4150 cc_library_shared {
4151 name: "libruntime",
4152 host_supported: true,
4153 }
4154
4155 cc_binary_host {
4156 name: "tool",
4157 srcs: ["foo.cpp"],
4158 }
4159
4160 genrule {
4161 name: "gen",
4162 tools: ["tool"],
4163 out: ["gen.cpp"],
4164 cmd: "$(location tool) $(out)",
4165 }
4166 `
4167
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004168 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004169 ctx := testCcWithConfig(t, config)
4170
4171 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4172 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4173 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4174 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4175 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4176
4177 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4178 t.Errorf("expected host bin dependency %q, got %q", w, g)
4179 }
4180
4181 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4182 t.Errorf("expected host bin dependency %q, got %q", w, g)
4183 }
4184
4185 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4186 t.Errorf("expected host bin dependency %q, got %q", w, g)
4187 }
4188
4189 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4190 t.Errorf("expected host bin dependency %q, got %q", w, g)
4191 }
4192
4193 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4194 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4195 }
4196
4197 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4198 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4199 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4200 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4201
4202 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4203 t.Errorf("expected device bin dependency %q, got %q", w, g)
4204 }
4205
4206 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4207 t.Errorf("expected device bin dependency %q, got %q", w, g)
4208 }
4209
4210 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4211 t.Errorf("expected device bin dependency %q, got %q", w, g)
4212 }
4213
4214 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4215 t.Errorf("expected device bin dependency %q, got %q", w, g)
4216 }
4217
4218 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4219 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4220 }
4221
4222}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004223
4224func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004225 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004226 ctx := testCc(t, `
4227 cc_library_shared {
4228 name: "libclient",
4229 srcs: ["foo.c"],
4230 shared_libs: ["libfoo#1"],
4231 }
4232
4233 cc_library_shared {
4234 name: "libfoo",
4235 srcs: ["foo.c"],
4236 shared_libs: ["libbar"],
4237 export_shared_lib_headers: ["libbar"],
4238 stubs: {
4239 symbol_file: "foo.map.txt",
4240 versions: ["1", "2", "3"],
4241 },
4242 }
4243
4244 cc_library_shared {
4245 name: "libbar",
4246 export_include_dirs: ["include/libbar"],
4247 srcs: ["foo.c"],
4248 }`)
4249
4250 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4251
4252 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4253 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4254 }
4255}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004256
Vinh Tran09581952023-05-16 16:03:20 -04004257func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004258 t.Parallel()
4259 ctx := android.GroupFixturePreparers(
4260 prepareForCcTest,
4261 aidl_library.PrepareForTestWithAidlLibrary,
4262 android.MockFS{
4263 "package_bar/Android.bp": []byte(`
4264 aidl_library {
4265 name: "bar",
4266 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004267 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004268 strip_import_prefix: "x",
4269 }
4270 `)}.AddToFixture(),
4271 android.MockFS{
4272 "package_foo/Android.bp": []byte(`
4273 aidl_library {
4274 name: "foo",
4275 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004276 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004277 strip_import_prefix: "a",
4278 deps: ["bar"],
4279 }
4280 cc_library {
4281 name: "libfoo",
4282 aidl: {
4283 libs: ["foo"],
4284 }
4285 }
4286 `),
4287 }.AddToFixture(),
4288 ).RunTest(t).TestContext
4289
4290 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004291
4292 android.AssertPathsRelativeToTopEquals(
4293 t,
4294 "aidl headers",
4295 []string{
4296 "package_bar/x/HeaderBar.aidl",
4297 "package_foo/a/HeaderFoo.aidl",
4298 "package_foo/a/b/Foo.aidl",
4299 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4300 },
4301 libfoo.Rule("aidl_library").Implicits,
4302 )
4303
4304 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004305 aidlCommand := manifest.Commands[0].GetCommand()
4306
4307 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4308 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4309 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4310 }
4311
4312 outputs := strings.Join(libfoo.AllOutputs(), " ")
4313
Vinh Tran09581952023-05-16 16:03:20 -04004314 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4315 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4316 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004317 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4318 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004319 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4320 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4321 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004322 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4323}
4324
Jooyung Hane197d8b2021-01-05 10:33:16 +09004325func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004326 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004327 ctx := android.GroupFixturePreparers(
4328 prepareForCcTest,
4329 aidl_library.PrepareForTestWithAidlLibrary,
4330 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004331 cc_library {
4332 name: "libfoo",
4333 srcs: ["a/Foo.aidl"],
4334 aidl: { flags: ["-Werror"], },
4335 }
4336 `)
4337
4338 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4339 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4340 aidlCommand := manifest.Commands[0].GetCommand()
4341 expectedAidlFlag := "-Werror"
4342 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4343 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4344 }
4345}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004346
Jooyung Han07f70c02021-11-06 07:08:45 +09004347func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004348 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004349 for _, tc := range []struct {
4350 name string
4351 sdkVersion string
4352 variant string
4353 expected string
4354 }{
4355 {
4356 name: "default is current",
4357 sdkVersion: "",
4358 variant: "android_arm64_armv8-a_static",
4359 expected: "platform_apis",
4360 },
4361 {
4362 name: "use sdk_version",
4363 sdkVersion: `sdk_version: "29"`,
4364 variant: "android_arm64_armv8-a_static",
4365 expected: "platform_apis",
4366 },
4367 {
4368 name: "use sdk_version(sdk variant)",
4369 sdkVersion: `sdk_version: "29"`,
4370 variant: "android_arm64_armv8-a_sdk_static",
4371 expected: "29",
4372 },
4373 {
4374 name: "use min_sdk_version",
4375 sdkVersion: `min_sdk_version: "29"`,
4376 variant: "android_arm64_armv8-a_static",
4377 expected: "29",
4378 },
4379 } {
4380 t.Run(tc.name, func(t *testing.T) {
4381 ctx := testCc(t, `
4382 cc_library {
4383 name: "libfoo",
4384 stl: "none",
4385 srcs: ["a/Foo.aidl"],
4386 `+tc.sdkVersion+`
4387 }
4388 `)
4389 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4390 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4391 aidlCommand := manifest.Commands[0].GetCommand()
4392 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4393 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4394 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4395 }
4396 })
4397 }
4398}
4399
Vinh Tran09581952023-05-16 16:03:20 -04004400func TestInvalidAidlProp(t *testing.T) {
4401 t.Parallel()
4402
4403 testCases := []struct {
4404 description string
4405 bp string
4406 }{
4407 {
4408 description: "Invalid use of aidl.libs and aidl.include_dirs",
4409 bp: `
4410 cc_library {
4411 name: "foo",
4412 aidl: {
4413 libs: ["foo_aidl"],
4414 include_dirs: ["bar/include"],
4415 }
4416 }
4417 `,
4418 },
4419 {
4420 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4421 bp: `
4422 cc_library {
4423 name: "foo",
4424 aidl: {
4425 libs: ["foo_aidl"],
4426 local_include_dirs: ["include"],
4427 }
4428 }
4429 `,
4430 },
4431 }
4432
4433 for _, testCase := range testCases {
4434 t.Run(testCase.description, func(t *testing.T) {
4435 bp := `
4436 aidl_library {
4437 name: "foo_aidl",
4438 srcs: ["Foo.aidl"],
4439 } ` + testCase.bp
4440 android.GroupFixturePreparers(
4441 prepareForCcTest,
4442 aidl_library.PrepareForTestWithAidlLibrary.
4443 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4444 ).RunTestWithBp(t, bp)
4445 })
4446 }
4447}
4448
Jiyong Parka008fb02021-03-16 17:15:53 +09004449func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004450 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004451 ctx := testCc(t, `
4452 cc_library_shared {
4453 name: "libfoo",
4454 srcs: ["foo.c"],
4455 min_sdk_version: "29",
4456 }`)
4457
4458 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4459 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4460}
4461
Vinh Tranf1924742022-06-24 16:40:11 -04004462func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004463 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004464 bp := `
4465 cc_library_shared {
4466 name: "libfoo",
4467 srcs: ["foo.c"],
4468 min_sdk_version: "S",
4469 }
4470 `
4471 result := android.GroupFixturePreparers(
4472 prepareForCcTest,
4473 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4474 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4475 }),
4476 ).RunTestWithBp(t, bp)
4477 ctx := result.TestContext
4478 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4479 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4480}
4481
Paul Duffin3cb603e2021-02-19 13:57:10 +00004482func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004483 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004484
4485 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4486 // embedded newline characters alone.
4487 trimIndentingSpaces := func(s string) string {
4488 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4489 }
4490
4491 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4492 t.Helper()
4493 expected = trimIndentingSpaces(expected)
4494 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4495 if expected != actual {
4496 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4497 }
4498 }
4499
4500 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4501
4502 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4503 t.Helper()
4504 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4505 name := module.Name()
4506
4507 for _, checker := range checkers {
4508 checker(t, name, exported)
4509 }
4510 }
4511
4512 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4513 return func(t *testing.T, name string, exported FlagExporterInfo) {
4514 t.Helper()
4515 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4516 }
4517 }
4518
4519 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4520 return func(t *testing.T, name string, exported FlagExporterInfo) {
4521 t.Helper()
4522 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4523 }
4524 }
4525
4526 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4527 return func(t *testing.T, name string, exported FlagExporterInfo) {
4528 t.Helper()
4529 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4530 }
4531 }
4532
4533 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4534 return func(t *testing.T, name string, exported FlagExporterInfo) {
4535 t.Helper()
4536 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4537 }
4538 }
4539
4540 genRuleModules := `
4541 genrule {
4542 name: "genrule_foo",
4543 cmd: "generate-foo",
4544 out: [
4545 "generated_headers/foo/generated_header.h",
4546 ],
4547 export_include_dirs: [
4548 "generated_headers",
4549 ],
4550 }
4551
4552 genrule {
4553 name: "genrule_bar",
4554 cmd: "generate-bar",
4555 out: [
4556 "generated_headers/bar/generated_header.h",
4557 ],
4558 export_include_dirs: [
4559 "generated_headers",
4560 ],
4561 }
4562 `
4563
4564 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4565 ctx := testCc(t, genRuleModules+`
4566 cc_library {
4567 name: "libfoo",
4568 srcs: ["foo.c"],
4569 export_include_dirs: ["foo/standard"],
4570 export_system_include_dirs: ["foo/system"],
4571 generated_headers: ["genrule_foo"],
4572 export_generated_headers: ["genrule_foo"],
4573 }
4574
4575 cc_library {
4576 name: "libbar",
4577 srcs: ["bar.c"],
4578 shared_libs: ["libfoo"],
4579 export_include_dirs: ["bar/standard"],
4580 export_system_include_dirs: ["bar/system"],
4581 generated_headers: ["genrule_bar"],
4582 export_generated_headers: ["genrule_bar"],
4583 }
4584 `)
4585 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4586 checkIncludeDirs(t, ctx, foo,
4587 expectedIncludeDirs(`
4588 foo/standard
4589 .intermediates/genrule_foo/gen/generated_headers
4590 `),
4591 expectedSystemIncludeDirs(`foo/system`),
4592 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4593 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4594 )
4595
4596 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4597 checkIncludeDirs(t, ctx, bar,
4598 expectedIncludeDirs(`
4599 bar/standard
4600 .intermediates/genrule_bar/gen/generated_headers
4601 `),
4602 expectedSystemIncludeDirs(`bar/system`),
4603 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4604 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4605 )
4606 })
4607
4608 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4609 ctx := testCc(t, genRuleModules+`
4610 cc_library {
4611 name: "libfoo",
4612 srcs: ["foo.c"],
4613 export_include_dirs: ["foo/standard"],
4614 export_system_include_dirs: ["foo/system"],
4615 generated_headers: ["genrule_foo"],
4616 export_generated_headers: ["genrule_foo"],
4617 }
4618
4619 cc_library {
4620 name: "libbar",
4621 srcs: ["bar.c"],
4622 whole_static_libs: ["libfoo"],
4623 export_include_dirs: ["bar/standard"],
4624 export_system_include_dirs: ["bar/system"],
4625 generated_headers: ["genrule_bar"],
4626 export_generated_headers: ["genrule_bar"],
4627 }
4628 `)
4629 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4630 checkIncludeDirs(t, ctx, foo,
4631 expectedIncludeDirs(`
4632 foo/standard
4633 .intermediates/genrule_foo/gen/generated_headers
4634 `),
4635 expectedSystemIncludeDirs(`foo/system`),
4636 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4637 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4638 )
4639
4640 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4641 checkIncludeDirs(t, ctx, bar,
4642 expectedIncludeDirs(`
4643 bar/standard
4644 foo/standard
4645 .intermediates/genrule_foo/gen/generated_headers
4646 .intermediates/genrule_bar/gen/generated_headers
4647 `),
4648 expectedSystemIncludeDirs(`
4649 bar/system
4650 foo/system
4651 `),
4652 expectedGeneratedHeaders(`
4653 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4654 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4655 `),
4656 expectedOrderOnlyDeps(`
4657 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4658 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4659 `),
4660 )
4661 })
4662
Paul Duffin3cb603e2021-02-19 13:57:10 +00004663 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004664 ctx := android.GroupFixturePreparers(
4665 prepareForCcTest,
4666 aidl_library.PrepareForTestWithAidlLibrary,
4667 ).RunTestWithBp(t, `
4668 aidl_library {
4669 name: "libfoo_aidl",
4670 srcs: ["x/y/Bar.aidl"],
4671 strip_import_prefix: "x",
4672 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004673 cc_library_shared {
4674 name: "libfoo",
4675 srcs: [
4676 "foo.c",
4677 "b.aidl",
4678 "a.proto",
4679 ],
4680 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004681 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004682 export_aidl_headers: true,
4683 }
4684 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004685 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004686 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4687 checkIncludeDirs(t, ctx, foo,
4688 expectedIncludeDirs(`
4689 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004690 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004691 `),
4692 expectedSystemIncludeDirs(``),
4693 expectedGeneratedHeaders(`
4694 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4695 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4696 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004697 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4698 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4699 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004700 `),
4701 expectedOrderOnlyDeps(`
4702 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4703 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4704 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004705 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4706 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4707 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004708 `),
4709 )
4710 })
4711
Paul Duffin3cb603e2021-02-19 13:57:10 +00004712 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4713 ctx := testCc(t, genRuleModules+`
4714 cc_library_shared {
4715 name: "libfoo",
4716 srcs: [
4717 "foo.c",
4718 "b.aidl",
4719 "a.proto",
4720 ],
4721 proto: {
4722 export_proto_headers: true,
4723 }
4724 }
4725 `)
4726 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4727 checkIncludeDirs(t, ctx, foo,
4728 expectedIncludeDirs(`
4729 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4730 `),
4731 expectedSystemIncludeDirs(``),
4732 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004733 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4734 `),
4735 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004736 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4737 `),
4738 )
4739 })
4740
Paul Duffin33056e82021-02-19 13:49:08 +00004741 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004742 ctx := testCc(t, genRuleModules+`
4743 cc_library_shared {
4744 name: "libfoo",
4745 srcs: [
4746 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004747 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004748 "b.aidl",
4749 "a.proto",
4750 ],
4751 }
4752 `)
4753 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4754 checkIncludeDirs(t, ctx, foo,
4755 expectedIncludeDirs(`
4756 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4757 `),
4758 expectedSystemIncludeDirs(``),
4759 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004760 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004761 `),
4762 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004763 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4764 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004765 `),
4766 )
4767 })
4768}
Colin Crossae628182021-06-14 16:52:28 -07004769
4770func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004771 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004772 baseExpectedFlags := []string{
4773 "${config.ArmThumbCflags}",
4774 "${config.ArmCflags}",
4775 "${config.CommonGlobalCflags}",
4776 "${config.DeviceGlobalCflags}",
4777 "${config.ExternalCflags}",
4778 "${config.ArmToolchainCflags}",
4779 "${config.ArmArmv7ANeonCflags}",
4780 "${config.ArmGenericCflags}",
4781 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004782 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004783 }
4784
4785 expectedIncludes := []string{
4786 "external/foo/android_arm_export_include_dirs",
4787 "external/foo/lib32_export_include_dirs",
4788 "external/foo/arm_export_include_dirs",
4789 "external/foo/android_export_include_dirs",
4790 "external/foo/linux_export_include_dirs",
4791 "external/foo/export_include_dirs",
4792 "external/foo/android_arm_local_include_dirs",
4793 "external/foo/lib32_local_include_dirs",
4794 "external/foo/arm_local_include_dirs",
4795 "external/foo/android_local_include_dirs",
4796 "external/foo/linux_local_include_dirs",
4797 "external/foo/local_include_dirs",
4798 "external/foo",
4799 "external/foo/libheader1",
4800 "external/foo/libheader2",
4801 "external/foo/libwhole1",
4802 "external/foo/libwhole2",
4803 "external/foo/libstatic1",
4804 "external/foo/libstatic2",
4805 "external/foo/libshared1",
4806 "external/foo/libshared2",
4807 "external/foo/liblinux",
4808 "external/foo/libandroid",
4809 "external/foo/libarm",
4810 "external/foo/lib32",
4811 "external/foo/libandroid_arm",
4812 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004813 }
4814
4815 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4816 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4817
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004818 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004819 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004820 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004821
4822 lastIncludes := []string{
4823 "out/soong/ndk/sysroot/usr/include",
4824 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4825 }
4826
4827 combineSlices := func(slices ...[]string) []string {
4828 var ret []string
4829 for _, s := range slices {
4830 ret = append(ret, s...)
4831 }
4832 return ret
4833 }
4834
4835 testCases := []struct {
4836 name string
4837 src string
4838 expected []string
4839 }{
4840 {
4841 name: "c",
4842 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004843 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004844 },
4845 {
4846 name: "cc",
4847 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004848 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004849 },
4850 {
4851 name: "assemble",
4852 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004853 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004854 },
4855 }
4856
4857 for _, tc := range testCases {
4858 t.Run(tc.name, func(t *testing.T) {
4859 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004860 cc_library {
4861 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004862 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004863 cflags: ["-std=candcpp"],
4864 conlyflags: ["-std=conly"],
4865 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004866 local_include_dirs: ["local_include_dirs"],
4867 export_include_dirs: ["export_include_dirs"],
4868 export_system_include_dirs: ["export_system_include_dirs"],
4869 static_libs: ["libstatic1", "libstatic2"],
4870 whole_static_libs: ["libwhole1", "libwhole2"],
4871 shared_libs: ["libshared1", "libshared2"],
4872 header_libs: ["libheader1", "libheader2"],
4873 target: {
4874 android: {
4875 shared_libs: ["libandroid"],
4876 local_include_dirs: ["android_local_include_dirs"],
4877 export_include_dirs: ["android_export_include_dirs"],
4878 },
4879 android_arm: {
4880 shared_libs: ["libandroid_arm"],
4881 local_include_dirs: ["android_arm_local_include_dirs"],
4882 export_include_dirs: ["android_arm_export_include_dirs"],
4883 },
4884 linux: {
4885 shared_libs: ["liblinux"],
4886 local_include_dirs: ["linux_local_include_dirs"],
4887 export_include_dirs: ["linux_export_include_dirs"],
4888 },
4889 },
4890 multilib: {
4891 lib32: {
4892 shared_libs: ["lib32"],
4893 local_include_dirs: ["lib32_local_include_dirs"],
4894 export_include_dirs: ["lib32_export_include_dirs"],
4895 },
4896 },
4897 arch: {
4898 arm: {
4899 shared_libs: ["libarm"],
4900 local_include_dirs: ["arm_local_include_dirs"],
4901 export_include_dirs: ["arm_export_include_dirs"],
4902 },
4903 },
4904 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004905 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004906 }
4907
4908 cc_library_headers {
4909 name: "libheader1",
4910 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004911 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004912 stl: "none",
4913 }
4914
4915 cc_library_headers {
4916 name: "libheader2",
4917 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004918 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004919 stl: "none",
4920 }
Liz Kammer08572c62021-09-30 10:11:04 -04004921 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004922
Liz Kammer08572c62021-09-30 10:11:04 -04004923 libs := []string{
4924 "libstatic1",
4925 "libstatic2",
4926 "libwhole1",
4927 "libwhole2",
4928 "libshared1",
4929 "libshared2",
4930 "libandroid",
4931 "libandroid_arm",
4932 "liblinux",
4933 "lib32",
4934 "libarm",
4935 }
Colin Crossae628182021-06-14 16:52:28 -07004936
Liz Kammer08572c62021-09-30 10:11:04 -04004937 for _, lib := range libs {
4938 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004939 cc_library {
4940 name: "%s",
4941 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004942 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004943 stl: "none",
4944 }
4945 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004946 }
4947
4948 ctx := android.GroupFixturePreparers(
4949 PrepareForIntegrationTestWithCc,
4950 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4951 ).RunTest(t)
4952 // Use the arm variant instead of the arm64 variant so that it gets headers from
4953 // ndk_libandroid_support to test LateStaticLibs.
4954 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4955
4956 var includes []string
4957 flags := strings.Split(cflags, " ")
4958 for _, flag := range flags {
4959 if strings.HasPrefix(flag, "-I") {
4960 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4961 } else if flag == "-isystem" {
4962 // skip isystem, include next
4963 } else if len(flag) > 0 {
4964 includes = append(includes, flag)
4965 }
4966 }
4967
4968 android.AssertArrayString(t, "includes", tc.expected, includes)
4969 })
Colin Crossae628182021-06-14 16:52:28 -07004970 }
4971
Colin Crossae628182021-06-14 16:52:28 -07004972}
Alixb5f6d9e2022-04-20 23:00:58 +00004973
zijunzhao933e3802023-01-12 07:26:20 +00004974func TestAddnoOverride64GlobalCflags(t *testing.T) {
4975 t.Parallel()
4976 ctx := testCc(t, `
4977 cc_library_shared {
4978 name: "libclient",
4979 srcs: ["foo.c"],
4980 shared_libs: ["libfoo#1"],
4981 }
4982
4983 cc_library_shared {
4984 name: "libfoo",
4985 srcs: ["foo.c"],
4986 shared_libs: ["libbar"],
4987 export_shared_lib_headers: ["libbar"],
4988 stubs: {
4989 symbol_file: "foo.map.txt",
4990 versions: ["1", "2", "3"],
4991 },
4992 }
4993
4994 cc_library_shared {
4995 name: "libbar",
4996 export_include_dirs: ["include/libbar"],
4997 srcs: ["foo.c"],
4998 }`)
4999
5000 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5001
5002 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5003 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5004 }
5005}
5006
Alixb5f6d9e2022-04-20 23:00:58 +00005007func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005008 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005009 tests := []struct {
5010 name string
5011 clang bool
5012 BuildBrokenClangProperty bool
5013 err string
5014 }{
5015 {
5016 name: "error when clang is set to false",
5017 clang: false,
5018 err: "is no longer supported",
5019 },
5020 {
5021 name: "error when clang is set to true",
5022 clang: true,
5023 err: "property is deprecated, see Changes.md",
5024 },
5025 {
5026 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5027 clang: true,
5028 BuildBrokenClangProperty: true,
5029 },
5030 }
5031
5032 for _, test := range tests {
5033 t.Run(test.name, func(t *testing.T) {
5034 bp := fmt.Sprintf(`
5035 cc_library {
5036 name: "foo",
5037 clang: %t,
5038 }`, test.clang)
5039
5040 if test.err == "" {
5041 android.GroupFixturePreparers(
5042 prepareForCcTest,
5043 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5044 if test.BuildBrokenClangProperty {
5045 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5046 }
5047 }),
5048 ).RunTestWithBp(t, bp)
5049 } else {
5050 prepareForCcTest.
5051 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5052 RunTestWithBp(t, bp)
5053 }
5054 })
5055 }
5056}
Alix Espinoef47e542022-09-14 19:10:51 +00005057
5058func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005059 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005060 tests := []struct {
5061 name string
5062 clangAsFlags []string
5063 BuildBrokenClangAsFlags bool
5064 err string
5065 }{
5066 {
5067 name: "error when clang_asflags is set",
5068 clangAsFlags: []string{"-a", "-b"},
5069 err: "clang_asflags: property is deprecated",
5070 },
5071 {
5072 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5073 clangAsFlags: []string{"-a", "-b"},
5074 BuildBrokenClangAsFlags: true,
5075 },
5076 }
5077
5078 for _, test := range tests {
5079 t.Run(test.name, func(t *testing.T) {
5080 bp := fmt.Sprintf(`
5081 cc_library {
5082 name: "foo",
5083 clang_asflags: %s,
5084 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5085
5086 if test.err == "" {
5087 android.GroupFixturePreparers(
5088 prepareForCcTest,
5089 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5090 if test.BuildBrokenClangAsFlags {
5091 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5092 }
5093 }),
5094 ).RunTestWithBp(t, bp)
5095 } else {
5096 prepareForCcTest.
5097 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5098 RunTestWithBp(t, bp)
5099 }
5100 })
5101 }
5102}
5103
5104func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005105 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005106 tests := []struct {
5107 name string
5108 clangCFlags []string
5109 BuildBrokenClangCFlags bool
5110 err string
5111 }{
5112 {
5113 name: "error when clang_cflags is set",
5114 clangCFlags: []string{"-a", "-b"},
5115 err: "clang_cflags: property is deprecated",
5116 },
5117 {
5118 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5119 clangCFlags: []string{"-a", "-b"},
5120 BuildBrokenClangCFlags: true,
5121 },
5122 }
5123
5124 for _, test := range tests {
5125 t.Run(test.name, func(t *testing.T) {
5126 bp := fmt.Sprintf(`
5127 cc_library {
5128 name: "foo",
5129 clang_cflags: %s,
5130 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5131
5132 if test.err == "" {
5133 android.GroupFixturePreparers(
5134 prepareForCcTest,
5135 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5136 if test.BuildBrokenClangCFlags {
5137 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5138 }
5139 }),
5140 ).RunTestWithBp(t, bp)
5141 } else {
5142 prepareForCcTest.
5143 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5144 RunTestWithBp(t, bp)
5145 }
5146 })
5147 }
5148}
Yu Liue4312402023-01-18 09:15:31 -08005149
5150func TestDclaLibraryInApex(t *testing.T) {
5151 t.Parallel()
5152 bp := `
5153 cc_library_shared {
5154 name: "cc_lib_in_apex",
5155 srcs: ["foo.cc"],
5156 apex_available: ["myapex"],
5157 bazel_module: { label: "//foo/bar:bar" },
5158 }`
5159 label := "//foo/bar:bar"
5160 arch64 := "arm64_armv8-a"
5161 arch32 := "arm_armv7-a-neon"
5162 apexCfgKey := android.ApexConfigKey{
5163 WithinApex: true,
5164 ApexSdkVersion: "28",
5165 }
5166
5167 result := android.GroupFixturePreparers(
5168 prepareForCcTest,
5169 android.FixtureRegisterWithContext(registerTestMutators),
5170 android.FixtureModifyConfig(func(config android.Config) {
5171 config.BazelContext = android.MockBazelContext{
5172 OutputBaseDir: "outputbase",
5173 LabelToCcInfo: map[string]cquery.CcInfo{
5174 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5175 RootDynamicLibraries: []string{"foo.so"},
5176 },
5177 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5178 RootDynamicLibraries: []string{"foo.so"},
5179 },
5180 },
5181 BazelRequests: make(map[string]bool),
5182 }
5183 }),
5184 ).RunTestWithBp(t, bp)
5185 ctx := result.TestContext
5186
5187 // Test if the bazel request is queued correctly
5188 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5189 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5190 t.Errorf("Bazel request was not queued: %s", key)
5191 }
5192
5193 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5194 producer := sharedFoo.(android.OutputFileProducer)
5195 outputFiles, err := producer.OutputFiles("")
5196 if err != nil {
5197 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5198 }
5199 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5200 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5201}
Sam Delmericoef69d472023-04-18 17:32:43 -04005202
5203func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5204 t.Parallel()
5205 bp := `
5206 cc_library_static {
5207 name: "foo_ubsan_minimal",
5208 srcs: ["foo.cc"],
5209 bazel_module: { label: "//foo_ubsan_minimal" },
5210 sanitize: {
5211 all_undefined: true,
5212 integer_overflow: true,
5213 },
5214 }
5215 cc_library_static {
5216 name: "foo",
5217 srcs: ["foo.cc"],
5218 bazel_module: { label: "//foo" },
5219 sanitize: {
5220 address: true,
5221 hwaddress: true,
5222 fuzzer: true,
5223 integer_overflow: true,
5224 scs: true,
5225 },
5226 }
5227 cc_library_static {
5228 name: "foo_tsan",
5229 srcs: ["foo.cc"],
5230 bazel_module: { label: "//foo_tsan" },
5231 sanitize: {
5232 thread: true,
5233 },
5234 }
5235 cc_library_static {
5236 name: "foo_cfi",
5237 srcs: ["foo.cc"],
5238 bazel_module: { label: "//foo_cfi" },
5239 sanitize: {
5240 cfi: true,
5241 },
5242 }
5243 cc_library_static {
5244 name: "foo_memtag_stack",
5245 srcs: ["foo.cc"],
5246 bazel_module: { label: "//foo_memtag_stack" },
5247 sanitize: {
5248 memtag_stack: true,
5249 },
5250 }
5251 cc_library_static {
5252 name: "foo_memtag_heap",
5253 srcs: ["foo.cc"],
5254 bazel_module: { label: "//foo_memtag_heap" },
5255 sanitize: {
5256 memtag_heap: true,
5257 },
5258 }
5259 cc_library_static {
5260 name: "foo_safestack",
5261 srcs: ["foo.cc"],
5262 bazel_module: { label: "//foo_safestack" },
5263 sanitize: {
5264 safestack: true,
5265 },
5266 }
5267 cc_library_static {
5268 name: "foo_scudo",
5269 srcs: ["foo.cc"],
5270 bazel_module: { label: "//foo_scudo" },
5271 sanitize: {
5272 scudo: true,
5273 },
5274 }
5275 `
5276 testcases := []struct {
5277 name string
5278 variant string
5279 expectedOutputPaths []string
5280 }{
5281 {
5282 name: "foo_ubsan_minimal",
5283 variant: "android_arm64_armv8-a_static_apex28",
5284 expectedOutputPaths: []string{
5285 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5286 },
5287 },
5288 {
5289 name: "foo",
5290 variant: "android_arm64_armv8-a_static_apex28",
5291 expectedOutputPaths: []string{
5292 "outputbase/execroot/__main__/foo.a",
5293 },
5294 },
5295 {
5296 name: "foo",
5297 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5298 expectedOutputPaths: []string{
5299 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5300 },
5301 },
5302 {
5303 name: "foo",
5304 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5305 expectedOutputPaths: []string{
5306 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5307 },
5308 },
5309 {
5310 name: "foo",
5311 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5312 expectedOutputPaths: []string{
5313 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5314 },
5315 },
5316 {
5317 name: "foo",
5318 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5319 expectedOutputPaths: []string{
5320 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5321 },
5322 },
5323 {
5324 name: "foo",
5325 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5326 expectedOutputPaths: []string{
5327 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5328 },
5329 },
5330 {
5331 name: "foo",
5332 variant: "android_arm64_armv8-a_static_scs_apex28",
5333 expectedOutputPaths: []string{
5334 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5335 },
5336 },
5337 {
5338 name: "foo",
5339 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5340 expectedOutputPaths: []string{
5341 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5342 },
5343 },
5344 {
5345 name: "foo",
5346 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5347 expectedOutputPaths: []string{
5348 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5349 },
5350 },
5351 {
5352 name: "foo_tsan",
5353 variant: "android_arm64_armv8-a_static_apex28",
5354 expectedOutputPaths: []string{
5355 "outputbase/execroot/__main__/foo_tsan.a",
5356 },
5357 },
5358 {
5359 name: "foo_tsan",
5360 variant: "android_arm64_armv8-a_static_tsan_apex28",
5361 expectedOutputPaths: []string{
5362 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5363 },
5364 },
5365 {
5366 name: "foo_cfi",
5367 variant: "android_arm64_armv8-a_static_apex28",
5368 expectedOutputPaths: []string{
5369 "outputbase/execroot/__main__/foo_cfi.a",
5370 },
5371 },
5372 {
5373 name: "foo_cfi",
5374 variant: "android_arm64_armv8-a_static_cfi_apex28",
5375 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005376 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005377 },
5378 },
5379 {
5380 name: "foo_memtag_stack",
5381 variant: "android_arm64_armv8-a_static_apex28",
5382 expectedOutputPaths: []string{
5383 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5384 },
5385 },
5386 {
5387 name: "foo_memtag_heap",
5388 variant: "android_arm64_armv8-a_static_apex28",
5389 expectedOutputPaths: []string{
5390 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5391 },
5392 },
5393 {
5394 name: "foo_safestack",
5395 variant: "android_arm64_armv8-a_static_apex28",
5396 expectedOutputPaths: []string{
5397 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5398 },
5399 },
5400 {
5401 name: "foo_scudo",
5402 variant: "android_arm64_armv8-a_static_apex28",
5403 expectedOutputPaths: []string{
5404 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5405 },
5406 },
5407 }
5408
5409 ctx := android.GroupFixturePreparers(
5410 prepareForCcTest,
5411 prepareForAsanTest,
5412 android.FixtureRegisterWithContext(registerTestMutators),
5413 android.FixtureModifyConfig(func(config android.Config) {
5414 config.BazelContext = android.MockBazelContext{
5415 OutputBaseDir: "outputbase",
5416 LabelToCcInfo: map[string]cquery.CcInfo{
5417 "//foo_ubsan_minimal": {
5418 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5419 },
5420 "//foo": {
5421 RootStaticArchives: []string{"foo.a"},
5422 },
5423 "//foo_tsan": {
5424 RootStaticArchives: []string{"foo_tsan.a"},
5425 },
5426 "//foo_cfi": {
5427 RootStaticArchives: []string{"foo_cfi.a"},
5428 },
5429 "//foo_memtag_stack": {
5430 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5431 },
5432 "//foo_memtag_heap": {
5433 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5434 },
5435 "//foo_safestack": {
5436 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5437 },
5438 "//foo_scudo": {
5439 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5440 },
5441 },
5442 }
5443 }),
5444 ).RunTestWithBp(t, bp).TestContext
5445
5446 for _, tc := range testcases {
5447 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5448 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5449 if err != nil {
5450 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5451 }
5452 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5453 }
5454}