blob: e2dba90438543d8cc4a6a8b218046975bccbe9ee [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)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900680 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800681
682 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
683
684 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900685
Jooyung Han2216fb12019-11-06 16:46:15 +0900686 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900687}
688
Chris Parsons79d66a52020-06-05 17:26:16 -0400689func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400690 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400691 bp := `
692 cc_test_library {
693 name: "test_lib",
694 srcs: ["test_lib.cpp"],
695 gtest: false,
696 }
697
698 cc_test {
699 name: "main_test",
700 data_libs: ["test_lib"],
701 gtest: false,
702 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400703 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400704
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000705 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400706 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900707 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400708 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
709
710 ctx := testCcWithConfig(t, config)
711 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
712 testBinary := module.(*Module).linker.(*testBinary)
713 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
714 if err != nil {
715 t.Errorf("Expected cc_test to produce output files, error: %s", err)
716 return
717 }
718 if len(outputFiles) != 1 {
719 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
720 return
721 }
722 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800723 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons79d66a52020-06-05 17:26:16 -0400724 return
725 }
726
727 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400728 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400729
730 if !strings.HasSuffix(outputPath, "/main_test") {
731 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
732 return
733 }
734 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
735 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
736 return
737 }
738}
739
Chris Parsons216e10a2020-07-09 17:12:52 -0400740func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400741 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400742 bp := `
743 cc_test_library {
744 name: "test_lib",
745 srcs: ["test_lib.cpp"],
746 relative_install_path: "foo/bar/baz",
747 gtest: false,
748 }
749
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400750 cc_binary {
751 name: "test_bin",
752 relative_install_path: "foo/bar/baz",
753 compile_multilib: "both",
754 }
755
Chris Parsons216e10a2020-07-09 17:12:52 -0400756 cc_test {
757 name: "main_test",
758 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400759 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400760 gtest: false,
761 }
762 `
763
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000764 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400765 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900766 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400767 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
768
769 ctx := testCcWithConfig(t, config)
770 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
771 testBinary := module.(*Module).linker.(*testBinary)
772 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
773 if err != nil {
774 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
775 }
776 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400777 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400778 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400779 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800780 t.Fatalf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400781 }
782
783 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400784
785 if !strings.HasSuffix(outputPath, "/main_test") {
786 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
787 }
Colin Crossaa255532020-07-03 13:18:24 -0700788 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400789 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
790 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400791 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400792 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400793 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
794 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
795 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
796 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400797}
798
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000799func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400800 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000801 bp := `
802 cc_test {
803 name: "main_test",
804 srcs: ["main_test.cpp"],
805 test_suites: [
806 "suite_1",
807 "suite_2",
808 ],
809 gtest: false,
810 }
811 `
812
813 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
814 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
815
816 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
817 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
818 if len(compatEntries) != 2 {
819 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
820 }
821 if compatEntries[0] != "suite_1" {
822 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
823 " but was '%s'", compatEntries[0])
824 }
825 if compatEntries[1] != "suite_2" {
826 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
827 " but was '%s'", compatEntries[1])
828 }
829}
830
831func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400832 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000833 bp := `
834 cc_test_library {
835 name: "main_test_lib",
836 srcs: ["main_test_lib.cpp"],
837 test_suites: [
838 "suite_1",
839 "suite_2",
840 ],
841 gtest: false,
842 }
843 `
844
845 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
846 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
847
848 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
849 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
850 if len(compatEntries) != 2 {
851 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
852 }
853 if compatEntries[0] != "suite_1" {
854 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
855 " but was '%s'", compatEntries[0])
856 }
857 if compatEntries[1] != "suite_2" {
858 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
859 " but was '%s'", compatEntries[1])
860 }
861}
862
Justin Yun63e9ec72020-10-29 16:49:43 +0900863func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400864 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900865 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900866 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900867 cc_library {
868 name: "libvndk",
869 vndk: {
870 enabled: true,
871 },
872 nocrt: true,
873 }
874 `)
875
Justin Yunc0d8c492021-01-07 17:45:31 +0900876 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900877 cc_library {
878 name: "libvndk",
879 product_available: true,
880 vndk: {
881 enabled: true,
882 },
883 nocrt: true,
884 }
885 `)
886
Justin Yun6977e8a2020-10-29 18:24:11 +0900887 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
888 cc_library {
889 name: "libvndkprop",
890 vendor_available: true,
891 product_available: true,
892 vndk: {
893 enabled: true,
894 },
895 nocrt: true,
896 target: {
897 vendor: {
898 cflags: ["-DTEST",],
899 },
900 },
901 }
902 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900903}
904
Logan Chiend3c59a22018-03-29 14:08:15 +0800905func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400906 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800907 // Check whether an error is emitted when a VNDK lib depends on a system lib.
908 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
909 cc_library {
910 name: "libvndk",
911 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900912 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800913 vndk: {
914 enabled: true,
915 },
916 shared_libs: ["libfwk"], // Cause error
917 nocrt: true,
918 }
919
920 cc_library {
921 name: "libfwk",
922 nocrt: true,
923 }
924 `)
925
926 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
927 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
928 cc_library {
929 name: "libvndk",
930 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900931 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800932 vndk: {
933 enabled: true,
934 },
935 shared_libs: ["libvendor"], // Cause error
936 nocrt: true,
937 }
938
939 cc_library {
940 name: "libvendor",
941 vendor: true,
942 nocrt: true,
943 }
944 `)
945
946 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
947 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
948 cc_library {
949 name: "libvndk_sp",
950 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900951 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800952 vndk: {
953 enabled: true,
954 support_system_process: true,
955 },
956 shared_libs: ["libfwk"], // Cause error
957 nocrt: true,
958 }
959
960 cc_library {
961 name: "libfwk",
962 nocrt: true,
963 }
964 `)
965
966 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
967 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
968 cc_library {
969 name: "libvndk_sp",
970 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900971 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800972 vndk: {
973 enabled: true,
974 support_system_process: true,
975 },
976 shared_libs: ["libvendor"], // Cause error
977 nocrt: true,
978 }
979
980 cc_library {
981 name: "libvendor",
982 vendor: true,
983 nocrt: true,
984 }
985 `)
986
987 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
988 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
989 cc_library {
990 name: "libvndk_sp",
991 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900992 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800993 vndk: {
994 enabled: true,
995 support_system_process: true,
996 },
997 shared_libs: ["libvndk"], // Cause error
998 nocrt: true,
999 }
1000
1001 cc_library {
1002 name: "libvndk",
1003 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001004 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001005 vndk: {
1006 enabled: true,
1007 },
1008 nocrt: true,
1009 }
1010 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001011
1012 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1013 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1014 cc_library {
1015 name: "libvndk",
1016 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001017 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001018 vndk: {
1019 enabled: true,
1020 },
1021 shared_libs: ["libnonvndk"],
1022 nocrt: true,
1023 }
1024
1025 cc_library {
1026 name: "libnonvndk",
1027 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001028 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001029 nocrt: true,
1030 }
1031 `)
1032
1033 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1034 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1035 cc_library {
1036 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001037 vendor_available: true,
1038 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001039 vndk: {
1040 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001041 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001042 },
1043 shared_libs: ["libnonvndk"],
1044 nocrt: true,
1045 }
1046
1047 cc_library {
1048 name: "libnonvndk",
1049 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001050 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001051 nocrt: true,
1052 }
1053 `)
1054
1055 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1056 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1057 cc_library {
1058 name: "libvndksp",
1059 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001060 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001061 vndk: {
1062 enabled: true,
1063 support_system_process: true,
1064 },
1065 shared_libs: ["libnonvndk"],
1066 nocrt: true,
1067 }
1068
1069 cc_library {
1070 name: "libnonvndk",
1071 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001072 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001073 nocrt: true,
1074 }
1075 `)
1076
1077 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1078 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1079 cc_library {
1080 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001081 vendor_available: true,
1082 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001083 vndk: {
1084 enabled: true,
1085 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001086 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001087 },
1088 shared_libs: ["libnonvndk"],
1089 nocrt: true,
1090 }
1091
1092 cc_library {
1093 name: "libnonvndk",
1094 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001095 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001096 nocrt: true,
1097 }
1098 `)
1099}
1100
1101func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001102 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001103 // okay to link : LLNDK -> double_loadable VNDK
1104 testCc(t, `
1105 cc_library {
1106 name: "libllndk",
1107 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001108 llndk: {
1109 symbol_file: "libllndk.map.txt",
1110 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001111 }
1112
1113 cc_library {
1114 name: "libdoubleloadable",
1115 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001116 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001117 vndk: {
1118 enabled: true,
1119 },
1120 double_loadable: true,
1121 }
1122 `)
1123 // okay to link : LLNDK -> VNDK-SP
1124 testCc(t, `
1125 cc_library {
1126 name: "libllndk",
1127 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001128 llndk: {
1129 symbol_file: "libllndk.map.txt",
1130 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001131 }
1132
1133 cc_library {
1134 name: "libvndksp",
1135 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001136 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001137 vndk: {
1138 enabled: true,
1139 support_system_process: true,
1140 },
1141 }
1142 `)
1143 // okay to link : double_loadable -> double_loadable
1144 testCc(t, `
1145 cc_library {
1146 name: "libdoubleloadable1",
1147 shared_libs: ["libdoubleloadable2"],
1148 vendor_available: true,
1149 double_loadable: true,
1150 }
1151
1152 cc_library {
1153 name: "libdoubleloadable2",
1154 vendor_available: true,
1155 double_loadable: true,
1156 }
1157 `)
1158 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1159 testCc(t, `
1160 cc_library {
1161 name: "libdoubleloadable",
1162 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001163 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001164 vndk: {
1165 enabled: true,
1166 },
1167 double_loadable: true,
1168 shared_libs: ["libnondoubleloadable"],
1169 }
1170
1171 cc_library {
1172 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001173 vendor_available: true,
1174 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001175 vndk: {
1176 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001177 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001178 },
1179 double_loadable: true,
1180 }
1181 `)
1182 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1183 testCc(t, `
1184 cc_library {
1185 name: "libllndk",
1186 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001187 llndk: {
1188 symbol_file: "libllndk.map.txt",
1189 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001190 }
1191
1192 cc_library {
1193 name: "libcoreonly",
1194 shared_libs: ["libvendoravailable"],
1195 }
1196
1197 // indirect dependency of LLNDK
1198 cc_library {
1199 name: "libvendoravailable",
1200 vendor_available: true,
1201 double_loadable: true,
1202 }
1203 `)
1204}
1205
1206func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001207 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001208 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1209 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1210 cc_library {
1211 name: "libllndk",
1212 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001213 llndk: {
1214 symbol_file: "libllndk.map.txt",
1215 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001216 }
1217
1218 cc_library {
1219 name: "libnondoubleloadable",
1220 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001221 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001222 vndk: {
1223 enabled: true,
1224 },
1225 }
1226 `)
1227
1228 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1229 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1230 cc_library {
1231 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001232 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001233 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001234 llndk: {
1235 symbol_file: "libllndk.map.txt",
1236 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001237 }
1238
1239 cc_library {
1240 name: "libnondoubleloadable",
1241 vendor_available: true,
1242 }
1243 `)
1244
Jooyung Hana70f0672019-01-18 15:20:43 +09001245 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1246 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1247 cc_library {
1248 name: "libllndk",
1249 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001250 llndk: {
1251 symbol_file: "libllndk.map.txt",
1252 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001253 }
1254
1255 cc_library {
1256 name: "libcoreonly",
1257 shared_libs: ["libvendoravailable"],
1258 }
1259
1260 // indirect dependency of LLNDK
1261 cc_library {
1262 name: "libvendoravailable",
1263 vendor_available: true,
1264 }
1265 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001266
1267 // The error is not from 'client' but from 'libllndk'
1268 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1269 cc_library {
1270 name: "client",
1271 vendor_available: true,
1272 double_loadable: true,
1273 shared_libs: ["libllndk"],
1274 }
1275 cc_library {
1276 name: "libllndk",
1277 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001278 llndk: {
1279 symbol_file: "libllndk.map.txt",
1280 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001281 }
1282 cc_library {
1283 name: "libnondoubleloadable",
1284 vendor_available: true,
1285 }
1286 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001287}
1288
Jooyung Han479ca172020-10-19 18:51:07 +09001289func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001290 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001291 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1292 cc_library {
1293 name: "libvndksp",
1294 shared_libs: ["libanothervndksp"],
1295 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001296 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001297 vndk: {
1298 enabled: true,
1299 support_system_process: true,
1300 }
1301 }
1302
1303 cc_library {
1304 name: "libllndk",
1305 shared_libs: ["libanothervndksp"],
1306 }
1307
Jooyung Han479ca172020-10-19 18:51:07 +09001308 cc_library {
1309 name: "libanothervndksp",
1310 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001311 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001312 }
1313 `)
1314}
1315
Logan Chienf3511742017-10-31 18:04:35 +08001316func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001317 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001318 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001319 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001320 cc_library {
1321 name: "libvndk",
1322 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001323 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001324 vndk: {
1325 enabled: true,
1326 },
1327 nocrt: true,
1328 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001329 cc_library {
1330 name: "libvndk2",
1331 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001332 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001333 vndk: {
1334 enabled: true,
1335 },
1336 target: {
1337 vendor: {
1338 suffix: "-suffix",
1339 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001340 product: {
1341 suffix: "-suffix",
1342 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001343 },
1344 nocrt: true,
1345 }
Logan Chienf3511742017-10-31 18:04:35 +08001346
1347 cc_library {
1348 name: "libvndk_ext",
1349 vendor: true,
1350 vndk: {
1351 enabled: true,
1352 extends: "libvndk",
1353 },
1354 nocrt: true,
1355 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001356
1357 cc_library {
1358 name: "libvndk2_ext",
1359 vendor: true,
1360 vndk: {
1361 enabled: true,
1362 extends: "libvndk2",
1363 },
1364 nocrt: true,
1365 }
Logan Chienf3511742017-10-31 18:04:35 +08001366
Justin Yun0ecf0b22020-02-28 15:07:59 +09001367 cc_library {
1368 name: "libvndk_ext_product",
1369 product_specific: true,
1370 vndk: {
1371 enabled: true,
1372 extends: "libvndk",
1373 },
1374 nocrt: true,
1375 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001376
Justin Yun0ecf0b22020-02-28 15:07:59 +09001377 cc_library {
1378 name: "libvndk2_ext_product",
1379 product_specific: true,
1380 vndk: {
1381 enabled: true,
1382 extends: "libvndk2",
1383 },
1384 nocrt: true,
1385 }
1386 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001387 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001388 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001389 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001390
1391 ctx := testCcWithConfig(t, config)
1392
1393 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1394 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1395
1396 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1397 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1398
1399 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1400 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001401}
1402
Logan Chienf3511742017-10-31 18:04:35 +08001403func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001404 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001405 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001406 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001407 cc_library {
1408 name: "libvndk",
1409 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001410 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001411 vndk: {
1412 enabled: true,
1413 },
1414 nocrt: true,
1415 }
1416
1417 cc_library {
1418 name: "libvndk_ext",
1419 vndk: {
1420 enabled: true,
1421 extends: "libvndk",
1422 },
1423 nocrt: true,
1424 }
1425 `)
1426
1427 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1428 cc_library {
1429 name: "libvndk",
1430 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001431 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001432 vndk: {
1433 enabled: true,
1434 },
1435 nocrt: true,
1436 }
1437
1438 cc_library {
1439 name: "libvndk_ext",
1440 vendor: true,
1441 vndk: {
1442 enabled: true,
1443 },
1444 nocrt: true,
1445 }
1446 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001447
1448 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1449 cc_library {
1450 name: "libvndk",
1451 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001452 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001453 vndk: {
1454 enabled: true,
1455 },
1456 nocrt: true,
1457 }
1458
1459 cc_library {
1460 name: "libvndk_ext_product",
1461 product_specific: true,
1462 vndk: {
1463 enabled: true,
1464 },
1465 nocrt: true,
1466 }
1467 `)
1468
1469 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1470 cc_library {
1471 name: "libvndk",
1472 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001473 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001474 vndk: {
1475 enabled: true,
1476 },
1477 nocrt: true,
1478 }
1479
1480 cc_library {
1481 name: "libvndk_ext_product",
1482 product_specific: true,
1483 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001484 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001485 vndk: {
1486 enabled: true,
1487 extends: "libvndk",
1488 },
1489 nocrt: true,
1490 }
1491 `)
Logan Chienf3511742017-10-31 18:04:35 +08001492}
1493
1494func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001495 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001496 // This test ensures an error is emitted for inconsistent support_system_process.
1497 testCcError(t, "module \".*\" with mismatched support_system_process", `
1498 cc_library {
1499 name: "libvndk",
1500 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001501 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001502 vndk: {
1503 enabled: true,
1504 },
1505 nocrt: true,
1506 }
1507
1508 cc_library {
1509 name: "libvndk_sp_ext",
1510 vendor: true,
1511 vndk: {
1512 enabled: true,
1513 extends: "libvndk",
1514 support_system_process: true,
1515 },
1516 nocrt: true,
1517 }
1518 `)
1519
1520 testCcError(t, "module \".*\" with mismatched support_system_process", `
1521 cc_library {
1522 name: "libvndk_sp",
1523 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001524 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001525 vndk: {
1526 enabled: true,
1527 support_system_process: true,
1528 },
1529 nocrt: true,
1530 }
1531
1532 cc_library {
1533 name: "libvndk_ext",
1534 vendor: true,
1535 vndk: {
1536 enabled: true,
1537 extends: "libvndk_sp",
1538 },
1539 nocrt: true,
1540 }
1541 `)
1542}
1543
1544func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001545 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001546 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001547 // with `private: true`.
1548 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001549 cc_library {
1550 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001551 vendor_available: true,
1552 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001553 vndk: {
1554 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001555 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001556 },
1557 nocrt: true,
1558 }
1559
1560 cc_library {
1561 name: "libvndk_ext",
1562 vendor: true,
1563 vndk: {
1564 enabled: true,
1565 extends: "libvndk",
1566 },
1567 nocrt: true,
1568 }
1569 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001570
Justin Yunfd9e8042020-12-23 18:23:14 +09001571 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001572 cc_library {
1573 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001574 vendor_available: true,
1575 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001576 vndk: {
1577 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001578 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001579 },
1580 nocrt: true,
1581 }
1582
1583 cc_library {
1584 name: "libvndk_ext_product",
1585 product_specific: true,
1586 vndk: {
1587 enabled: true,
1588 extends: "libvndk",
1589 },
1590 nocrt: true,
1591 }
1592 `)
Logan Chienf3511742017-10-31 18:04:35 +08001593}
1594
Logan Chiend3c59a22018-03-29 14:08:15 +08001595func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001596 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001597 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001598 testCc(t, `
1599 cc_library {
1600 name: "libvndk",
1601 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001602 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001603 vndk: {
1604 enabled: true,
1605 },
1606 nocrt: true,
1607 }
1608
1609 cc_library {
1610 name: "libvndk_ext",
1611 vendor: true,
1612 vndk: {
1613 enabled: true,
1614 extends: "libvndk",
1615 },
1616 nocrt: true,
1617 }
1618
1619 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001620 name: "libvndk_sp",
1621 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001622 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001623 vndk: {
1624 enabled: true,
1625 support_system_process: true,
1626 },
1627 nocrt: true,
1628 }
1629
1630 cc_library {
1631 name: "libvndk_sp_ext",
1632 vendor: true,
1633 vndk: {
1634 enabled: true,
1635 extends: "libvndk_sp",
1636 support_system_process: true,
1637 },
1638 nocrt: true,
1639 }
1640
1641 cc_library {
1642 name: "libvendor",
1643 vendor: true,
1644 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1645 nocrt: true,
1646 }
1647 `)
1648}
1649
Logan Chiend3c59a22018-03-29 14:08:15 +08001650func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001651 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001652 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001653 testCc(t, `
1654 cc_library {
1655 name: "libvndk",
1656 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001657 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001658 vndk: {
1659 enabled: true,
1660 },
1661 nocrt: true,
1662 }
1663
1664 cc_library {
1665 name: "libvndk_ext",
1666 vendor: true,
1667 vndk: {
1668 enabled: true,
1669 extends: "libvndk",
1670 },
1671 shared_libs: ["libvendor"],
1672 nocrt: true,
1673 }
1674
1675 cc_library {
1676 name: "libvendor",
1677 vendor: true,
1678 nocrt: true,
1679 }
1680 `)
Logan Chienf3511742017-10-31 18:04:35 +08001681
Logan Chiend3c59a22018-03-29 14:08:15 +08001682 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1683 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001684 cc_library {
1685 name: "libvndk_sp",
1686 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001687 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001688 vndk: {
1689 enabled: true,
1690 support_system_process: true,
1691 },
1692 nocrt: true,
1693 }
1694
1695 cc_library {
1696 name: "libvndk_sp_ext",
1697 vendor: true,
1698 vndk: {
1699 enabled: true,
1700 extends: "libvndk_sp",
1701 support_system_process: true,
1702 },
1703 shared_libs: ["libvendor"], // Cause an error
1704 nocrt: true,
1705 }
1706
1707 cc_library {
1708 name: "libvendor",
1709 vendor: true,
1710 nocrt: true,
1711 }
1712 `)
1713}
1714
Justin Yun0ecf0b22020-02-28 15:07:59 +09001715func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001716 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001717 bp := `
1718 cc_library {
1719 name: "libvndk",
1720 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001721 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001722 vndk: {
1723 enabled: true,
1724 },
1725 nocrt: true,
1726 }
1727
1728 cc_library {
1729 name: "libvndk_ext_product",
1730 product_specific: true,
1731 vndk: {
1732 enabled: true,
1733 extends: "libvndk",
1734 },
1735 shared_libs: ["libproduct_for_vndklibs"],
1736 nocrt: true,
1737 }
1738
1739 cc_library {
1740 name: "libvndk_sp",
1741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001742 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001743 vndk: {
1744 enabled: true,
1745 support_system_process: true,
1746 },
1747 nocrt: true,
1748 }
1749
1750 cc_library {
1751 name: "libvndk_sp_ext_product",
1752 product_specific: true,
1753 vndk: {
1754 enabled: true,
1755 extends: "libvndk_sp",
1756 support_system_process: true,
1757 },
1758 shared_libs: ["libproduct_for_vndklibs"],
1759 nocrt: true,
1760 }
1761
1762 cc_library {
1763 name: "libproduct",
1764 product_specific: true,
1765 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "libproduct_for_vndklibs",
1771 product_specific: true,
1772 nocrt: true,
1773 }
1774 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001775 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001776 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001777 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001778
1779 testCcWithConfig(t, config)
1780}
1781
Logan Chiend3c59a22018-03-29 14:08:15 +08001782func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001783 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001784 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1785 // library.
1786 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1787 cc_library {
1788 name: "libvndk",
1789 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001790 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001791 vndk: {
1792 enabled: true,
1793 },
1794 nocrt: true,
1795 }
1796
1797 cc_library {
1798 name: "libvndk_sp",
1799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001800 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001801 vndk: {
1802 enabled: true,
1803 support_system_process: true,
1804 },
1805 nocrt: true,
1806 }
1807
1808 cc_library {
1809 name: "libvndk_sp_ext",
1810 vendor: true,
1811 vndk: {
1812 enabled: true,
1813 extends: "libvndk_sp",
1814 support_system_process: true,
1815 },
1816 shared_libs: ["libvndk"], // Cause an error
1817 nocrt: true,
1818 }
1819 `)
1820
1821 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1822 // library.
1823 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1824 cc_library {
1825 name: "libvndk",
1826 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001827 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001828 vndk: {
1829 enabled: true,
1830 },
1831 nocrt: true,
1832 }
1833
1834 cc_library {
1835 name: "libvndk_ext",
1836 vendor: true,
1837 vndk: {
1838 enabled: true,
1839 extends: "libvndk",
1840 },
1841 nocrt: true,
1842 }
1843
1844 cc_library {
1845 name: "libvndk_sp",
1846 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001847 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001848 vndk: {
1849 enabled: true,
1850 support_system_process: true,
1851 },
1852 nocrt: true,
1853 }
1854
1855 cc_library {
1856 name: "libvndk_sp_ext",
1857 vendor: true,
1858 vndk: {
1859 enabled: true,
1860 extends: "libvndk_sp",
1861 support_system_process: true,
1862 },
1863 shared_libs: ["libvndk_ext"], // Cause an error
1864 nocrt: true,
1865 }
1866 `)
1867}
1868
1869func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001870 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001871 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1872 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001873 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1874 cc_library {
1875 name: "libvndk",
1876 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001877 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001878 vndk: {
1879 enabled: true,
1880 },
1881 nocrt: true,
1882 }
1883
1884 cc_library {
1885 name: "libvndk_ext",
1886 vendor: true,
1887 vndk: {
1888 enabled: true,
1889 extends: "libvndk",
1890 },
1891 nocrt: true,
1892 }
1893
1894 cc_library {
1895 name: "libvndk2",
1896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001897 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001898 vndk: {
1899 enabled: true,
1900 },
1901 shared_libs: ["libvndk_ext"],
1902 nocrt: true,
1903 }
1904 `)
1905
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001906 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001907 cc_library {
1908 name: "libvndk",
1909 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001910 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001911 vndk: {
1912 enabled: true,
1913 },
1914 nocrt: true,
1915 }
1916
1917 cc_library {
1918 name: "libvndk_ext",
1919 vendor: true,
1920 vndk: {
1921 enabled: true,
1922 extends: "libvndk",
1923 },
1924 nocrt: true,
1925 }
1926
1927 cc_library {
1928 name: "libvndk2",
1929 vendor_available: true,
1930 vndk: {
1931 enabled: true,
1932 },
1933 target: {
1934 vendor: {
1935 shared_libs: ["libvndk_ext"],
1936 },
1937 },
1938 nocrt: true,
1939 }
1940 `)
1941
1942 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1943 cc_library {
1944 name: "libvndk_sp",
1945 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001946 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001947 vndk: {
1948 enabled: true,
1949 support_system_process: true,
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_sp_ext",
1956 vendor: true,
1957 vndk: {
1958 enabled: true,
1959 extends: "libvndk_sp",
1960 support_system_process: true,
1961 },
1962 nocrt: true,
1963 }
1964
1965 cc_library {
1966 name: "libvndk_sp_2",
1967 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001968 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001969 vndk: {
1970 enabled: true,
1971 support_system_process: true,
1972 },
1973 shared_libs: ["libvndk_sp_ext"],
1974 nocrt: true,
1975 }
1976 `)
1977
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001978 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001979 cc_library {
1980 name: "libvndk_sp",
1981 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001982 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001983 vndk: {
1984 enabled: true,
1985 },
1986 nocrt: true,
1987 }
1988
1989 cc_library {
1990 name: "libvndk_sp_ext",
1991 vendor: true,
1992 vndk: {
1993 enabled: true,
1994 extends: "libvndk_sp",
1995 },
1996 nocrt: true,
1997 }
1998
1999 cc_library {
2000 name: "libvndk_sp2",
2001 vendor_available: true,
2002 vndk: {
2003 enabled: true,
2004 },
2005 target: {
2006 vendor: {
2007 shared_libs: ["libvndk_sp_ext"],
2008 },
2009 },
2010 nocrt: true,
2011 }
2012 `)
2013}
2014
Justin Yun5f7f7e82019-11-18 19:52:14 +09002015func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002016 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002017 bp := `
2018 cc_library {
2019 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002020 llndk: {
2021 symbol_file: "libllndk.map.txt",
2022 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002023 }
2024 cc_library {
2025 name: "libvndk",
2026 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002027 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002028 vndk: {
2029 enabled: true,
2030 },
2031 nocrt: true,
2032 }
2033 cc_library {
2034 name: "libvndk_sp",
2035 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002036 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002037 vndk: {
2038 enabled: true,
2039 support_system_process: true,
2040 },
2041 nocrt: true,
2042 }
2043 cc_library {
2044 name: "libva",
2045 vendor_available: true,
2046 nocrt: true,
2047 }
2048 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002049 name: "libpa",
2050 product_available: true,
2051 nocrt: true,
2052 }
2053 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002054 name: "libboth_available",
2055 vendor_available: true,
2056 product_available: true,
2057 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002058 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002059 target: {
2060 vendor: {
2061 suffix: "-vendor",
2062 },
2063 product: {
2064 suffix: "-product",
2065 },
2066 }
2067 }
2068 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002069 name: "libproduct_va",
2070 product_specific: true,
2071 vendor_available: true,
2072 nocrt: true,
2073 }
2074 cc_library {
2075 name: "libprod",
2076 product_specific: true,
2077 shared_libs: [
2078 "libllndk",
2079 "libvndk",
2080 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002081 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002082 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002083 "libproduct_va",
2084 ],
2085 nocrt: true,
2086 }
2087 cc_library {
2088 name: "libvendor",
2089 vendor: true,
2090 shared_libs: [
2091 "libllndk",
2092 "libvndk",
2093 "libvndk_sp",
2094 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002095 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002096 "libproduct_va",
2097 ],
2098 nocrt: true,
2099 }
2100 `
2101
Paul Duffin8567f222021-03-23 00:02:06 +00002102 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002103
Jooyung Han261e1582020-10-20 18:54:21 +09002104 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2105 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002106
2107 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2108 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2109
2110 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2111 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002112
2113 ensureStringContains := func(t *testing.T, str string, substr string) {
2114 t.Helper()
2115 if !strings.Contains(str, substr) {
2116 t.Errorf("%q is not found in %v", substr, str)
2117 }
2118 }
2119 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2120 t.Helper()
2121 if strings.Contains(str, substr) {
2122 t.Errorf("%q is found in %v", substr, str)
2123 }
2124 }
2125
2126 // _static variant is used since _shared reuses *.o from the static variant
2127 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2128 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2129
2130 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2131 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2132 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2133 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2134
2135 product_cflags := product_static.Rule("cc").Args["cFlags"]
2136 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2137 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2138 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002139}
2140
2141func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002142 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002143 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002144 cc_library {
2145 name: "libprod",
2146 product_specific: true,
2147 shared_libs: [
2148 "libvendor",
2149 ],
2150 nocrt: true,
2151 }
2152 cc_library {
2153 name: "libvendor",
2154 vendor: true,
2155 nocrt: true,
2156 }
2157 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002158 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002159 cc_library {
2160 name: "libprod",
2161 product_specific: true,
2162 shared_libs: [
2163 "libsystem",
2164 ],
2165 nocrt: true,
2166 }
2167 cc_library {
2168 name: "libsystem",
2169 nocrt: true,
2170 }
2171 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002172 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002173 cc_library {
2174 name: "libprod",
2175 product_specific: true,
2176 shared_libs: [
2177 "libva",
2178 ],
2179 nocrt: true,
2180 }
2181 cc_library {
2182 name: "libva",
2183 vendor_available: true,
2184 nocrt: true,
2185 }
2186 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002187 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002188 cc_library {
2189 name: "libprod",
2190 product_specific: true,
2191 shared_libs: [
2192 "libvndk_private",
2193 ],
2194 nocrt: true,
2195 }
2196 cc_library {
2197 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002198 vendor_available: true,
2199 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002200 vndk: {
2201 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002202 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002203 },
2204 nocrt: true,
2205 }
2206 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002207 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002208 cc_library {
2209 name: "libprod",
2210 product_specific: true,
2211 shared_libs: [
2212 "libsystem_ext",
2213 ],
2214 nocrt: true,
2215 }
2216 cc_library {
2217 name: "libsystem_ext",
2218 system_ext_specific: true,
2219 nocrt: true,
2220 }
2221 `)
2222 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2223 cc_library {
2224 name: "libsystem",
2225 shared_libs: [
2226 "libproduct_va",
2227 ],
2228 nocrt: true,
2229 }
2230 cc_library {
2231 name: "libproduct_va",
2232 product_specific: true,
2233 vendor_available: true,
2234 nocrt: true,
2235 }
2236 `)
2237}
2238
Jooyung Han38002912019-05-16 04:01:54 +09002239func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002240 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002241 bp := `
2242 cc_library {
2243 name: "libvndk",
2244 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002245 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002246 vndk: {
2247 enabled: true,
2248 },
2249 }
2250 cc_library {
2251 name: "libvndksp",
2252 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002253 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002254 vndk: {
2255 enabled: true,
2256 support_system_process: true,
2257 },
2258 }
2259 cc_library {
2260 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002261 vendor_available: true,
2262 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002263 vndk: {
2264 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002265 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002266 },
2267 }
2268 cc_library {
2269 name: "libvendor",
2270 vendor: true,
2271 }
2272 cc_library {
2273 name: "libvndkext",
2274 vendor: true,
2275 vndk: {
2276 enabled: true,
2277 extends: "libvndk",
2278 },
2279 }
2280 vndk_prebuilt_shared {
2281 name: "prevndk",
2282 version: "27",
2283 target_arch: "arm",
2284 binder32bit: true,
2285 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002286 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002287 vndk: {
2288 enabled: true,
2289 },
2290 arch: {
2291 arm: {
2292 srcs: ["liba.so"],
2293 },
2294 },
2295 }
2296 cc_library {
2297 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002298 llndk: {
2299 symbol_file: "libllndk.map.txt",
2300 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002301 }
2302 cc_library {
2303 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002304 llndk: {
2305 symbol_file: "libllndkprivate.map.txt",
2306 private: true,
2307 }
Colin Cross78212242021-01-06 14:51:30 -08002308 }
2309
2310 llndk_libraries_txt {
2311 name: "llndk.libraries.txt",
2312 }
2313 vndkcore_libraries_txt {
2314 name: "vndkcore.libraries.txt",
2315 }
2316 vndksp_libraries_txt {
2317 name: "vndksp.libraries.txt",
2318 }
2319 vndkprivate_libraries_txt {
2320 name: "vndkprivate.libraries.txt",
2321 }
2322 vndkcorevariant_libraries_txt {
2323 name: "vndkcorevariant.libraries.txt",
2324 insert_vndk_version: false,
2325 }
2326 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002327
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002328 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002329 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002330 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002331 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002332 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002333
Colin Cross78212242021-01-06 14:51:30 -08002334 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2335 []string{"libvndk.so", "libvndkprivate.so"})
2336 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2337 []string{"libc++.so", "libvndksp.so"})
2338 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2339 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2340 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2341 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002342
Colin Crossfb0c16e2019-11-20 17:12:35 -08002343 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002344
Jooyung Han38002912019-05-16 04:01:54 +09002345 tests := []struct {
2346 variant string
2347 name string
2348 expected string
2349 }{
2350 {vendorVariant, "libvndk", "native:vndk"},
2351 {vendorVariant, "libvndksp", "native:vndk"},
2352 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2353 {vendorVariant, "libvendor", "native:vendor"},
2354 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002355 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002356 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002357 {coreVariant, "libvndk", "native:platform"},
2358 {coreVariant, "libvndkprivate", "native:platform"},
2359 {coreVariant, "libllndk", "native:platform"},
2360 }
2361 for _, test := range tests {
2362 t.Run(test.name, func(t *testing.T) {
2363 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2364 assertString(t, module.makeLinkType, test.expected)
2365 })
2366 }
2367}
2368
Jeff Gaston294356f2017-09-27 17:05:30 -07002369var staticLinkDepOrderTestCases = []struct {
2370 // This is a string representation of a map[moduleName][]moduleDependency .
2371 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002372 inStatic string
2373
2374 // This is a string representation of a map[moduleName][]moduleDependency .
2375 // It models the dependencies declared in an Android.bp file.
2376 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002377
2378 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2379 // The keys of allOrdered specify which modules we would like to check.
2380 // The values of allOrdered specify the expected result (of the transitive closure of all
2381 // dependencies) for each module to test
2382 allOrdered string
2383
2384 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2385 // The keys of outOrdered specify which modules we would like to check.
2386 // The values of outOrdered specify the expected result (of the ordered linker command line)
2387 // for each module to test.
2388 outOrdered string
2389}{
2390 // Simple tests
2391 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002392 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002393 outOrdered: "",
2394 },
2395 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002396 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002397 outOrdered: "a:",
2398 },
2399 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002400 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002401 outOrdered: "a:b; b:",
2402 },
2403 // Tests of reordering
2404 {
2405 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002406 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002407 outOrdered: "a:b,c,d; b:d; c:d; d:",
2408 },
2409 {
2410 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002411 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002412 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2413 },
2414 {
2415 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002416 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002417 outOrdered: "a:d,b,e,c; d:b; e:c",
2418 },
2419 {
2420 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002421 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002422 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2423 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2424 },
2425 {
2426 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002427 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 -07002428 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2429 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2430 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002431 // shared dependencies
2432 {
2433 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2434 // So, we don't actually have to check that a shared dependency of c will change the order
2435 // of a library that depends statically on b and on c. We only need to check that if c has
2436 // a shared dependency on b, that that shows up in allOrdered.
2437 inShared: "c:b",
2438 allOrdered: "c:b",
2439 outOrdered: "c:",
2440 },
2441 {
2442 // This test doesn't actually include any shared dependencies but it's a reminder of what
2443 // the second phase of the above test would look like
2444 inStatic: "a:b,c; c:b",
2445 allOrdered: "a:c,b; c:b",
2446 outOrdered: "a:c,b; c:b",
2447 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002448 // tiebreakers for when two modules specifying different orderings and there is no dependency
2449 // to dictate an order
2450 {
2451 // 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 -08002452 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002453 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2454 },
2455 {
2456 // 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 -08002457 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 -07002458 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2459 },
2460 // Tests involving duplicate dependencies
2461 {
2462 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002463 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002464 outOrdered: "a:c,b",
2465 },
2466 {
2467 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002468 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002469 outOrdered: "a:d,c,b",
2470 },
2471 // Tests to confirm the nonexistence of infinite loops.
2472 // These cases should never happen, so as long as the test terminates and the
2473 // result is deterministic then that should be fine.
2474 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002475 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002476 outOrdered: "a:a",
2477 },
2478 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002479 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002480 allOrdered: "a:b,c; b:c,a; c:a,b",
2481 outOrdered: "a:b; b:c; c:a",
2482 },
2483 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002484 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002485 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2486 outOrdered: "a:c,b; b:a,c; c:b,a",
2487 },
2488}
2489
2490// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2491func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2492 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2493 strippedText := strings.Replace(text, " ", "", -1)
2494 if len(strippedText) < 1 {
2495 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2496 }
2497 allDeps = make(map[android.Path][]android.Path, 0)
2498
2499 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2500 moduleTexts := strings.Split(strippedText, ";")
2501
2502 outputForModuleName := func(moduleName string) android.Path {
2503 return android.PathForTesting(moduleName)
2504 }
2505
2506 for _, moduleText := range moduleTexts {
2507 // convert from "a:b,c" to ["a", "b,c"]
2508 components := strings.Split(moduleText, ":")
2509 if len(components) != 2 {
2510 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2511 }
2512 moduleName := components[0]
2513 moduleOutput := outputForModuleName(moduleName)
2514 modulesInOrder = append(modulesInOrder, moduleOutput)
2515
2516 depString := components[1]
2517 // convert from "b,c" to ["b", "c"]
2518 depNames := strings.Split(depString, ",")
2519 if len(depString) < 1 {
2520 depNames = []string{}
2521 }
2522 var deps []android.Path
2523 for _, depName := range depNames {
2524 deps = append(deps, outputForModuleName(depName))
2525 }
2526 allDeps[moduleOutput] = deps
2527 }
2528 return modulesInOrder, allDeps
2529}
2530
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002532 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002533 ctx := testCc(t, `
2534 cc_library {
2535 name: "a",
2536 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002537 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002538 }
2539 cc_library {
2540 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002541 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002542 }
2543 cc_library {
2544 name: "c",
2545 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002546 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002547 }
2548 cc_library {
2549 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002550 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002551 }
2552
2553 `)
2554
Colin Cross7113d202019-11-20 16:39:12 -08002555 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002557 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2558 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002559 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002560
2561 if !reflect.DeepEqual(actual, expected) {
2562 t.Errorf("staticDeps orderings were not propagated correctly"+
2563 "\nactual: %v"+
2564 "\nexpected: %v",
2565 actual,
2566 expected,
2567 )
2568 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002569}
Jeff Gaston294356f2017-09-27 17:05:30 -07002570
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002571func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002572 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002573 ctx := testCc(t, `
2574 cc_library {
2575 name: "a",
2576 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002577 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002578 }
2579 cc_library {
2580 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002581 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002582 }
2583 cc_library {
2584 name: "c",
2585 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002586 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002587 }
2588
2589 `)
2590
Colin Cross7113d202019-11-20 16:39:12 -08002591 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002592 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002593 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2594 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002595 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596
2597 if !reflect.DeepEqual(actual, expected) {
2598 t.Errorf("staticDeps orderings did not account for shared libs"+
2599 "\nactual: %v"+
2600 "\nexpected: %v",
2601 actual,
2602 expected,
2603 )
2604 }
2605}
2606
Jooyung Hanb04a4992020-03-13 18:57:35 +09002607func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002608 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002609 if !reflect.DeepEqual(actual, expected) {
2610 t.Errorf(message+
2611 "\nactual: %v"+
2612 "\nexpected: %v",
2613 actual,
2614 expected,
2615 )
2616 }
2617}
2618
Jooyung Han61b66e92020-03-21 14:21:46 +00002619func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002620 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002621 result := prepareForCcTest.RunTestWithBp(t, `
2622 cc_library {
2623 name: "libllndk",
2624 stubs: { versions: ["1", "2"] },
2625 llndk: {
2626 symbol_file: "libllndk.map.txt",
2627 },
2628 export_include_dirs: ["include"],
2629 }
2630
2631 cc_prebuilt_library_shared {
2632 name: "libllndkprebuilt",
2633 stubs: { versions: ["1", "2"] },
2634 llndk: {
2635 symbol_file: "libllndkprebuilt.map.txt",
2636 },
2637 }
2638
2639 cc_library {
2640 name: "libllndk_with_external_headers",
2641 stubs: { versions: ["1", "2"] },
2642 llndk: {
2643 symbol_file: "libllndk.map.txt",
2644 export_llndk_headers: ["libexternal_llndk_headers"],
2645 },
2646 header_libs: ["libexternal_headers"],
2647 export_header_lib_headers: ["libexternal_headers"],
2648 }
2649 cc_library_headers {
2650 name: "libexternal_headers",
2651 export_include_dirs: ["include"],
2652 vendor_available: true,
2653 }
2654 cc_library_headers {
2655 name: "libexternal_llndk_headers",
2656 export_include_dirs: ["include_llndk"],
2657 llndk: {
2658 symbol_file: "libllndk.map.txt",
2659 },
2660 vendor_available: true,
2661 }
2662
2663 cc_library {
2664 name: "libllndk_with_override_headers",
2665 stubs: { versions: ["1", "2"] },
2666 llndk: {
2667 symbol_file: "libllndk.map.txt",
2668 override_export_include_dirs: ["include_llndk"],
2669 },
2670 export_include_dirs: ["include"],
2671 }
2672 `)
2673 actual := result.ModuleVariantsForTests("libllndk")
2674 for i := 0; i < len(actual); i++ {
2675 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2676 actual = append(actual[:i], actual[i+1:]...)
2677 i--
2678 }
2679 }
2680 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002681 "android_vendor.29_arm64_armv8-a_shared_current",
2682 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002683 "android_vendor.29_arm_armv7-a-neon_shared_current",
2684 "android_vendor.29_arm_armv7-a-neon_shared",
2685 }
2686 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2687
2688 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2689 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2690
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002691 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2692 t.Helper()
2693 m := result.ModuleForTests(module, variant).Module()
2694 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2695 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2696 expectedDirs, f.IncludeDirs)
2697 }
2698
2699 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2700 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2701 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2702 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2703 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2704 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2705}
2706
Jiyong Parka46a4d52017-12-14 19:54:34 +09002707func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002708 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002709 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002710 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002711 name: "libllndk_headers",
2712 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002713 llndk: {
2714 llndk_headers: true,
2715 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002716 }
2717 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002718 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002719 llndk: {
2720 symbol_file: "libllndk.map.txt",
2721 export_llndk_headers: ["libllndk_headers"],
2722 }
Colin Cross0477b422020-10-13 18:43:54 -07002723 }
2724
2725 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002726 name: "libvendor",
2727 shared_libs: ["libllndk"],
2728 vendor: true,
2729 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002730 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002731 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002732 }
2733 `)
2734
2735 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002736 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002737 cflags := cc.Args["cFlags"]
2738 if !strings.Contains(cflags, "-Imy_include") {
2739 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2740 }
2741}
2742
Logan Chien43d34c32017-12-20 01:17:32 +08002743func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2744 actual := module.Properties.AndroidMkRuntimeLibs
2745 if !reflect.DeepEqual(actual, expected) {
2746 t.Errorf("incorrect runtime_libs for shared libs"+
2747 "\nactual: %v"+
2748 "\nexpected: %v",
2749 actual,
2750 expected,
2751 )
2752 }
2753}
2754
2755const runtimeLibAndroidBp = `
2756 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002757 name: "liball_available",
2758 vendor_available: true,
2759 product_available: true,
2760 no_libcrt : true,
2761 nocrt : true,
2762 system_shared_libs : [],
2763 }
2764 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002765 name: "libvendor_available1",
2766 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002767 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002768 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002769 nocrt : true,
2770 system_shared_libs : [],
2771 }
2772 cc_library {
2773 name: "libvendor_available2",
2774 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002775 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002776 target: {
2777 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002778 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002779 }
2780 },
Yi Konge7fe9912019-06-02 00:53:50 -07002781 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002782 nocrt : true,
2783 system_shared_libs : [],
2784 }
2785 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002786 name: "libproduct_vendor",
2787 product_specific: true,
2788 vendor_available: true,
2789 no_libcrt : true,
2790 nocrt : true,
2791 system_shared_libs : [],
2792 }
2793 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002794 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002795 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002796 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002797 nocrt : true,
2798 system_shared_libs : [],
2799 }
2800 cc_library {
2801 name: "libvendor1",
2802 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002803 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002804 nocrt : true,
2805 system_shared_libs : [],
2806 }
2807 cc_library {
2808 name: "libvendor2",
2809 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002810 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002811 no_libcrt : true,
2812 nocrt : true,
2813 system_shared_libs : [],
2814 }
2815 cc_library {
2816 name: "libproduct_available1",
2817 product_available: true,
2818 runtime_libs: ["liball_available"],
2819 no_libcrt : true,
2820 nocrt : true,
2821 system_shared_libs : [],
2822 }
2823 cc_library {
2824 name: "libproduct1",
2825 product_specific: true,
2826 no_libcrt : true,
2827 nocrt : true,
2828 system_shared_libs : [],
2829 }
2830 cc_library {
2831 name: "libproduct2",
2832 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002833 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002834 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002835 nocrt : true,
2836 system_shared_libs : [],
2837 }
2838`
2839
2840func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002841 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002842 ctx := testCc(t, runtimeLibAndroidBp)
2843
2844 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002845 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002846
Justin Yun8a2600c2020-12-07 12:44:03 +09002847 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2848 checkRuntimeLibs(t, []string{"liball_available"}, module)
2849
2850 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2851 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002852
2853 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002854 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002855
2856 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2857 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002858 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002859
Justin Yun8a2600c2020-12-07 12:44:03 +09002860 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2861 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002862
2863 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002864 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002865
2866 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2867 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002868 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002869
2870 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2871 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2872
2873 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002874 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002875}
2876
2877func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002878 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002879 ctx := testCc(t, runtimeLibAndroidBp)
2880
Colin Cross7113d202019-11-20 16:39:12 -08002881 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002882 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2883 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002884
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002885 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002886 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002887 checkRuntimeLibs(t, nil, module)
2888}
2889
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002890func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002891 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002892 actual := module.Properties.AndroidMkStaticLibs
2893 if !reflect.DeepEqual(actual, expected) {
2894 t.Errorf("incorrect static_libs"+
2895 "\nactual: %v"+
2896 "\nexpected: %v",
2897 actual,
2898 expected,
2899 )
2900 }
2901}
2902
2903const staticLibAndroidBp = `
2904 cc_library {
2905 name: "lib1",
2906 }
2907 cc_library {
2908 name: "lib2",
2909 static_libs: ["lib1"],
2910 }
2911`
2912
2913func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002914 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002915 ctx := testCc(t, staticLibAndroidBp)
2916
2917 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002918 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002919 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002920 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002921
2922 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002923 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002924 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2925 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002926 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002927}
2928
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002929func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
2930 bp := `
2931 cc_library {
2932 name: "static_dep",
2933 }
2934 cc_library {
2935 name: "whole_static_dep",
2936 }
2937 cc_library {
2938 name: "shared_dep",
2939 }
2940 cc_library {
2941 name: "lib",
2942 bazel_module: { label: "//:lib" },
2943 static_libs: ["static_dep"],
2944 whole_static_libs: ["whole_static_dep"],
2945 shared_libs: ["shared_dep"],
2946 }
2947 cc_test {
2948 name: "test",
2949 bazel_module: { label: "//:test" },
2950 static_libs: ["static_dep"],
2951 whole_static_libs: ["whole_static_dep"],
2952 shared_libs: ["shared_dep"],
2953 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04002954 sanitize: {
2955 // cc_test modules default to memtag_heap: true,
2956 // but this adds extra dependencies that we don't care about
2957 never: true,
2958 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002959 }
2960 cc_binary {
2961 name: "binary",
2962 bazel_module: { label: "//:binary" },
2963 static_libs: ["static_dep"],
2964 whole_static_libs: ["whole_static_dep"],
2965 shared_libs: ["shared_dep"],
2966 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05002967 cc_library_headers {
2968 name: "lib_headers",
2969 bazel_module: { label: "//:lib_headers" },
2970 static_libs: ["static_dep"],
2971 whole_static_libs: ["whole_static_dep"],
2972 shared_libs: ["shared_dep"],
2973 }
2974 cc_prebuilt_library {
2975 name: "lib_prebuilt",
2976 bazel_module: { label: "//:lib_prebuilt" },
2977 static_libs: ["static_dep"],
2978 whole_static_libs: ["whole_static_dep"],
2979 shared_libs: ["shared_dep"],
2980 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002981 `
2982
2983 testCases := []struct {
2984 name string
2985 moduleName string
2986 variant string
2987 androidMkInfo cquery.CcAndroidMkInfo
2988 }{
2989 {
2990 name: "shared lib",
2991 moduleName: "lib",
2992 variant: "android_arm64_armv8-a_shared",
2993 androidMkInfo: cquery.CcAndroidMkInfo{
2994 LocalStaticLibs: []string{"static_dep"},
2995 LocalWholeStaticLibs: []string{"whole_static_dep"},
2996 LocalSharedLibs: []string{"shared_dep"},
2997 },
2998 },
2999 {
3000 name: "static lib",
3001 moduleName: "lib",
3002 variant: "android_arm64_armv8-a_static",
3003 androidMkInfo: cquery.CcAndroidMkInfo{
3004 LocalStaticLibs: []string{"static_dep"},
3005 LocalWholeStaticLibs: []string{"whole_static_dep"},
3006 LocalSharedLibs: []string{"shared_dep"},
3007 },
3008 },
3009 {
3010 name: "cc_test arm64",
3011 moduleName: "test",
3012 variant: "android_arm64_armv8-a",
3013 androidMkInfo: cquery.CcAndroidMkInfo{
3014 LocalStaticLibs: []string{"static_dep"},
3015 LocalWholeStaticLibs: []string{"whole_static_dep"},
3016 LocalSharedLibs: []string{"shared_dep"},
3017 },
3018 },
3019 {
3020 name: "cc_test arm",
3021 moduleName: "test",
3022 variant: "android_arm_armv7-a-neon",
3023 androidMkInfo: cquery.CcAndroidMkInfo{
3024 LocalStaticLibs: []string{"static_dep"},
3025 LocalWholeStaticLibs: []string{"whole_static_dep"},
3026 LocalSharedLibs: []string{"shared_dep"},
3027 },
3028 },
3029 {
3030 name: "cc_binary",
3031 moduleName: "binary",
3032 variant: "android_arm64_armv8-a",
3033 androidMkInfo: cquery.CcAndroidMkInfo{
3034 LocalStaticLibs: []string{"static_dep"},
3035 LocalWholeStaticLibs: []string{"whole_static_dep"},
3036 LocalSharedLibs: []string{"shared_dep"},
3037 },
3038 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003039 {
3040 name: "cc_library_headers",
3041 moduleName: "lib_headers",
3042 variant: "android_arm64_armv8-a",
3043 androidMkInfo: cquery.CcAndroidMkInfo{
3044 LocalStaticLibs: []string{"static_dep"},
3045 LocalWholeStaticLibs: []string{"whole_static_dep"},
3046 LocalSharedLibs: []string{"shared_dep"},
3047 },
3048 },
3049 {
3050 name: "prebuilt lib static",
3051 moduleName: "lib_prebuilt",
3052 variant: "android_arm64_armv8-a_static",
3053 androidMkInfo: cquery.CcAndroidMkInfo{
3054 LocalStaticLibs: []string{"static_dep"},
3055 LocalWholeStaticLibs: []string{"whole_static_dep"},
3056 LocalSharedLibs: []string{"shared_dep"},
3057 },
3058 },
3059 {
3060 name: "prebuilt lib shared",
3061 moduleName: "lib_prebuilt",
3062 variant: "android_arm64_armv8-a_shared",
3063 androidMkInfo: cquery.CcAndroidMkInfo{
3064 LocalStaticLibs: []string{"static_dep"},
3065 LocalWholeStaticLibs: []string{"whole_static_dep"},
3066 LocalSharedLibs: []string{"shared_dep"},
3067 },
3068 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003069 }
3070
3071 outputBaseDir := "out/bazel"
3072 for _, tc := range testCases {
3073 t.Run(tc.name, func(t *testing.T) {
3074 result := android.GroupFixturePreparers(
3075 prepareForCcTest,
3076 android.FixtureModifyConfig(func(config android.Config) {
3077 config.BazelContext = android.MockBazelContext{
3078 OutputBaseDir: outputBaseDir,
3079 LabelToCcInfo: map[string]cquery.CcInfo{
3080 "//:lib": cquery.CcInfo{
3081 CcAndroidMkInfo: tc.androidMkInfo,
3082 RootDynamicLibraries: []string{""},
3083 },
3084 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3085 CcAndroidMkInfo: tc.androidMkInfo,
3086 RootStaticArchives: []string{""},
3087 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003088 "//:lib_headers": cquery.CcInfo{
3089 CcAndroidMkInfo: tc.androidMkInfo,
3090 OutputFiles: []string{""},
3091 },
3092 "//:lib_prebuilt": cquery.CcInfo{
3093 CcAndroidMkInfo: tc.androidMkInfo,
3094 },
3095 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3096 CcAndroidMkInfo: tc.androidMkInfo,
3097 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003098 },
3099 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003100 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003101 CcAndroidMkInfo: tc.androidMkInfo,
3102 },
3103 "//:binary": cquery.CcUnstrippedInfo{
3104 CcAndroidMkInfo: tc.androidMkInfo,
3105 },
3106 },
3107 }
3108 }),
3109 ).RunTestWithBp(t, bp)
3110 ctx := result.TestContext
3111
3112 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3113 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003114 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3115 t.Errorf("incorrect static_libs"+
3116 "\nactual: %v"+
3117 "\nexpected: %v",
3118 module.Properties.AndroidMkStaticLibs,
3119 tc.androidMkInfo.LocalStaticLibs,
3120 )
3121 }
3122 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3123 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3124 tc.androidMkInfo.LocalStaticLibs,
3125 )
3126 if staticDepsDiffer {
3127 t.Errorf(
3128 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3129 tc.androidMkInfo.LocalStaticLibs,
3130 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3131 missingStaticDeps,
3132 additionalStaticDeps,
3133 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003134 }
3135
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003136 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3137 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3138 tc.androidMkInfo.LocalWholeStaticLibs,
3139 module.Properties.AndroidMkWholeStaticLibs,
3140 )
3141 }
3142 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3143 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3144 tc.androidMkInfo.LocalWholeStaticLibs,
3145 )
3146 if wholeStaticDepsDiffer {
3147 t.Errorf(
3148 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3149 tc.androidMkInfo.LocalWholeStaticLibs,
3150 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3151 missingWholeStaticDeps,
3152 additionalWholeStaticDeps,
3153 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003154 }
3155
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003156 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3157 t.Errorf("incorrect shared_libs"+
3158 "\nactual: %v"+
3159 "\nexpected: %v",
3160 module.Properties.AndroidMkSharedLibs,
3161 tc.androidMkInfo.LocalSharedLibs,
3162 )
3163 }
3164 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3165 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3166 tc.androidMkInfo.LocalSharedLibs,
3167 )
3168 if sharedDepsDiffer {
3169 t.Errorf(
3170 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3171 tc.androidMkInfo.LocalSharedLibs,
3172 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3173 missingSharedDeps,
3174 additionalSharedDeps,
3175 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003176 }
3177 })
3178 }
3179}
3180
Jiyong Parkd08b6972017-09-26 10:50:54 +09003181var compilerFlagsTestCases = []struct {
3182 in string
3183 out bool
3184}{
3185 {
3186 in: "a",
3187 out: false,
3188 },
3189 {
3190 in: "-a",
3191 out: true,
3192 },
3193 {
3194 in: "-Ipath/to/something",
3195 out: false,
3196 },
3197 {
3198 in: "-isystempath/to/something",
3199 out: false,
3200 },
3201 {
3202 in: "--coverage",
3203 out: false,
3204 },
3205 {
3206 in: "-include a/b",
3207 out: true,
3208 },
3209 {
3210 in: "-include a/b c/d",
3211 out: false,
3212 },
3213 {
3214 in: "-DMACRO",
3215 out: true,
3216 },
3217 {
3218 in: "-DMAC RO",
3219 out: false,
3220 },
3221 {
3222 in: "-a -b",
3223 out: false,
3224 },
3225 {
3226 in: "-DMACRO=definition",
3227 out: true,
3228 },
3229 {
3230 in: "-DMACRO=defi nition",
3231 out: true, // TODO(jiyong): this should be false
3232 },
3233 {
3234 in: "-DMACRO(x)=x + 1",
3235 out: true,
3236 },
3237 {
3238 in: "-DMACRO=\"defi nition\"",
3239 out: true,
3240 },
3241}
3242
3243type mockContext struct {
3244 BaseModuleContext
3245 result bool
3246}
3247
3248func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3249 // CheckBadCompilerFlags calls this function when the flag should be rejected
3250 ctx.result = false
3251}
3252
3253func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003254 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003255 for _, testCase := range compilerFlagsTestCases {
3256 ctx := &mockContext{result: true}
3257 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3258 if ctx.result != testCase.out {
3259 t.Errorf("incorrect output:")
3260 t.Errorf(" input: %#v", testCase.in)
3261 t.Errorf(" expected: %#v", testCase.out)
3262 t.Errorf(" got: %#v", ctx.result)
3263 }
3264 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003265}
Jiyong Park374510b2018-03-19 18:23:01 +09003266
Jiyong Park37b25202018-07-11 10:49:27 +09003267func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003268 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003269 ctx := testCc(t, `
3270 cc_library_shared {
3271 name: "librecovery",
3272 recovery: true,
3273 }
3274 cc_library_shared {
3275 name: "librecovery32",
3276 recovery: true,
3277 compile_multilib:"32",
3278 }
Jiyong Park5baac542018-08-28 09:55:37 +09003279 cc_library_shared {
3280 name: "libHalInRecovery",
3281 recovery_available: true,
3282 vendor: true,
3283 }
Jiyong Park37b25202018-07-11 10:49:27 +09003284 `)
3285
3286 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003287 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003288 if len(variants) != 1 || !android.InList(arm64, variants) {
3289 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3290 }
3291
3292 variants = ctx.ModuleVariantsForTests("librecovery32")
3293 if android.InList(arm64, variants) {
3294 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3295 }
Jiyong Park5baac542018-08-28 09:55:37 +09003296
3297 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3298 if !recoveryModule.Platform() {
3299 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3300 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003301}
Jiyong Park5baac542018-08-28 09:55:37 +09003302
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003303func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003304 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003305 bp := `
3306 cc_prebuilt_test_library_shared {
3307 name: "test_lib",
3308 relative_install_path: "foo/bar/baz",
3309 srcs: ["srcpath/dontusethispath/baz.so"],
3310 }
3311
3312 cc_test {
3313 name: "main_test",
3314 data_libs: ["test_lib"],
3315 gtest: false,
3316 }
3317 `
3318
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003319 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003320 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003321 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003322 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3323
3324 ctx := testCcWithConfig(t, config)
3325 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3326 testBinary := module.(*Module).linker.(*testBinary)
3327 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3328 if err != nil {
3329 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3330 }
3331 if len(outputFiles) != 1 {
3332 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3333 }
3334 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003335 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003336 }
3337
3338 outputPath := outputFiles[0].String()
3339
3340 if !strings.HasSuffix(outputPath, "/main_test") {
3341 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3342 }
Colin Crossaa255532020-07-03 13:18:24 -07003343 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003344 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3345 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3346 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3347 }
3348}
3349
Jiyong Park7ed9de32018-10-15 22:25:07 +09003350func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003351 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003352 ctx := testCc(t, `
3353 cc_library_shared {
3354 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003355 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003356 stubs: {
3357 symbol_file: "foo.map.txt",
3358 versions: ["1", "2", "3"],
3359 },
3360 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003361
Jiyong Park7ed9de32018-10-15 22:25:07 +09003362 cc_library_shared {
3363 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003364 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003365 shared_libs: ["libFoo#1"],
3366 }`)
3367
3368 variants := ctx.ModuleVariantsForTests("libFoo")
3369 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003370 "android_arm64_armv8-a_shared",
3371 "android_arm64_armv8-a_shared_1",
3372 "android_arm64_armv8-a_shared_2",
3373 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003374 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003375 "android_arm_armv7-a-neon_shared",
3376 "android_arm_armv7-a-neon_shared_1",
3377 "android_arm_armv7-a-neon_shared_2",
3378 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003379 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003380 }
3381 variantsMismatch := false
3382 if len(variants) != len(expectedVariants) {
3383 variantsMismatch = true
3384 } else {
3385 for _, v := range expectedVariants {
3386 if !inList(v, variants) {
3387 variantsMismatch = false
3388 }
3389 }
3390 }
3391 if variantsMismatch {
3392 t.Errorf("variants of libFoo expected:\n")
3393 for _, v := range expectedVariants {
3394 t.Errorf("%q\n", v)
3395 }
3396 t.Errorf(", but got:\n")
3397 for _, v := range variants {
3398 t.Errorf("%q\n", v)
3399 }
3400 }
3401
Colin Cross7113d202019-11-20 16:39:12 -08003402 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003403 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003404 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003405 if !strings.Contains(libFlags, libFoo1StubPath) {
3406 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3407 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003408
Colin Cross7113d202019-11-20 16:39:12 -08003409 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003410 cFlags := libBarCompileRule.Args["cFlags"]
3411 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3412 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3413 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3414 }
Jiyong Park37b25202018-07-11 10:49:27 +09003415}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003416
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003417func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3418 t.Parallel()
3419 ctx := testCc(t, `
3420 cc_library_shared {
3421 name: "libFoo",
3422 srcs: ["foo.c"],
3423 stubs: {
3424 symbol_file: "foo.map.txt",
3425 versions: ["current"],
3426 },
3427 apex_available: ["bar", "a1"],
3428 }
3429
3430 cc_library_shared {
3431 name: "libBar",
3432 srcs: ["bar.c"],
3433 shared_libs: ["libFoo"],
3434 apex_available: ["a1"],
3435 }
3436
3437 cc_library_shared {
3438 name: "libA1",
3439 srcs: ["a1.c"],
3440 shared_libs: ["libFoo"],
3441 apex_available: ["a1"],
3442 }
3443
3444 cc_library_shared {
3445 name: "libBarA1",
3446 srcs: ["bara1.c"],
3447 shared_libs: ["libFoo"],
3448 apex_available: ["bar", "a1"],
3449 }
3450
3451 cc_library_shared {
3452 name: "libAnyApex",
3453 srcs: ["anyApex.c"],
3454 shared_libs: ["libFoo"],
3455 apex_available: ["//apex_available:anyapex"],
3456 }
3457
3458 cc_library_shared {
3459 name: "libBaz",
3460 srcs: ["baz.c"],
3461 shared_libs: ["libFoo"],
3462 apex_available: ["baz"],
3463 }
3464
3465 cc_library_shared {
3466 name: "libQux",
3467 srcs: ["qux.c"],
3468 shared_libs: ["libFoo"],
3469 apex_available: ["qux", "bar"],
3470 }`)
3471
3472 variants := ctx.ModuleVariantsForTests("libFoo")
3473 expectedVariants := []string{
3474 "android_arm64_armv8-a_shared",
3475 "android_arm64_armv8-a_shared_current",
3476 "android_arm_armv7-a-neon_shared",
3477 "android_arm_armv7-a-neon_shared_current",
3478 }
3479 variantsMismatch := false
3480 if len(variants) != len(expectedVariants) {
3481 variantsMismatch = true
3482 } else {
3483 for _, v := range expectedVariants {
3484 if !inList(v, variants) {
3485 variantsMismatch = false
3486 }
3487 }
3488 }
3489 if variantsMismatch {
3490 t.Errorf("variants of libFoo expected:\n")
3491 for _, v := range expectedVariants {
3492 t.Errorf("%q\n", v)
3493 }
3494 t.Errorf(", but got:\n")
3495 for _, v := range variants {
3496 t.Errorf("%q\n", v)
3497 }
3498 }
3499
3500 linkAgainstFoo := []string{"libBarA1"}
3501 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3502
3503 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3504 for _, lib := range linkAgainstFoo {
3505 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3506 libFlags := libLinkRule.Args["libFlags"]
3507 if !strings.Contains(libFlags, libFooPath) {
3508 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3509 }
3510 }
3511
3512 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3513 for _, lib := range linkAgainstFooStubs {
3514 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3515 libFlags := libLinkRule.Args["libFlags"]
3516 if !strings.Contains(libFlags, libFooStubPath) {
3517 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3518 }
3519 }
3520}
3521
Sam Delmerico75dbca22023-04-20 13:13:25 +00003522func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003523 t.Parallel()
3524 bp := `
3525 cc_library_shared {
3526 name: "libFoo",
3527 bazel_module: { label: "//:libFoo" },
3528 srcs: ["foo.c"],
3529 stubs: {
3530 symbol_file: "foo.map.txt",
3531 versions: ["current"],
3532 },
3533 apex_available: ["bar", "a1"],
3534 }
3535
3536 cc_library_shared {
3537 name: "libBar",
3538 srcs: ["bar.c"],
3539 shared_libs: ["libFoo"],
3540 apex_available: ["a1"],
3541 }
3542
3543 cc_library_shared {
3544 name: "libA1",
3545 srcs: ["a1.c"],
3546 shared_libs: ["libFoo"],
3547 apex_available: ["a1"],
3548 }
3549
3550 cc_library_shared {
3551 name: "libBarA1",
3552 srcs: ["bara1.c"],
3553 shared_libs: ["libFoo"],
3554 apex_available: ["bar", "a1"],
3555 }
3556
3557 cc_library_shared {
3558 name: "libAnyApex",
3559 srcs: ["anyApex.c"],
3560 shared_libs: ["libFoo"],
3561 apex_available: ["//apex_available:anyapex"],
3562 }
3563
3564 cc_library_shared {
3565 name: "libBaz",
3566 srcs: ["baz.c"],
3567 shared_libs: ["libFoo"],
3568 apex_available: ["baz"],
3569 }
3570
3571 cc_library_shared {
3572 name: "libQux",
3573 srcs: ["qux.c"],
3574 shared_libs: ["libFoo"],
3575 apex_available: ["qux", "bar"],
3576 }`
3577
3578 result := android.GroupFixturePreparers(
3579 prepareForCcTest,
3580 android.FixtureModifyConfig(func(config android.Config) {
3581 config.BazelContext = android.MockBazelContext{
3582 OutputBaseDir: "out/bazel",
3583 LabelToCcInfo: map[string]cquery.CcInfo{
3584 "//:libFoo": {
3585 RootDynamicLibraries: []string{"libFoo.so"},
3586 },
3587 "//:libFoo_stub_libs-current": {
3588 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3589 },
3590 },
3591 }
3592 }),
3593 ).RunTestWithBp(t, bp)
3594 ctx := result.TestContext
3595
3596 variants := ctx.ModuleVariantsForTests("libFoo")
3597 expectedVariants := []string{
3598 "android_arm64_armv8-a_shared",
3599 "android_arm64_armv8-a_shared_current",
3600 "android_arm_armv7-a-neon_shared",
3601 "android_arm_armv7-a-neon_shared_current",
3602 }
3603 variantsMismatch := false
3604 if len(variants) != len(expectedVariants) {
3605 variantsMismatch = true
3606 } else {
3607 for _, v := range expectedVariants {
3608 if !inList(v, variants) {
3609 variantsMismatch = false
3610 }
3611 }
3612 }
3613 if variantsMismatch {
3614 t.Errorf("variants of libFoo expected:\n")
3615 for _, v := range expectedVariants {
3616 t.Errorf("%q\n", v)
3617 }
3618 t.Errorf(", but got:\n")
3619 for _, v := range variants {
3620 t.Errorf("%q\n", v)
3621 }
3622 }
3623
3624 linkAgainstFoo := []string{"libBarA1"}
3625 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3626
3627 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3628 for _, lib := range linkAgainstFoo {
3629 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3630 libFlags := libLinkRule.Args["libFlags"]
3631 if !strings.Contains(libFlags, libFooPath) {
3632 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3633 }
3634 }
3635
3636 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3637 for _, lib := range linkAgainstFooStubs {
3638 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3639 libFlags := libLinkRule.Args["libFlags"]
3640 if !strings.Contains(libFlags, libFooStubPath) {
3641 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3642 }
3643 }
3644}
3645
Jooyung Hanb04a4992020-03-13 18:57:35 +09003646func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003647 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003648 for _, tc := range []struct{ moduleName, expected string }{
3649 {"libc", "__LIBC_API__"},
3650 {"libfoo", "__LIBFOO_API__"},
3651 {"libfoo@1", "__LIBFOO_1_API__"},
3652 {"libfoo-v1", "__LIBFOO_V1_API__"},
3653 {"libfoo.v1", "__LIBFOO_V1_API__"},
3654 } {
3655 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3656 }
3657}
3658
Liz Kammer83cf81b2022-09-22 08:24:20 -04003659func pathsToBase(paths android.Paths) []string {
3660 var ret []string
3661 for _, p := range paths {
3662 ret = append(ret, p.Base())
3663 }
3664 return ret
3665}
3666
3667func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003668 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003669 ctx := testCc(t, `
3670 cc_library_static {
3671 name: "foo",
3672 srcs: ["foo.c"],
3673 }
3674
3675 cc_library_static {
3676 name: "bar",
3677 srcs: ["bar.c"],
3678 }
3679
3680 cc_library_shared {
3681 name: "qux",
3682 srcs: ["qux.c"],
3683 }
3684
3685 cc_library_static {
3686 name: "baz",
3687 srcs: ["baz.c"],
3688 static_libs: ["foo"],
3689 shared_libs: ["qux"],
3690 whole_static_libs: ["bar"],
3691 }`)
3692
3693 variant := "android_arm64_armv8-a_static"
3694 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3695
3696 // For static libraries, the object files of a whole static dep are included in the archive
3697 // directly
3698 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3699 t.Errorf("Expected input objects %q, got %q", w, g)
3700 }
3701
3702 // non whole static dependencies are not linked into the archive
3703 if len(arRule.Implicits) > 0 {
3704 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3705 }
3706}
3707
3708func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003709 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003710 ctx := testCc(t, `
3711 cc_library_static {
3712 name: "foo",
3713 srcs: ["foo.c"],
3714 }
3715
3716 cc_library_static {
3717 name: "bar",
3718 srcs: ["bar.c"],
3719 }
3720
3721 cc_library_shared {
3722 name: "qux",
3723 srcs: ["qux.c"],
3724 }
3725
3726 cc_library_shared {
3727 name: "baz",
3728 srcs: ["baz.c"],
3729 static_libs: ["foo"],
3730 shared_libs: ["qux"],
3731 whole_static_libs: ["bar"],
3732 }`)
3733
3734 variant := "android_arm64_armv8-a_shared"
3735 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3736 libFlags := linkRule.Args["libFlags"]
3737 // When dynamically linking, we expect static dependencies to be found on the command line
3738 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3739 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3740 }
3741 // When dynamically linking, we expect whole static dependencies to be found on the command line
3742 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3743 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3744 }
3745
3746 // When dynamically linking, we expect shared dependencies to be found on the command line
3747 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3748 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3749 }
3750
3751 // We should only have the objects from the shared library srcs, not the whole static dependencies
3752 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3753 t.Errorf("Expected input objects %q, got %q", w, g)
3754 }
3755}
3756
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003757func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003758 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003759 ctx := testCc(t, `
3760 cc_binary {
3761 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003762 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003763 static_executable: true,
3764 }`)
3765
Colin Cross7113d202019-11-20 16:39:12 -08003766 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003767 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3768 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003769 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003770 for _, lib := range systemStaticLibs {
3771 if !strings.Contains(libFlags, lib) {
3772 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3773 }
3774 }
3775 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3776 for _, lib := range systemSharedLibs {
3777 if strings.Contains(libFlags, lib) {
3778 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3779 }
3780 }
3781}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003782
3783func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003784 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003785 ctx := testCc(t, `
3786 cc_binary {
3787 name: "mybin",
3788 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003789 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003790 static_executable: true,
3791 stl: "none",
3792 }
3793
3794 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003795 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003796 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003797 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003798 stl: "none",
3799 }
3800
3801 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003802 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003803 srcs: ["foo.c"],
3804 stl: "none",
3805 stubs: {
3806 versions: ["1"],
3807 },
3808 }`)
3809
Colin Cross0de8a1e2020-09-18 14:15:30 -07003810 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3811 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003812 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003813
3814 if !reflect.DeepEqual(actual, expected) {
3815 t.Errorf("staticDeps orderings were not propagated correctly"+
3816 "\nactual: %v"+
3817 "\nexpected: %v",
3818 actual,
3819 expected,
3820 )
3821 }
3822}
Jooyung Han38002912019-05-16 04:01:54 +09003823
Jooyung Hand48f3c32019-08-23 11:18:57 +09003824func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003825 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003826 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3827 cc_library {
3828 name: "libA",
3829 srcs: ["foo.c"],
3830 shared_libs: ["libB"],
3831 stl: "none",
3832 }
3833
3834 cc_library {
3835 name: "libB",
3836 srcs: ["foo.c"],
3837 enabled: false,
3838 stl: "none",
3839 }
3840 `)
3841}
3842
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003843func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3844 bp := `
3845 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003846 name: "test_afl_fuzz_target",
3847 srcs: ["foo.c"],
3848 host_supported: true,
3849 static_libs: [
3850 "afl_fuzz_static_lib",
3851 ],
3852 shared_libs: [
3853 "afl_fuzz_shared_lib",
3854 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003855 fuzzing_frameworks: {
3856 afl: true,
3857 libfuzzer: false,
3858 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003859 }
3860 cc_library {
3861 name: "afl_fuzz_static_lib",
3862 host_supported: true,
3863 srcs: ["static_file.c"],
3864 }
3865 cc_library {
3866 name: "libfuzzer_only_static_lib",
3867 host_supported: true,
3868 srcs: ["static_file.c"],
3869 }
3870 cc_library {
3871 name: "afl_fuzz_shared_lib",
3872 host_supported: true,
3873 srcs: ["shared_file.c"],
3874 static_libs: [
3875 "second_static_lib",
3876 ],
3877 }
3878 cc_library_headers {
3879 name: "libafl_headers",
3880 vendor_available: true,
3881 host_supported: true,
3882 export_include_dirs: [
3883 "include",
3884 "instrumentation",
3885 ],
3886 }
3887 cc_object {
3888 name: "afl-compiler-rt",
3889 vendor_available: true,
3890 host_supported: true,
3891 cflags: [
3892 "-fPIC",
3893 ],
3894 srcs: [
3895 "instrumentation/afl-compiler-rt.o.c",
3896 ],
3897 }
3898 cc_library {
3899 name: "second_static_lib",
3900 host_supported: true,
3901 srcs: ["second_file.c"],
3902 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003903 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003904 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003905 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003906 srcs: [
3907 "aflpp_driver.c",
3908 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003909 }`
3910
3911 testEnv := map[string]string{
3912 "FUZZ_FRAMEWORK": "AFL",
3913 }
3914
3915 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003916
3917 checkPcGuardFlag := func(
3918 modName string, variantName string, shouldHave bool) {
3919 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3920
3921 cFlags, ok := cc.Args["cFlags"]
3922 if !ok {
3923 t.Errorf("Could not find cFlags for module %s and variant %s",
3924 modName, variantName)
3925 }
3926
3927 if strings.Contains(
3928 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3929 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3930 "Test failed for module %s and variant %s",
3931 !shouldHave, shouldHave, modName, variantName)
3932 }
3933 }
3934
Cory Barkera1da26f2022-06-07 20:12:06 +00003935 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003936 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003937
3938 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003939 checkPcGuardFlag(moduleName, variant+"_static", false)
3940 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003941
3942 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003943 checkPcGuardFlag(moduleName, variant+"_static", false)
3944 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003945
3946 ctx.ModuleForTests("afl_fuzz_shared_lib",
3947 "android_arm64_armv8-a_shared").Rule("cc")
3948 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003949 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3950}
3951
3952func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003953 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003954 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3955}
3956
3957func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003958 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003959 if runtime.GOOS != "linux" {
3960 t.Skip("requires linux")
3961 }
3962
3963 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003964}
3965
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003966// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3967// correctly.
3968func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003969 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003970 ctx := testCc(t, `
3971 cc_fuzz {
3972 name: "fuzz_smoke_test",
3973 srcs: ["foo.c"],
3974 }`)
3975
Paul Duffin075c4172019-12-19 19:06:13 +00003976 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003977 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3978}
3979
Jooyung Han38002912019-05-16 04:01:54 +09003980func assertString(t *testing.T, got, expected string) {
3981 t.Helper()
3982 if got != expected {
3983 t.Errorf("expected %q got %q", expected, got)
3984 }
3985}
3986
3987func assertArrayString(t *testing.T, got, expected []string) {
3988 t.Helper()
3989 if len(got) != len(expected) {
3990 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3991 return
3992 }
3993 for i := range got {
3994 if got[i] != expected[i] {
3995 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3996 i, expected[i], expected, got[i], got)
3997 return
3998 }
3999 }
4000}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004001
Jooyung Han0302a842019-10-30 18:43:49 +09004002func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4003 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004004 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004005}
4006
Colin Crosse1bb5d02019-09-24 14:55:04 -07004007func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004008 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004009 ctx := testCc(t, `
4010 cc_defaults {
4011 name: "defaults",
4012 srcs: ["foo.c"],
4013 static: {
4014 srcs: ["bar.c"],
4015 },
4016 shared: {
4017 srcs: ["baz.c"],
4018 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004019 bazel_module: {
4020 bp2build_available: true,
4021 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004022 }
4023
4024 cc_library_static {
4025 name: "libstatic",
4026 defaults: ["defaults"],
4027 }
4028
4029 cc_library_shared {
4030 name: "libshared",
4031 defaults: ["defaults"],
4032 }
4033
4034 cc_library {
4035 name: "libboth",
4036 defaults: ["defaults"],
4037 }
4038
4039 cc_binary {
4040 name: "binary",
4041 defaults: ["defaults"],
4042 }`)
4043
Colin Cross7113d202019-11-20 16:39:12 -08004044 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004045 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4046 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4047 }
Colin Cross7113d202019-11-20 16:39:12 -08004048 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004049 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4050 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4051 }
Colin Cross7113d202019-11-20 16:39:12 -08004052 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004053 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4054 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4055 }
4056
Colin Cross7113d202019-11-20 16:39:12 -08004057 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004058 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4059 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4060 }
Colin Cross7113d202019-11-20 16:39:12 -08004061 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004062 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4063 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4064 }
4065}
Colin Crosseabaedd2020-02-06 17:01:55 -08004066
4067func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004068 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004069 bp := `
4070 cc_defaults {
4071 name: "libfoo_defaults",
4072 srcs: ["foo.c"],
4073 cppflags: ["-DFOO"],
4074 product_variables: {
4075 debuggable: {
4076 cppflags: ["-DBAR"],
4077 },
4078 },
4079 }
4080
4081 cc_library {
4082 name: "libfoo",
4083 defaults: ["libfoo_defaults"],
4084 }
4085 `
4086
Paul Duffin8567f222021-03-23 00:02:06 +00004087 result := android.GroupFixturePreparers(
4088 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004089 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004090
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004091 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4092 variables.Debuggable = BoolPtr(true)
4093 }),
4094 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004095
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004096 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004097 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004098}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004099
4100func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4101 t.Parallel()
4102 bp := `
4103 cc_library_static {
4104 name: "libfoo",
4105 srcs: ["foo.c"],
4106 whole_static_libs: ["libbar"],
4107 }
4108
4109 cc_library_static {
4110 name: "libbar",
4111 whole_static_libs: ["libmissing"],
4112 }
4113 `
4114
Paul Duffin8567f222021-03-23 00:02:06 +00004115 result := android.GroupFixturePreparers(
4116 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004117 android.PrepareForTestWithAllowMissingDependencies,
4118 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004119
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004120 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004121 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004122
Paul Duffine84b1332021-03-12 11:59:43 +00004123 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004124
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004125 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004126 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004127}
Colin Crosse9fe2942020-11-10 18:12:15 -08004128
4129func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004130 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004131 bp := `
4132 cc_binary {
4133 name: "bin",
4134 host_supported: true,
4135 shared_libs: ["libshared"],
4136 runtime_libs: ["libruntime"],
4137 srcs: [":gen"],
4138 }
4139
4140 cc_library_shared {
4141 name: "libshared",
4142 host_supported: true,
4143 shared_libs: ["libtransitive"],
4144 }
4145
4146 cc_library_shared {
4147 name: "libtransitive",
4148 host_supported: true,
4149 }
4150
4151 cc_library_shared {
4152 name: "libruntime",
4153 host_supported: true,
4154 }
4155
4156 cc_binary_host {
4157 name: "tool",
4158 srcs: ["foo.cpp"],
4159 }
4160
4161 genrule {
4162 name: "gen",
4163 tools: ["tool"],
4164 out: ["gen.cpp"],
4165 cmd: "$(location tool) $(out)",
4166 }
4167 `
4168
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004169 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004170 ctx := testCcWithConfig(t, config)
4171
4172 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4173 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4174 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4175 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4176 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4177
4178 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4179 t.Errorf("expected host bin dependency %q, got %q", w, g)
4180 }
4181
4182 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4183 t.Errorf("expected host bin dependency %q, got %q", w, g)
4184 }
4185
4186 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4187 t.Errorf("expected host bin dependency %q, got %q", w, g)
4188 }
4189
4190 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4191 t.Errorf("expected host bin dependency %q, got %q", w, g)
4192 }
4193
4194 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4195 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4196 }
4197
4198 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4199 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4200 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4201 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4202
4203 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4204 t.Errorf("expected device bin dependency %q, got %q", w, g)
4205 }
4206
4207 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4208 t.Errorf("expected device bin dependency %q, got %q", w, g)
4209 }
4210
4211 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4212 t.Errorf("expected device bin dependency %q, got %q", w, g)
4213 }
4214
4215 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4216 t.Errorf("expected device bin dependency %q, got %q", w, g)
4217 }
4218
4219 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4220 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4221 }
4222
4223}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004224
4225func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004226 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004227 ctx := testCc(t, `
4228 cc_library_shared {
4229 name: "libclient",
4230 srcs: ["foo.c"],
4231 shared_libs: ["libfoo#1"],
4232 }
4233
4234 cc_library_shared {
4235 name: "libfoo",
4236 srcs: ["foo.c"],
4237 shared_libs: ["libbar"],
4238 export_shared_lib_headers: ["libbar"],
4239 stubs: {
4240 symbol_file: "foo.map.txt",
4241 versions: ["1", "2", "3"],
4242 },
4243 }
4244
4245 cc_library_shared {
4246 name: "libbar",
4247 export_include_dirs: ["include/libbar"],
4248 srcs: ["foo.c"],
4249 }`)
4250
4251 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4252
4253 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4254 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4255 }
4256}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004257
Vinh Tran09581952023-05-16 16:03:20 -04004258func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004259 t.Parallel()
4260 ctx := android.GroupFixturePreparers(
4261 prepareForCcTest,
4262 aidl_library.PrepareForTestWithAidlLibrary,
4263 android.MockFS{
4264 "package_bar/Android.bp": []byte(`
4265 aidl_library {
4266 name: "bar",
4267 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004268 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004269 strip_import_prefix: "x",
4270 }
4271 `)}.AddToFixture(),
4272 android.MockFS{
4273 "package_foo/Android.bp": []byte(`
4274 aidl_library {
4275 name: "foo",
4276 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004277 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004278 strip_import_prefix: "a",
4279 deps: ["bar"],
4280 }
4281 cc_library {
4282 name: "libfoo",
4283 aidl: {
4284 libs: ["foo"],
4285 }
4286 }
4287 `),
4288 }.AddToFixture(),
4289 ).RunTest(t).TestContext
4290
4291 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004292
4293 android.AssertPathsRelativeToTopEquals(
4294 t,
4295 "aidl headers",
4296 []string{
4297 "package_bar/x/HeaderBar.aidl",
4298 "package_foo/a/HeaderFoo.aidl",
4299 "package_foo/a/b/Foo.aidl",
4300 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4301 },
4302 libfoo.Rule("aidl_library").Implicits,
4303 )
4304
4305 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004306 aidlCommand := manifest.Commands[0].GetCommand()
4307
4308 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4309 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4310 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4311 }
4312
4313 outputs := strings.Join(libfoo.AllOutputs(), " ")
4314
Vinh Tran09581952023-05-16 16:03:20 -04004315 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4316 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4317 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004318 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4319 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004320 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4321 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4322 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004323 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4324}
4325
Jooyung Hane197d8b2021-01-05 10:33:16 +09004326func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004327 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004328 ctx := android.GroupFixturePreparers(
4329 prepareForCcTest,
4330 aidl_library.PrepareForTestWithAidlLibrary,
4331 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004332 cc_library {
4333 name: "libfoo",
4334 srcs: ["a/Foo.aidl"],
4335 aidl: { flags: ["-Werror"], },
4336 }
4337 `)
4338
4339 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4340 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4341 aidlCommand := manifest.Commands[0].GetCommand()
4342 expectedAidlFlag := "-Werror"
4343 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4344 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4345 }
4346}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004347
Jooyung Han07f70c02021-11-06 07:08:45 +09004348func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004349 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004350 for _, tc := range []struct {
4351 name string
4352 sdkVersion string
4353 variant string
4354 expected string
4355 }{
4356 {
4357 name: "default is current",
4358 sdkVersion: "",
4359 variant: "android_arm64_armv8-a_static",
4360 expected: "platform_apis",
4361 },
4362 {
4363 name: "use sdk_version",
4364 sdkVersion: `sdk_version: "29"`,
4365 variant: "android_arm64_armv8-a_static",
4366 expected: "platform_apis",
4367 },
4368 {
4369 name: "use sdk_version(sdk variant)",
4370 sdkVersion: `sdk_version: "29"`,
4371 variant: "android_arm64_armv8-a_sdk_static",
4372 expected: "29",
4373 },
4374 {
4375 name: "use min_sdk_version",
4376 sdkVersion: `min_sdk_version: "29"`,
4377 variant: "android_arm64_armv8-a_static",
4378 expected: "29",
4379 },
4380 } {
4381 t.Run(tc.name, func(t *testing.T) {
4382 ctx := testCc(t, `
4383 cc_library {
4384 name: "libfoo",
4385 stl: "none",
4386 srcs: ["a/Foo.aidl"],
4387 `+tc.sdkVersion+`
4388 }
4389 `)
4390 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4391 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4392 aidlCommand := manifest.Commands[0].GetCommand()
4393 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4394 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4395 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4396 }
4397 })
4398 }
4399}
4400
Vinh Tran09581952023-05-16 16:03:20 -04004401func TestInvalidAidlProp(t *testing.T) {
4402 t.Parallel()
4403
4404 testCases := []struct {
4405 description string
4406 bp string
4407 }{
4408 {
4409 description: "Invalid use of aidl.libs and aidl.include_dirs",
4410 bp: `
4411 cc_library {
4412 name: "foo",
4413 aidl: {
4414 libs: ["foo_aidl"],
4415 include_dirs: ["bar/include"],
4416 }
4417 }
4418 `,
4419 },
4420 {
4421 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4422 bp: `
4423 cc_library {
4424 name: "foo",
4425 aidl: {
4426 libs: ["foo_aidl"],
4427 local_include_dirs: ["include"],
4428 }
4429 }
4430 `,
4431 },
4432 }
4433
4434 for _, testCase := range testCases {
4435 t.Run(testCase.description, func(t *testing.T) {
4436 bp := `
4437 aidl_library {
4438 name: "foo_aidl",
4439 srcs: ["Foo.aidl"],
4440 } ` + testCase.bp
4441 android.GroupFixturePreparers(
4442 prepareForCcTest,
4443 aidl_library.PrepareForTestWithAidlLibrary.
4444 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4445 ).RunTestWithBp(t, bp)
4446 })
4447 }
4448}
4449
Jiyong Parka008fb02021-03-16 17:15:53 +09004450func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004451 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004452 ctx := testCc(t, `
4453 cc_library_shared {
4454 name: "libfoo",
4455 srcs: ["foo.c"],
4456 min_sdk_version: "29",
4457 }`)
4458
4459 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4460 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4461}
4462
Vinh Tranf1924742022-06-24 16:40:11 -04004463func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004464 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004465 bp := `
4466 cc_library_shared {
4467 name: "libfoo",
4468 srcs: ["foo.c"],
4469 min_sdk_version: "S",
4470 }
4471 `
4472 result := android.GroupFixturePreparers(
4473 prepareForCcTest,
4474 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4475 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4476 }),
4477 ).RunTestWithBp(t, bp)
4478 ctx := result.TestContext
4479 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4480 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4481}
4482
Paul Duffin3cb603e2021-02-19 13:57:10 +00004483func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004484 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004485
4486 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4487 // embedded newline characters alone.
4488 trimIndentingSpaces := func(s string) string {
4489 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4490 }
4491
4492 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4493 t.Helper()
4494 expected = trimIndentingSpaces(expected)
4495 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4496 if expected != actual {
4497 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4498 }
4499 }
4500
4501 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4502
4503 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4504 t.Helper()
4505 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4506 name := module.Name()
4507
4508 for _, checker := range checkers {
4509 checker(t, name, exported)
4510 }
4511 }
4512
4513 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4514 return func(t *testing.T, name string, exported FlagExporterInfo) {
4515 t.Helper()
4516 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4517 }
4518 }
4519
4520 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4521 return func(t *testing.T, name string, exported FlagExporterInfo) {
4522 t.Helper()
4523 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4524 }
4525 }
4526
4527 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4528 return func(t *testing.T, name string, exported FlagExporterInfo) {
4529 t.Helper()
4530 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4531 }
4532 }
4533
4534 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4535 return func(t *testing.T, name string, exported FlagExporterInfo) {
4536 t.Helper()
4537 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4538 }
4539 }
4540
4541 genRuleModules := `
4542 genrule {
4543 name: "genrule_foo",
4544 cmd: "generate-foo",
4545 out: [
4546 "generated_headers/foo/generated_header.h",
4547 ],
4548 export_include_dirs: [
4549 "generated_headers",
4550 ],
4551 }
4552
4553 genrule {
4554 name: "genrule_bar",
4555 cmd: "generate-bar",
4556 out: [
4557 "generated_headers/bar/generated_header.h",
4558 ],
4559 export_include_dirs: [
4560 "generated_headers",
4561 ],
4562 }
4563 `
4564
4565 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4566 ctx := testCc(t, genRuleModules+`
4567 cc_library {
4568 name: "libfoo",
4569 srcs: ["foo.c"],
4570 export_include_dirs: ["foo/standard"],
4571 export_system_include_dirs: ["foo/system"],
4572 generated_headers: ["genrule_foo"],
4573 export_generated_headers: ["genrule_foo"],
4574 }
4575
4576 cc_library {
4577 name: "libbar",
4578 srcs: ["bar.c"],
4579 shared_libs: ["libfoo"],
4580 export_include_dirs: ["bar/standard"],
4581 export_system_include_dirs: ["bar/system"],
4582 generated_headers: ["genrule_bar"],
4583 export_generated_headers: ["genrule_bar"],
4584 }
4585 `)
4586 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4587 checkIncludeDirs(t, ctx, foo,
4588 expectedIncludeDirs(`
4589 foo/standard
4590 .intermediates/genrule_foo/gen/generated_headers
4591 `),
4592 expectedSystemIncludeDirs(`foo/system`),
4593 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4594 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4595 )
4596
4597 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4598 checkIncludeDirs(t, ctx, bar,
4599 expectedIncludeDirs(`
4600 bar/standard
4601 .intermediates/genrule_bar/gen/generated_headers
4602 `),
4603 expectedSystemIncludeDirs(`bar/system`),
4604 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4605 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4606 )
4607 })
4608
4609 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4610 ctx := testCc(t, genRuleModules+`
4611 cc_library {
4612 name: "libfoo",
4613 srcs: ["foo.c"],
4614 export_include_dirs: ["foo/standard"],
4615 export_system_include_dirs: ["foo/system"],
4616 generated_headers: ["genrule_foo"],
4617 export_generated_headers: ["genrule_foo"],
4618 }
4619
4620 cc_library {
4621 name: "libbar",
4622 srcs: ["bar.c"],
4623 whole_static_libs: ["libfoo"],
4624 export_include_dirs: ["bar/standard"],
4625 export_system_include_dirs: ["bar/system"],
4626 generated_headers: ["genrule_bar"],
4627 export_generated_headers: ["genrule_bar"],
4628 }
4629 `)
4630 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4631 checkIncludeDirs(t, ctx, foo,
4632 expectedIncludeDirs(`
4633 foo/standard
4634 .intermediates/genrule_foo/gen/generated_headers
4635 `),
4636 expectedSystemIncludeDirs(`foo/system`),
4637 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4638 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4639 )
4640
4641 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4642 checkIncludeDirs(t, ctx, bar,
4643 expectedIncludeDirs(`
4644 bar/standard
4645 foo/standard
4646 .intermediates/genrule_foo/gen/generated_headers
4647 .intermediates/genrule_bar/gen/generated_headers
4648 `),
4649 expectedSystemIncludeDirs(`
4650 bar/system
4651 foo/system
4652 `),
4653 expectedGeneratedHeaders(`
4654 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4655 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4656 `),
4657 expectedOrderOnlyDeps(`
4658 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4659 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4660 `),
4661 )
4662 })
4663
Paul Duffin3cb603e2021-02-19 13:57:10 +00004664 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004665 ctx := android.GroupFixturePreparers(
4666 prepareForCcTest,
4667 aidl_library.PrepareForTestWithAidlLibrary,
4668 ).RunTestWithBp(t, `
4669 aidl_library {
4670 name: "libfoo_aidl",
4671 srcs: ["x/y/Bar.aidl"],
4672 strip_import_prefix: "x",
4673 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004674 cc_library_shared {
4675 name: "libfoo",
4676 srcs: [
4677 "foo.c",
4678 "b.aidl",
4679 "a.proto",
4680 ],
4681 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004682 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004683 export_aidl_headers: true,
4684 }
4685 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004686 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004687 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4688 checkIncludeDirs(t, ctx, foo,
4689 expectedIncludeDirs(`
4690 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004691 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004692 `),
4693 expectedSystemIncludeDirs(``),
4694 expectedGeneratedHeaders(`
4695 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4696 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4697 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004698 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4699 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4700 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004701 `),
4702 expectedOrderOnlyDeps(`
4703 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4704 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4705 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004706 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4707 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4708 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004709 `),
4710 )
4711 })
4712
Paul Duffin3cb603e2021-02-19 13:57:10 +00004713 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4714 ctx := testCc(t, genRuleModules+`
4715 cc_library_shared {
4716 name: "libfoo",
4717 srcs: [
4718 "foo.c",
4719 "b.aidl",
4720 "a.proto",
4721 ],
4722 proto: {
4723 export_proto_headers: true,
4724 }
4725 }
4726 `)
4727 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4728 checkIncludeDirs(t, ctx, foo,
4729 expectedIncludeDirs(`
4730 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4731 `),
4732 expectedSystemIncludeDirs(``),
4733 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004734 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4735 `),
4736 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004737 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4738 `),
4739 )
4740 })
4741
Paul Duffin33056e82021-02-19 13:49:08 +00004742 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004743 ctx := testCc(t, genRuleModules+`
4744 cc_library_shared {
4745 name: "libfoo",
4746 srcs: [
4747 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004748 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004749 "b.aidl",
4750 "a.proto",
4751 ],
4752 }
4753 `)
4754 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4755 checkIncludeDirs(t, ctx, foo,
4756 expectedIncludeDirs(`
4757 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4758 `),
4759 expectedSystemIncludeDirs(``),
4760 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004761 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004762 `),
4763 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004764 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4765 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004766 `),
4767 )
4768 })
4769}
Colin Crossae628182021-06-14 16:52:28 -07004770
4771func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004772 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004773 baseExpectedFlags := []string{
4774 "${config.ArmThumbCflags}",
4775 "${config.ArmCflags}",
4776 "${config.CommonGlobalCflags}",
4777 "${config.DeviceGlobalCflags}",
4778 "${config.ExternalCflags}",
4779 "${config.ArmToolchainCflags}",
4780 "${config.ArmArmv7ANeonCflags}",
4781 "${config.ArmGenericCflags}",
4782 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004783 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004784 }
4785
4786 expectedIncludes := []string{
4787 "external/foo/android_arm_export_include_dirs",
4788 "external/foo/lib32_export_include_dirs",
4789 "external/foo/arm_export_include_dirs",
4790 "external/foo/android_export_include_dirs",
4791 "external/foo/linux_export_include_dirs",
4792 "external/foo/export_include_dirs",
4793 "external/foo/android_arm_local_include_dirs",
4794 "external/foo/lib32_local_include_dirs",
4795 "external/foo/arm_local_include_dirs",
4796 "external/foo/android_local_include_dirs",
4797 "external/foo/linux_local_include_dirs",
4798 "external/foo/local_include_dirs",
4799 "external/foo",
4800 "external/foo/libheader1",
4801 "external/foo/libheader2",
4802 "external/foo/libwhole1",
4803 "external/foo/libwhole2",
4804 "external/foo/libstatic1",
4805 "external/foo/libstatic2",
4806 "external/foo/libshared1",
4807 "external/foo/libshared2",
4808 "external/foo/liblinux",
4809 "external/foo/libandroid",
4810 "external/foo/libarm",
4811 "external/foo/lib32",
4812 "external/foo/libandroid_arm",
4813 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004814 }
4815
4816 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4817 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4818
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004819 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004820 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004821 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004822
4823 lastIncludes := []string{
4824 "out/soong/ndk/sysroot/usr/include",
4825 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4826 }
4827
4828 combineSlices := func(slices ...[]string) []string {
4829 var ret []string
4830 for _, s := range slices {
4831 ret = append(ret, s...)
4832 }
4833 return ret
4834 }
4835
4836 testCases := []struct {
4837 name string
4838 src string
4839 expected []string
4840 }{
4841 {
4842 name: "c",
4843 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004844 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004845 },
4846 {
4847 name: "cc",
4848 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004849 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004850 },
4851 {
4852 name: "assemble",
4853 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004854 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004855 },
4856 }
4857
4858 for _, tc := range testCases {
4859 t.Run(tc.name, func(t *testing.T) {
4860 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004861 cc_library {
4862 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004863 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004864 cflags: ["-std=candcpp"],
4865 conlyflags: ["-std=conly"],
4866 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004867 local_include_dirs: ["local_include_dirs"],
4868 export_include_dirs: ["export_include_dirs"],
4869 export_system_include_dirs: ["export_system_include_dirs"],
4870 static_libs: ["libstatic1", "libstatic2"],
4871 whole_static_libs: ["libwhole1", "libwhole2"],
4872 shared_libs: ["libshared1", "libshared2"],
4873 header_libs: ["libheader1", "libheader2"],
4874 target: {
4875 android: {
4876 shared_libs: ["libandroid"],
4877 local_include_dirs: ["android_local_include_dirs"],
4878 export_include_dirs: ["android_export_include_dirs"],
4879 },
4880 android_arm: {
4881 shared_libs: ["libandroid_arm"],
4882 local_include_dirs: ["android_arm_local_include_dirs"],
4883 export_include_dirs: ["android_arm_export_include_dirs"],
4884 },
4885 linux: {
4886 shared_libs: ["liblinux"],
4887 local_include_dirs: ["linux_local_include_dirs"],
4888 export_include_dirs: ["linux_export_include_dirs"],
4889 },
4890 },
4891 multilib: {
4892 lib32: {
4893 shared_libs: ["lib32"],
4894 local_include_dirs: ["lib32_local_include_dirs"],
4895 export_include_dirs: ["lib32_export_include_dirs"],
4896 },
4897 },
4898 arch: {
4899 arm: {
4900 shared_libs: ["libarm"],
4901 local_include_dirs: ["arm_local_include_dirs"],
4902 export_include_dirs: ["arm_export_include_dirs"],
4903 },
4904 },
4905 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004906 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004907 }
4908
4909 cc_library_headers {
4910 name: "libheader1",
4911 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004912 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004913 stl: "none",
4914 }
4915
4916 cc_library_headers {
4917 name: "libheader2",
4918 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004919 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004920 stl: "none",
4921 }
Liz Kammer08572c62021-09-30 10:11:04 -04004922 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004923
Liz Kammer08572c62021-09-30 10:11:04 -04004924 libs := []string{
4925 "libstatic1",
4926 "libstatic2",
4927 "libwhole1",
4928 "libwhole2",
4929 "libshared1",
4930 "libshared2",
4931 "libandroid",
4932 "libandroid_arm",
4933 "liblinux",
4934 "lib32",
4935 "libarm",
4936 }
Colin Crossae628182021-06-14 16:52:28 -07004937
Liz Kammer08572c62021-09-30 10:11:04 -04004938 for _, lib := range libs {
4939 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004940 cc_library {
4941 name: "%s",
4942 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004943 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004944 stl: "none",
4945 }
4946 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004947 }
4948
4949 ctx := android.GroupFixturePreparers(
4950 PrepareForIntegrationTestWithCc,
4951 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4952 ).RunTest(t)
4953 // Use the arm variant instead of the arm64 variant so that it gets headers from
4954 // ndk_libandroid_support to test LateStaticLibs.
4955 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4956
4957 var includes []string
4958 flags := strings.Split(cflags, " ")
4959 for _, flag := range flags {
4960 if strings.HasPrefix(flag, "-I") {
4961 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4962 } else if flag == "-isystem" {
4963 // skip isystem, include next
4964 } else if len(flag) > 0 {
4965 includes = append(includes, flag)
4966 }
4967 }
4968
4969 android.AssertArrayString(t, "includes", tc.expected, includes)
4970 })
Colin Crossae628182021-06-14 16:52:28 -07004971 }
4972
Colin Crossae628182021-06-14 16:52:28 -07004973}
Alixb5f6d9e2022-04-20 23:00:58 +00004974
zijunzhao933e3802023-01-12 07:26:20 +00004975func TestAddnoOverride64GlobalCflags(t *testing.T) {
4976 t.Parallel()
4977 ctx := testCc(t, `
4978 cc_library_shared {
4979 name: "libclient",
4980 srcs: ["foo.c"],
4981 shared_libs: ["libfoo#1"],
4982 }
4983
4984 cc_library_shared {
4985 name: "libfoo",
4986 srcs: ["foo.c"],
4987 shared_libs: ["libbar"],
4988 export_shared_lib_headers: ["libbar"],
4989 stubs: {
4990 symbol_file: "foo.map.txt",
4991 versions: ["1", "2", "3"],
4992 },
4993 }
4994
4995 cc_library_shared {
4996 name: "libbar",
4997 export_include_dirs: ["include/libbar"],
4998 srcs: ["foo.c"],
4999 }`)
5000
5001 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5002
5003 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5004 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5005 }
5006}
5007
Alixb5f6d9e2022-04-20 23:00:58 +00005008func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005009 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005010 tests := []struct {
5011 name string
5012 clang bool
5013 BuildBrokenClangProperty bool
5014 err string
5015 }{
5016 {
5017 name: "error when clang is set to false",
5018 clang: false,
5019 err: "is no longer supported",
5020 },
5021 {
5022 name: "error when clang is set to true",
5023 clang: true,
5024 err: "property is deprecated, see Changes.md",
5025 },
5026 {
5027 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5028 clang: true,
5029 BuildBrokenClangProperty: true,
5030 },
5031 }
5032
5033 for _, test := range tests {
5034 t.Run(test.name, func(t *testing.T) {
5035 bp := fmt.Sprintf(`
5036 cc_library {
5037 name: "foo",
5038 clang: %t,
5039 }`, test.clang)
5040
5041 if test.err == "" {
5042 android.GroupFixturePreparers(
5043 prepareForCcTest,
5044 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5045 if test.BuildBrokenClangProperty {
5046 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5047 }
5048 }),
5049 ).RunTestWithBp(t, bp)
5050 } else {
5051 prepareForCcTest.
5052 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5053 RunTestWithBp(t, bp)
5054 }
5055 })
5056 }
5057}
Alix Espinoef47e542022-09-14 19:10:51 +00005058
5059func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005060 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005061 tests := []struct {
5062 name string
5063 clangAsFlags []string
5064 BuildBrokenClangAsFlags bool
5065 err string
5066 }{
5067 {
5068 name: "error when clang_asflags is set",
5069 clangAsFlags: []string{"-a", "-b"},
5070 err: "clang_asflags: property is deprecated",
5071 },
5072 {
5073 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5074 clangAsFlags: []string{"-a", "-b"},
5075 BuildBrokenClangAsFlags: true,
5076 },
5077 }
5078
5079 for _, test := range tests {
5080 t.Run(test.name, func(t *testing.T) {
5081 bp := fmt.Sprintf(`
5082 cc_library {
5083 name: "foo",
5084 clang_asflags: %s,
5085 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5086
5087 if test.err == "" {
5088 android.GroupFixturePreparers(
5089 prepareForCcTest,
5090 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5091 if test.BuildBrokenClangAsFlags {
5092 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5093 }
5094 }),
5095 ).RunTestWithBp(t, bp)
5096 } else {
5097 prepareForCcTest.
5098 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5099 RunTestWithBp(t, bp)
5100 }
5101 })
5102 }
5103}
5104
5105func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005106 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005107 tests := []struct {
5108 name string
5109 clangCFlags []string
5110 BuildBrokenClangCFlags bool
5111 err string
5112 }{
5113 {
5114 name: "error when clang_cflags is set",
5115 clangCFlags: []string{"-a", "-b"},
5116 err: "clang_cflags: property is deprecated",
5117 },
5118 {
5119 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5120 clangCFlags: []string{"-a", "-b"},
5121 BuildBrokenClangCFlags: true,
5122 },
5123 }
5124
5125 for _, test := range tests {
5126 t.Run(test.name, func(t *testing.T) {
5127 bp := fmt.Sprintf(`
5128 cc_library {
5129 name: "foo",
5130 clang_cflags: %s,
5131 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5132
5133 if test.err == "" {
5134 android.GroupFixturePreparers(
5135 prepareForCcTest,
5136 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5137 if test.BuildBrokenClangCFlags {
5138 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5139 }
5140 }),
5141 ).RunTestWithBp(t, bp)
5142 } else {
5143 prepareForCcTest.
5144 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5145 RunTestWithBp(t, bp)
5146 }
5147 })
5148 }
5149}
Yu Liue4312402023-01-18 09:15:31 -08005150
5151func TestDclaLibraryInApex(t *testing.T) {
5152 t.Parallel()
5153 bp := `
5154 cc_library_shared {
5155 name: "cc_lib_in_apex",
5156 srcs: ["foo.cc"],
5157 apex_available: ["myapex"],
5158 bazel_module: { label: "//foo/bar:bar" },
5159 }`
5160 label := "//foo/bar:bar"
5161 arch64 := "arm64_armv8-a"
5162 arch32 := "arm_armv7-a-neon"
5163 apexCfgKey := android.ApexConfigKey{
5164 WithinApex: true,
5165 ApexSdkVersion: "28",
5166 }
5167
5168 result := android.GroupFixturePreparers(
5169 prepareForCcTest,
5170 android.FixtureRegisterWithContext(registerTestMutators),
5171 android.FixtureModifyConfig(func(config android.Config) {
5172 config.BazelContext = android.MockBazelContext{
5173 OutputBaseDir: "outputbase",
5174 LabelToCcInfo: map[string]cquery.CcInfo{
5175 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5176 RootDynamicLibraries: []string{"foo.so"},
5177 },
5178 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5179 RootDynamicLibraries: []string{"foo.so"},
5180 },
5181 },
5182 BazelRequests: make(map[string]bool),
5183 }
5184 }),
5185 ).RunTestWithBp(t, bp)
5186 ctx := result.TestContext
5187
5188 // Test if the bazel request is queued correctly
5189 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5190 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5191 t.Errorf("Bazel request was not queued: %s", key)
5192 }
5193
5194 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5195 producer := sharedFoo.(android.OutputFileProducer)
5196 outputFiles, err := producer.OutputFiles("")
5197 if err != nil {
5198 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5199 }
5200 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5201 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5202}
Sam Delmericoef69d472023-04-18 17:32:43 -04005203
5204func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5205 t.Parallel()
5206 bp := `
5207 cc_library_static {
5208 name: "foo_ubsan_minimal",
5209 srcs: ["foo.cc"],
5210 bazel_module: { label: "//foo_ubsan_minimal" },
5211 sanitize: {
5212 all_undefined: true,
5213 integer_overflow: true,
5214 },
5215 }
5216 cc_library_static {
5217 name: "foo",
5218 srcs: ["foo.cc"],
5219 bazel_module: { label: "//foo" },
5220 sanitize: {
5221 address: true,
5222 hwaddress: true,
5223 fuzzer: true,
5224 integer_overflow: true,
5225 scs: true,
5226 },
5227 }
5228 cc_library_static {
5229 name: "foo_tsan",
5230 srcs: ["foo.cc"],
5231 bazel_module: { label: "//foo_tsan" },
5232 sanitize: {
5233 thread: true,
5234 },
5235 }
5236 cc_library_static {
5237 name: "foo_cfi",
5238 srcs: ["foo.cc"],
5239 bazel_module: { label: "//foo_cfi" },
5240 sanitize: {
5241 cfi: true,
5242 },
5243 }
5244 cc_library_static {
5245 name: "foo_memtag_stack",
5246 srcs: ["foo.cc"],
5247 bazel_module: { label: "//foo_memtag_stack" },
5248 sanitize: {
5249 memtag_stack: true,
5250 },
5251 }
5252 cc_library_static {
5253 name: "foo_memtag_heap",
5254 srcs: ["foo.cc"],
5255 bazel_module: { label: "//foo_memtag_heap" },
5256 sanitize: {
5257 memtag_heap: true,
5258 },
5259 }
5260 cc_library_static {
5261 name: "foo_safestack",
5262 srcs: ["foo.cc"],
5263 bazel_module: { label: "//foo_safestack" },
5264 sanitize: {
5265 safestack: true,
5266 },
5267 }
5268 cc_library_static {
5269 name: "foo_scudo",
5270 srcs: ["foo.cc"],
5271 bazel_module: { label: "//foo_scudo" },
5272 sanitize: {
5273 scudo: true,
5274 },
5275 }
5276 `
5277 testcases := []struct {
5278 name string
5279 variant string
5280 expectedOutputPaths []string
5281 }{
5282 {
5283 name: "foo_ubsan_minimal",
5284 variant: "android_arm64_armv8-a_static_apex28",
5285 expectedOutputPaths: []string{
5286 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5287 },
5288 },
5289 {
5290 name: "foo",
5291 variant: "android_arm64_armv8-a_static_apex28",
5292 expectedOutputPaths: []string{
5293 "outputbase/execroot/__main__/foo.a",
5294 },
5295 },
5296 {
5297 name: "foo",
5298 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5299 expectedOutputPaths: []string{
5300 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5301 },
5302 },
5303 {
5304 name: "foo",
5305 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5306 expectedOutputPaths: []string{
5307 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5308 },
5309 },
5310 {
5311 name: "foo",
5312 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5313 expectedOutputPaths: []string{
5314 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5315 },
5316 },
5317 {
5318 name: "foo",
5319 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5320 expectedOutputPaths: []string{
5321 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5322 },
5323 },
5324 {
5325 name: "foo",
5326 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5327 expectedOutputPaths: []string{
5328 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5329 },
5330 },
5331 {
5332 name: "foo",
5333 variant: "android_arm64_armv8-a_static_scs_apex28",
5334 expectedOutputPaths: []string{
5335 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5336 },
5337 },
5338 {
5339 name: "foo",
5340 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5341 expectedOutputPaths: []string{
5342 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5343 },
5344 },
5345 {
5346 name: "foo",
5347 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5348 expectedOutputPaths: []string{
5349 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5350 },
5351 },
5352 {
5353 name: "foo_tsan",
5354 variant: "android_arm64_armv8-a_static_apex28",
5355 expectedOutputPaths: []string{
5356 "outputbase/execroot/__main__/foo_tsan.a",
5357 },
5358 },
5359 {
5360 name: "foo_tsan",
5361 variant: "android_arm64_armv8-a_static_tsan_apex28",
5362 expectedOutputPaths: []string{
5363 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5364 },
5365 },
5366 {
5367 name: "foo_cfi",
5368 variant: "android_arm64_armv8-a_static_apex28",
5369 expectedOutputPaths: []string{
5370 "outputbase/execroot/__main__/foo_cfi.a",
5371 },
5372 },
5373 {
5374 name: "foo_cfi",
5375 variant: "android_arm64_armv8-a_static_cfi_apex28",
5376 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005377 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005378 },
5379 },
5380 {
5381 name: "foo_memtag_stack",
5382 variant: "android_arm64_armv8-a_static_apex28",
5383 expectedOutputPaths: []string{
5384 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5385 },
5386 },
5387 {
5388 name: "foo_memtag_heap",
5389 variant: "android_arm64_armv8-a_static_apex28",
5390 expectedOutputPaths: []string{
5391 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5392 },
5393 },
5394 {
5395 name: "foo_safestack",
5396 variant: "android_arm64_armv8-a_static_apex28",
5397 expectedOutputPaths: []string{
5398 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5399 },
5400 },
5401 {
5402 name: "foo_scudo",
5403 variant: "android_arm64_armv8-a_static_apex28",
5404 expectedOutputPaths: []string{
5405 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5406 },
5407 },
5408 }
5409
5410 ctx := android.GroupFixturePreparers(
5411 prepareForCcTest,
5412 prepareForAsanTest,
5413 android.FixtureRegisterWithContext(registerTestMutators),
5414 android.FixtureModifyConfig(func(config android.Config) {
5415 config.BazelContext = android.MockBazelContext{
5416 OutputBaseDir: "outputbase",
5417 LabelToCcInfo: map[string]cquery.CcInfo{
5418 "//foo_ubsan_minimal": {
5419 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5420 },
5421 "//foo": {
5422 RootStaticArchives: []string{"foo.a"},
5423 },
5424 "//foo_tsan": {
5425 RootStaticArchives: []string{"foo_tsan.a"},
5426 },
5427 "//foo_cfi": {
5428 RootStaticArchives: []string{"foo_cfi.a"},
5429 },
5430 "//foo_memtag_stack": {
5431 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5432 },
5433 "//foo_memtag_heap": {
5434 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5435 },
5436 "//foo_safestack": {
5437 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5438 },
5439 "//foo_scudo": {
5440 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5441 },
5442 },
5443 }
5444 }),
5445 ).RunTestWithBp(t, bp).TestContext
5446
5447 for _, tc := range testcases {
5448 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5449 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5450 if err != nil {
5451 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5452 }
5453 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5454 }
5455}