blob: 321bd380b33b05c73a70c46d4c28886380bd17da [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"
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090029
30 "github.com/google/blueprint"
Colin Cross74d1ec02015-04-28 13:30:13 -070031)
32
Yu Liue4312402023-01-18 09:15:31 -080033func init() {
34 registerTestMutators(android.InitRegistrationContext)
35}
36
Jiyong Park6a43f042017-10-12 23:05:00 +090037func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000038 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090039}
40
Paul Duffin2e6f90e2021-03-22 23:20:25 +000041var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000042 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000043 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Justin Yun41cbb5e2023-11-29 17:58:16 +090044 variables.VendorApiLevel = StringPtr("202404")
Paul Duffin02a3d652021-02-24 18:51:54 +000045 variables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090046 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000047 }),
48)
49
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090050// TODO(b/316829758) Update prepareForCcTest with this configuration and remove prepareForCcTestWithoutVndk
51var prepareForCcTestWithoutVndk = android.GroupFixturePreparers(
52 PrepareForIntegrationTestWithCc,
53 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
54 variables.VendorApiLevel = StringPtr("202404")
55 }),
56)
57
Yu Liue4312402023-01-18 09:15:31 -080058var apexVariationName = "apex28"
59var apexVersion = "28"
60
61func registerTestMutators(ctx android.RegistrationContext) {
62 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
63 ctx.BottomUp("apex", testApexMutator).Parallel()
Yu Liue4312402023-01-18 09:15:31 -080064 })
65}
66
Yu Liue4312402023-01-18 09:15:31 -080067func testApexMutator(mctx android.BottomUpMutatorContext) {
68 modules := mctx.CreateVariations(apexVariationName)
69 apexInfo := android.ApexInfo{
70 ApexVariationName: apexVariationName,
71 MinSdkVersion: android.ApiLevelForTest(apexVersion),
72 }
73 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
74}
75
Paul Duffin8567f222021-03-23 00:02:06 +000076// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000077//
78// See testCc for an explanation as to how to stop using this deprecated method.
79//
80// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080081func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070082 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000083 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000084 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090085}
86
Paul Duffin8567f222021-03-23 00:02:06 +000087// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000088//
Paul Duffin8567f222021-03-23 00:02:06 +000089// 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 +000090// easier to customize the test behavior.
91//
92// 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 +000093// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000094// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
95// that it did not change the test behavior unexpectedly.
96//
97// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080098func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080099 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000100 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000101 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800102}
103
Paul Duffin8567f222021-03-23 00:02:06 +0000104// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000105//
106// See testCc for an explanation as to how to stop using this deprecated method.
107//
108// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900109func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800110 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800111
Paul Duffin8567f222021-03-23 00:02:06 +0000112 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000113 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
114 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800115}
116
Paul Duffin8567f222021-03-23 00:02:06 +0000117// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000118//
119// See testCc for an explanation as to how to stop using this deprecated method.
120//
121// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900122func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900123 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000124 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900125 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900126 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900127 testCcErrorWithConfig(t, pattern, config)
128 return
129}
130
Paul Duffin8567f222021-03-23 00:02:06 +0000131// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000132//
133// See testCc for an explanation as to how to stop using this deprecated method.
134//
135// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900136func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900137 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000138 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900139 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900140 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900141 testCcErrorWithConfig(t, pattern, config)
142 return
143}
144
Logan Chienf3511742017-10-31 18:04:35 +0800145const (
Colin Cross7113d202019-11-20 16:39:12 -0800146 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900147 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
148 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800149 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800150)
151
Paul Duffindb462dd2021-03-21 22:01:55 +0000152// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
153// running it in a fixture that requires all source files to exist.
154func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
155 android.GroupFixturePreparers(
156 PrepareForTestWithCcDefaultModules,
157 android.PrepareForTestDisallowNonExistentPaths,
158 ).RunTest(t)
159}
160
Jiyong Park6a43f042017-10-12 23:05:00 +0900161func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400162 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900163 ctx := testCc(t, `
164 cc_library {
165 name: "libTest",
166 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700167 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800168 nocrt: true,
169 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900170 vendor_available: true,
171 target: {
172 vendor: {
173 srcs: ["bar.c"],
174 },
175 },
176 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900177 `)
178
Logan Chienf3511742017-10-31 18:04:35 +0800179 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900180 var objs []string
181 for _, o := range ld.Inputs {
182 objs = append(objs, o.Base())
183 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800184 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900185 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
186 }
187}
188
Justin Yun7f99ec72021-04-12 13:19:28 +0900189func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
190 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
191 partitionDefined := false
192 checkPartition := func(specific bool, partition string) {
193 if specific {
194 if expected != partition && !partitionDefined {
195 // The variant is installed to the 'partition'
196 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
197 }
198 partitionDefined = true
199 } else {
200 // The variant is not installed to the 'partition'
201 if expected == partition {
202 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
203 }
204 }
205 }
206 socSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800207 return m.SocSpecific() || m.InstallInVendor()
Justin Yun7f99ec72021-04-12 13:19:28 +0900208 }
209 deviceSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800210 return m.DeviceSpecific() || m.InstallInOdm()
Justin Yun7f99ec72021-04-12 13:19:28 +0900211 }
212 productSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800213 return m.ProductSpecific() || m.InstallInProduct()
Justin Yun7f99ec72021-04-12 13:19:28 +0900214 }
215 systemExtSpecific := func(m *Module) bool {
216 return m.SystemExtSpecific()
217 }
218 checkPartition(socSpecific(mod), "vendor")
219 checkPartition(deviceSpecific(mod), "odm")
220 checkPartition(productSpecific(mod), "product")
221 checkPartition(systemExtSpecific(mod), "system_ext")
222 if !partitionDefined && expected != "system" {
223 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
224 " but installed to system partition", variant, name, expected)
225 }
226}
227
228func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400229 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900230 t.Helper()
231 ctx := prepareForCcTest.RunTestWithBp(t, `
232 cc_library {
233 name: "libsystem",
234 }
235 cc_library {
236 name: "libsystem_ext",
237 system_ext_specific: true,
238 }
239 cc_library {
240 name: "libproduct",
241 product_specific: true,
242 }
243 cc_library {
244 name: "libvendor",
245 vendor: true,
246 }
247 cc_library {
248 name: "libodm",
249 device_specific: true,
250 }
251 cc_library {
252 name: "liball_available",
253 vendor_available: true,
254 product_available: true,
255 }
256 cc_library {
257 name: "libsystem_ext_all_available",
258 system_ext_specific: true,
259 vendor_available: true,
260 product_available: true,
261 }
262 cc_library {
263 name: "liball_available_odm",
264 odm_available: true,
265 product_available: true,
266 }
267 cc_library {
268 name: "libproduct_vendoravailable",
269 product_specific: true,
270 vendor_available: true,
271 }
272 cc_library {
273 name: "libproduct_odmavailable",
274 product_specific: true,
275 odm_available: true,
276 }
277 `).TestContext
278
279 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
280 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
281 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
282 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
283 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
284
285 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
286 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
287 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
288
289 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
290 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
291 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
292
293 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
294 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
295 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
296
297 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
298 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
299
300 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
301 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
302}
303
Logan Chienf3511742017-10-31 18:04:35 +0800304func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900305 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800306
Logan Chiend3c59a22018-03-29 14:08:15 +0800307 t.Helper()
308
Justin Yun0ecf0b22020-02-28 15:07:59 +0900309 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800310
311 // Check library properties.
312 lib, ok := mod.compiler.(*libraryDecorator)
313 if !ok {
314 t.Errorf("%q must have libraryDecorator", name)
315 } else if lib.baseInstaller.subDir != subDir {
316 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
317 lib.baseInstaller.subDir)
318 }
319
320 // Check VNDK properties.
321 if mod.vndkdep == nil {
322 t.Fatalf("%q must have `vndkdep`", name)
323 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700324 if !mod.IsVndk() {
325 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800326 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400327 if mod.IsVndkSp() != isVndkSp {
328 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800329 }
330
331 // Check VNDK extension properties.
332 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500333 if mod.IsVndkExt() != isVndkExt {
334 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800335 }
336
337 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
338 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
339 }
340}
341
Colin Crossf61d03d2023-11-02 16:56:39 -0700342func checkWriteFileOutput(t *testing.T, ctx *android.TestContext, params android.TestingBuildParams, expected []string) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900343 t.Helper()
Colin Crossf61d03d2023-11-02 16:56:39 -0700344 content := android.ContentFromFileRuleForTests(t, ctx, params)
Colin Crosscf371cc2020-11-13 11:48:42 -0800345 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900346 assertArrayString(t, actual, expected)
347}
348
Jooyung Han097087b2019-10-22 19:32:18 +0900349func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
350 t.Helper()
351 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Colin Crossf61d03d2023-11-02 16:56:39 -0700352 checkWriteFileOutput(t, ctx, vndkSnapshot.Output(output), expected)
Jooyung Han2216fb12019-11-06 16:46:15 +0900353}
354
355func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
356 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800357 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800358 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900359}
360
Logan Chienf3511742017-10-31 18:04:35 +0800361func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400362 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800363 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800364 cc_library {
365 name: "libvndk",
366 vendor_available: true,
367 vndk: {
368 enabled: true,
369 },
370 nocrt: true,
371 }
372
373 cc_library {
374 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900375 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800376 vndk: {
377 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900378 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800379 },
380 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900381 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800382 }
383
384 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900385 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800386 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900387 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800388 vndk: {
389 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900390 },
391 nocrt: true,
392 target: {
393 vendor: {
394 cflags: ["-DTEST"],
395 },
396 product: {
397 cflags: ["-DTEST"],
398 },
399 },
400 }
401
402 cc_library {
403 name: "libvndk_sp",
404 vendor_available: true,
405 vndk: {
406 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800407 support_system_process: true,
408 },
409 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900410 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800411 }
412
413 cc_library {
414 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900415 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800416 vndk: {
417 enabled: true,
418 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900419 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800420 },
421 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900422 target: {
423 vendor: {
424 suffix: "-x",
425 },
426 },
Logan Chienf3511742017-10-31 18:04:35 +0800427 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900428
429 cc_library {
430 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900431 vendor_available: true,
432 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900433 vndk: {
434 enabled: true,
435 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900436 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900437 },
438 nocrt: true,
439 target: {
440 vendor: {
441 suffix: "-x",
442 },
443 product: {
444 suffix: "-x",
445 },
446 },
447 }
448
Justin Yun450ae722021-04-16 19:58:18 +0900449 cc_library {
450 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700451 llndk: {
452 symbol_file: "libllndk.map.txt",
453 export_llndk_headers: ["libllndk_headers"],
454 }
Justin Yun450ae722021-04-16 19:58:18 +0900455 }
456
Justin Yun611e8862021-05-24 18:17:33 +0900457 cc_library {
458 name: "libclang_rt.hwasan-llndk",
459 llndk: {
460 symbol_file: "libclang_rt.hwasan.map.txt",
461 }
462 }
463
Colin Cross627280f2021-04-26 16:53:58 -0700464 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900465 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700466 llndk: {
467 llndk_headers: true,
468 },
Justin Yun450ae722021-04-16 19:58:18 +0900469 export_include_dirs: ["include"],
470 }
471
Colin Crosse4e44bc2020-12-28 13:50:21 -0800472 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900473 name: "llndk.libraries.txt",
474 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800475 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900476 name: "vndkcore.libraries.txt",
477 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800478 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900479 name: "vndksp.libraries.txt",
480 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800481 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900482 name: "vndkprivate.libraries.txt",
483 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800484 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900485 name: "vndkproduct.libraries.txt",
486 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800487 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900488 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800489 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900490 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800491 `
492
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000493 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800494 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900495 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800496
497 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800498
Jooyung Han261e1582020-10-20 18:54:21 +0900499 // subdir == "" because VNDK libs are not supposed to be installed separately.
500 // They are installed as part of VNDK APEX instead.
501 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
502 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900503 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900504 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
505 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900506 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900507
Justin Yun6977e8a2020-10-29 18:24:11 +0900508 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
509 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900510
Inseob Kim1f086e22019-05-09 13:29:15 +0900511 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900512 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000513 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900514
515 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
516 "arm64", "armv8-a"))
517 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
518 "arm", "armv7-a-neon"))
519
520 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
521 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900522 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
523
Inseob Kim1f086e22019-05-09 13:29:15 +0900524 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
525 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900526 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900527
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900528 variant := "android_vendor.29_arm64_armv8-a_shared"
529 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900530
Inseob Kim7f283f42020-06-01 21:53:49 +0900531 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
532
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400533 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
534 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
535 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
536 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
537 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
538 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
539 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
540 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900541
Jooyung Han39edb6c2019-11-06 16:53:07 +0900542 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800543 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
544 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
545 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
546 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
547 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900548
Jooyung Han097087b2019-10-22 19:32:18 +0900549 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
550 "LLNDK: libc.so",
551 "LLNDK: libdl.so",
552 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900553 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900554 "LLNDK: libm.so",
555 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900556 "VNDK-SP: libvndk_sp-x.so",
557 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900558 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900559 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900560 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900561 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900562 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900563 "VNDK-private: libvndk-private.so",
564 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900565 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900566 "VNDK-product: libc++.so",
567 "VNDK-product: libvndk_product.so",
568 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900569 })
Justin Yun611e8862021-05-24 18:17:33 +0900570 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 +0900571 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
572 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
573 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 +0900574 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 +0900575 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
576}
577
Yo Chiangbba545e2020-06-09 16:15:37 +0800578func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400579 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800580 ctx := testCc(t, `
581 cc_library {
582 name: "libvndk_host_supported",
583 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900584 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800585 vndk: {
586 enabled: true,
587 },
588 host_supported: true,
589 }
590
591 cc_library {
592 name: "libvndk_host_supported_but_disabled_on_device",
593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900594 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800595 vndk: {
596 enabled: true,
597 },
598 host_supported: true,
599 enabled: false,
600 target: {
601 host: {
602 enabled: true,
603 }
604 }
605 }
606
Colin Crosse4e44bc2020-12-28 13:50:21 -0800607 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800608 name: "vndkcore.libraries.txt",
609 }
610 `)
611
612 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
613}
614
Jooyung Han2216fb12019-11-06 16:46:15 +0900615func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400616 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800617 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800618 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900619 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800620 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800621 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000622 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800623 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900624 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900625 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800626 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900627
Colin Cross45bce852021-11-11 22:47:54 -0800628 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700629 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900630 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900631}
632
633func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400634 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800635 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900636 cc_library {
637 name: "libvndk",
638 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900639 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900640 vndk: {
641 enabled: true,
642 },
643 nocrt: true,
644 }
645
646 cc_library {
647 name: "libvndk_sp",
648 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900649 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900650 vndk: {
651 enabled: true,
652 support_system_process: true,
653 },
654 nocrt: true,
655 }
656
657 cc_library {
658 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900659 vendor_available: true,
660 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900661 vndk: {
662 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900663 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900664 },
665 nocrt: true,
666 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900667
Colin Crosse4e44bc2020-12-28 13:50:21 -0800668 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900669 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800670 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900671 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800672 `
673
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000674 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800675 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900676 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800677 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900678 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800679
680 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
681
682 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900683
Jooyung Han2216fb12019-11-06 16:46:15 +0900684 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900685}
686
Chris Parsons79d66a52020-06-05 17:26:16 -0400687func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400688 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400689 bp := `
690 cc_test_library {
691 name: "test_lib",
692 srcs: ["test_lib.cpp"],
693 gtest: false,
694 }
695
696 cc_test {
697 name: "main_test",
698 data_libs: ["test_lib"],
699 gtest: false,
700 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400701 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400702
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000703 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400704 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900705 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400706 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
707
708 ctx := testCcWithConfig(t, config)
709 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
710 testBinary := module.(*Module).linker.(*testBinary)
711 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
712 if err != nil {
713 t.Errorf("Expected cc_test to produce output files, error: %s", err)
714 return
715 }
716 if len(outputFiles) != 1 {
717 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
718 return
719 }
720 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800721 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons79d66a52020-06-05 17:26:16 -0400722 return
723 }
724
725 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400726 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400727
728 if !strings.HasSuffix(outputPath, "/main_test") {
729 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
730 return
731 }
732 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
733 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
734 return
735 }
736}
737
Chris Parsons216e10a2020-07-09 17:12:52 -0400738func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400739 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400740 bp := `
741 cc_test_library {
742 name: "test_lib",
743 srcs: ["test_lib.cpp"],
744 relative_install_path: "foo/bar/baz",
745 gtest: false,
746 }
747
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400748 cc_binary {
749 name: "test_bin",
750 relative_install_path: "foo/bar/baz",
751 compile_multilib: "both",
752 }
753
Chris Parsons216e10a2020-07-09 17:12:52 -0400754 cc_test {
755 name: "main_test",
756 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400757 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400758 gtest: false,
759 }
760 `
761
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000762 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400763 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900764 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400765 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
766
767 ctx := testCcWithConfig(t, config)
768 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
769 testBinary := module.(*Module).linker.(*testBinary)
770 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
771 if err != nil {
772 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
773 }
774 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400775 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400776 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400777 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800778 t.Fatalf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400779 }
780
781 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400782
783 if !strings.HasSuffix(outputPath, "/main_test") {
784 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
785 }
Colin Crossaa255532020-07-03 13:18:24 -0700786 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400787 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
788 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400789 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400790 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400791 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
792 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
793 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
794 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400795}
796
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000797func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400798 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000799 bp := `
800 cc_test {
801 name: "main_test",
802 srcs: ["main_test.cpp"],
803 test_suites: [
804 "suite_1",
805 "suite_2",
806 ],
807 gtest: false,
808 }
809 `
810
811 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
812 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
813
814 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
815 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
816 if len(compatEntries) != 2 {
817 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
818 }
819 if compatEntries[0] != "suite_1" {
820 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
821 " but was '%s'", compatEntries[0])
822 }
823 if compatEntries[1] != "suite_2" {
824 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
825 " but was '%s'", compatEntries[1])
826 }
827}
828
829func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400830 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000831 bp := `
832 cc_test_library {
833 name: "main_test_lib",
834 srcs: ["main_test_lib.cpp"],
835 test_suites: [
836 "suite_1",
837 "suite_2",
838 ],
839 gtest: false,
840 }
841 `
842
843 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
844 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
845
846 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
847 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
848 if len(compatEntries) != 2 {
849 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
850 }
851 if compatEntries[0] != "suite_1" {
852 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
853 " but was '%s'", compatEntries[0])
854 }
855 if compatEntries[1] != "suite_2" {
856 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
857 " but was '%s'", compatEntries[1])
858 }
859}
860
Justin Yun63e9ec72020-10-29 16:49:43 +0900861func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400862 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900863 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900864 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900865 cc_library {
866 name: "libvndk",
867 vndk: {
868 enabled: true,
869 },
870 nocrt: true,
871 }
872 `)
873
Justin Yunc0d8c492021-01-07 17:45:31 +0900874 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900875 cc_library {
876 name: "libvndk",
877 product_available: true,
878 vndk: {
879 enabled: true,
880 },
881 nocrt: true,
882 }
883 `)
884
Justin Yun6977e8a2020-10-29 18:24:11 +0900885 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
886 cc_library {
887 name: "libvndkprop",
888 vendor_available: true,
889 product_available: true,
890 vndk: {
891 enabled: true,
892 },
893 nocrt: true,
894 target: {
895 vendor: {
896 cflags: ["-DTEST",],
897 },
898 },
899 }
900 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900901}
902
Logan Chiend3c59a22018-03-29 14:08:15 +0800903func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400904 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800905 // Check whether an error is emitted when a VNDK lib depends on a system lib.
906 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
907 cc_library {
908 name: "libvndk",
909 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900910 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800911 vndk: {
912 enabled: true,
913 },
914 shared_libs: ["libfwk"], // Cause error
915 nocrt: true,
916 }
917
918 cc_library {
919 name: "libfwk",
920 nocrt: true,
921 }
922 `)
923
924 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
925 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
926 cc_library {
927 name: "libvndk",
928 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900929 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800930 vndk: {
931 enabled: true,
932 },
933 shared_libs: ["libvendor"], // Cause error
934 nocrt: true,
935 }
936
937 cc_library {
938 name: "libvendor",
939 vendor: true,
940 nocrt: true,
941 }
942 `)
943
944 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
945 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
946 cc_library {
947 name: "libvndk_sp",
948 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900949 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800950 vndk: {
951 enabled: true,
952 support_system_process: true,
953 },
954 shared_libs: ["libfwk"], // Cause error
955 nocrt: true,
956 }
957
958 cc_library {
959 name: "libfwk",
960 nocrt: true,
961 }
962 `)
963
964 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
965 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
966 cc_library {
967 name: "libvndk_sp",
968 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900969 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800970 vndk: {
971 enabled: true,
972 support_system_process: true,
973 },
974 shared_libs: ["libvendor"], // Cause error
975 nocrt: true,
976 }
977
978 cc_library {
979 name: "libvendor",
980 vendor: true,
981 nocrt: true,
982 }
983 `)
984
985 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
986 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
987 cc_library {
988 name: "libvndk_sp",
989 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900990 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800991 vndk: {
992 enabled: true,
993 support_system_process: true,
994 },
995 shared_libs: ["libvndk"], // Cause error
996 nocrt: true,
997 }
998
999 cc_library {
1000 name: "libvndk",
1001 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001002 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001003 vndk: {
1004 enabled: true,
1005 },
1006 nocrt: true,
1007 }
1008 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001009
1010 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1011 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1012 cc_library {
1013 name: "libvndk",
1014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001015 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001016 vndk: {
1017 enabled: true,
1018 },
1019 shared_libs: ["libnonvndk"],
1020 nocrt: true,
1021 }
1022
1023 cc_library {
1024 name: "libnonvndk",
1025 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001026 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001027 nocrt: true,
1028 }
1029 `)
1030
1031 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1032 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1033 cc_library {
1034 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001035 vendor_available: true,
1036 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001037 vndk: {
1038 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001039 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001040 },
1041 shared_libs: ["libnonvndk"],
1042 nocrt: true,
1043 }
1044
1045 cc_library {
1046 name: "libnonvndk",
1047 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001048 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001049 nocrt: true,
1050 }
1051 `)
1052
1053 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1054 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1055 cc_library {
1056 name: "libvndksp",
1057 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001058 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001059 vndk: {
1060 enabled: true,
1061 support_system_process: true,
1062 },
1063 shared_libs: ["libnonvndk"],
1064 nocrt: true,
1065 }
1066
1067 cc_library {
1068 name: "libnonvndk",
1069 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001070 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001071 nocrt: true,
1072 }
1073 `)
1074
1075 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1076 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1077 cc_library {
1078 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001079 vendor_available: true,
1080 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001081 vndk: {
1082 enabled: true,
1083 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001084 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001085 },
1086 shared_libs: ["libnonvndk"],
1087 nocrt: true,
1088 }
1089
1090 cc_library {
1091 name: "libnonvndk",
1092 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001093 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001094 nocrt: true,
1095 }
1096 `)
1097}
1098
1099func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001100 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001101 // okay to link : LLNDK -> double_loadable VNDK
1102 testCc(t, `
1103 cc_library {
1104 name: "libllndk",
1105 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001106 llndk: {
1107 symbol_file: "libllndk.map.txt",
1108 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001109 }
1110
1111 cc_library {
1112 name: "libdoubleloadable",
1113 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001114 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001115 vndk: {
1116 enabled: true,
1117 },
1118 double_loadable: true,
1119 }
1120 `)
1121 // okay to link : LLNDK -> VNDK-SP
1122 testCc(t, `
1123 cc_library {
1124 name: "libllndk",
1125 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001126 llndk: {
1127 symbol_file: "libllndk.map.txt",
1128 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001129 }
1130
1131 cc_library {
1132 name: "libvndksp",
1133 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001134 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001135 vndk: {
1136 enabled: true,
1137 support_system_process: true,
1138 },
1139 }
1140 `)
1141 // okay to link : double_loadable -> double_loadable
1142 testCc(t, `
1143 cc_library {
1144 name: "libdoubleloadable1",
1145 shared_libs: ["libdoubleloadable2"],
1146 vendor_available: true,
1147 double_loadable: true,
1148 }
1149
1150 cc_library {
1151 name: "libdoubleloadable2",
1152 vendor_available: true,
1153 double_loadable: true,
1154 }
1155 `)
1156 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1157 testCc(t, `
1158 cc_library {
1159 name: "libdoubleloadable",
1160 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001161 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001162 vndk: {
1163 enabled: true,
1164 },
1165 double_loadable: true,
1166 shared_libs: ["libnondoubleloadable"],
1167 }
1168
1169 cc_library {
1170 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001171 vendor_available: true,
1172 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001173 vndk: {
1174 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001175 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001176 },
1177 double_loadable: true,
1178 }
1179 `)
1180 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1181 testCc(t, `
1182 cc_library {
1183 name: "libllndk",
1184 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001185 llndk: {
1186 symbol_file: "libllndk.map.txt",
1187 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001188 }
1189
1190 cc_library {
1191 name: "libcoreonly",
1192 shared_libs: ["libvendoravailable"],
1193 }
1194
1195 // indirect dependency of LLNDK
1196 cc_library {
1197 name: "libvendoravailable",
1198 vendor_available: true,
1199 double_loadable: true,
1200 }
1201 `)
1202}
1203
1204func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001205 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001206 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1207 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1208 cc_library {
1209 name: "libllndk",
1210 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001211 llndk: {
1212 symbol_file: "libllndk.map.txt",
1213 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001214 }
1215
1216 cc_library {
1217 name: "libnondoubleloadable",
1218 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001219 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001220 vndk: {
1221 enabled: true,
1222 },
1223 }
1224 `)
1225
1226 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1227 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1228 cc_library {
1229 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001230 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001231 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001232 llndk: {
1233 symbol_file: "libllndk.map.txt",
1234 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001235 }
1236
1237 cc_library {
1238 name: "libnondoubleloadable",
1239 vendor_available: true,
1240 }
1241 `)
1242
Jooyung Hana70f0672019-01-18 15:20:43 +09001243 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1244 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1245 cc_library {
1246 name: "libllndk",
1247 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001248 llndk: {
1249 symbol_file: "libllndk.map.txt",
1250 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001251 }
1252
1253 cc_library {
1254 name: "libcoreonly",
1255 shared_libs: ["libvendoravailable"],
1256 }
1257
1258 // indirect dependency of LLNDK
1259 cc_library {
1260 name: "libvendoravailable",
1261 vendor_available: true,
1262 }
1263 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001264
1265 // The error is not from 'client' but from 'libllndk'
1266 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1267 cc_library {
1268 name: "client",
1269 vendor_available: true,
1270 double_loadable: true,
1271 shared_libs: ["libllndk"],
1272 }
1273 cc_library {
1274 name: "libllndk",
1275 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001276 llndk: {
1277 symbol_file: "libllndk.map.txt",
1278 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001279 }
1280 cc_library {
1281 name: "libnondoubleloadable",
1282 vendor_available: true,
1283 }
1284 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001285}
1286
Jooyung Han479ca172020-10-19 18:51:07 +09001287func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001288 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001289 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1290 cc_library {
1291 name: "libvndksp",
1292 shared_libs: ["libanothervndksp"],
1293 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001294 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001295 vndk: {
1296 enabled: true,
1297 support_system_process: true,
1298 }
1299 }
1300
1301 cc_library {
1302 name: "libllndk",
1303 shared_libs: ["libanothervndksp"],
1304 }
1305
Jooyung Han479ca172020-10-19 18:51:07 +09001306 cc_library {
1307 name: "libanothervndksp",
1308 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001309 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001310 }
1311 `)
1312}
1313
Logan Chienf3511742017-10-31 18:04:35 +08001314func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001315 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001316 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001317 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001318 cc_library {
1319 name: "libvndk",
1320 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001321 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001322 vndk: {
1323 enabled: true,
1324 },
1325 nocrt: true,
1326 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001327 cc_library {
1328 name: "libvndk2",
1329 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001330 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001331 vndk: {
1332 enabled: true,
1333 },
1334 target: {
1335 vendor: {
1336 suffix: "-suffix",
1337 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001338 product: {
1339 suffix: "-suffix",
1340 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001341 },
1342 nocrt: true,
1343 }
Logan Chienf3511742017-10-31 18:04:35 +08001344
1345 cc_library {
1346 name: "libvndk_ext",
1347 vendor: true,
1348 vndk: {
1349 enabled: true,
1350 extends: "libvndk",
1351 },
1352 nocrt: true,
1353 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001354
1355 cc_library {
1356 name: "libvndk2_ext",
1357 vendor: true,
1358 vndk: {
1359 enabled: true,
1360 extends: "libvndk2",
1361 },
1362 nocrt: true,
1363 }
Logan Chienf3511742017-10-31 18:04:35 +08001364
Justin Yun0ecf0b22020-02-28 15:07:59 +09001365 cc_library {
1366 name: "libvndk_ext_product",
1367 product_specific: true,
1368 vndk: {
1369 enabled: true,
1370 extends: "libvndk",
1371 },
1372 nocrt: true,
1373 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001374
Justin Yun0ecf0b22020-02-28 15:07:59 +09001375 cc_library {
1376 name: "libvndk2_ext_product",
1377 product_specific: true,
1378 vndk: {
1379 enabled: true,
1380 extends: "libvndk2",
1381 },
1382 nocrt: true,
1383 }
1384 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001385 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001386 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001387 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001388
1389 ctx := testCcWithConfig(t, config)
1390
1391 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1392 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1393
1394 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1395 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1396
1397 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1398 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001399}
1400
Logan Chienf3511742017-10-31 18:04:35 +08001401func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001402 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001403 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001404 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001405 cc_library {
1406 name: "libvndk",
1407 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001408 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001409 vndk: {
1410 enabled: true,
1411 },
1412 nocrt: true,
1413 }
1414
1415 cc_library {
1416 name: "libvndk_ext",
1417 vndk: {
1418 enabled: true,
1419 extends: "libvndk",
1420 },
1421 nocrt: true,
1422 }
1423 `)
1424
1425 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1426 cc_library {
1427 name: "libvndk",
1428 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001429 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001430 vndk: {
1431 enabled: true,
1432 },
1433 nocrt: true,
1434 }
1435
1436 cc_library {
1437 name: "libvndk_ext",
1438 vendor: true,
1439 vndk: {
1440 enabled: true,
1441 },
1442 nocrt: true,
1443 }
1444 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001445
1446 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1447 cc_library {
1448 name: "libvndk",
1449 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001450 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001451 vndk: {
1452 enabled: true,
1453 },
1454 nocrt: true,
1455 }
1456
1457 cc_library {
1458 name: "libvndk_ext_product",
1459 product_specific: true,
1460 vndk: {
1461 enabled: true,
1462 },
1463 nocrt: true,
1464 }
1465 `)
1466
1467 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1468 cc_library {
1469 name: "libvndk",
1470 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001471 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001472 vndk: {
1473 enabled: true,
1474 },
1475 nocrt: true,
1476 }
1477
1478 cc_library {
1479 name: "libvndk_ext_product",
1480 product_specific: true,
1481 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001482 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001483 vndk: {
1484 enabled: true,
1485 extends: "libvndk",
1486 },
1487 nocrt: true,
1488 }
1489 `)
Logan Chienf3511742017-10-31 18:04:35 +08001490}
1491
1492func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001493 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001494 // This test ensures an error is emitted for inconsistent support_system_process.
1495 testCcError(t, "module \".*\" with mismatched support_system_process", `
1496 cc_library {
1497 name: "libvndk",
1498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001499 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001500 vndk: {
1501 enabled: true,
1502 },
1503 nocrt: true,
1504 }
1505
1506 cc_library {
1507 name: "libvndk_sp_ext",
1508 vendor: true,
1509 vndk: {
1510 enabled: true,
1511 extends: "libvndk",
1512 support_system_process: true,
1513 },
1514 nocrt: true,
1515 }
1516 `)
1517
1518 testCcError(t, "module \".*\" with mismatched support_system_process", `
1519 cc_library {
1520 name: "libvndk_sp",
1521 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001522 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001523 vndk: {
1524 enabled: true,
1525 support_system_process: true,
1526 },
1527 nocrt: true,
1528 }
1529
1530 cc_library {
1531 name: "libvndk_ext",
1532 vendor: true,
1533 vndk: {
1534 enabled: true,
1535 extends: "libvndk_sp",
1536 },
1537 nocrt: true,
1538 }
1539 `)
1540}
1541
1542func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001543 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001544 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001545 // with `private: true`.
1546 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001547 cc_library {
1548 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001549 vendor_available: true,
1550 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001551 vndk: {
1552 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001553 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001554 },
1555 nocrt: true,
1556 }
1557
1558 cc_library {
1559 name: "libvndk_ext",
1560 vendor: true,
1561 vndk: {
1562 enabled: true,
1563 extends: "libvndk",
1564 },
1565 nocrt: true,
1566 }
1567 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001568
Justin Yunfd9e8042020-12-23 18:23:14 +09001569 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001570 cc_library {
1571 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001572 vendor_available: true,
1573 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001574 vndk: {
1575 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001576 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001577 },
1578 nocrt: true,
1579 }
1580
1581 cc_library {
1582 name: "libvndk_ext_product",
1583 product_specific: true,
1584 vndk: {
1585 enabled: true,
1586 extends: "libvndk",
1587 },
1588 nocrt: true,
1589 }
1590 `)
Logan Chienf3511742017-10-31 18:04:35 +08001591}
1592
Logan Chiend3c59a22018-03-29 14:08:15 +08001593func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001594 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001595 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001596 testCc(t, `
1597 cc_library {
1598 name: "libvndk",
1599 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001600 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001601 vndk: {
1602 enabled: true,
1603 },
1604 nocrt: true,
1605 }
1606
1607 cc_library {
1608 name: "libvndk_ext",
1609 vendor: true,
1610 vndk: {
1611 enabled: true,
1612 extends: "libvndk",
1613 },
1614 nocrt: true,
1615 }
1616
1617 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001618 name: "libvndk_sp",
1619 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001620 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001621 vndk: {
1622 enabled: true,
1623 support_system_process: true,
1624 },
1625 nocrt: true,
1626 }
1627
1628 cc_library {
1629 name: "libvndk_sp_ext",
1630 vendor: true,
1631 vndk: {
1632 enabled: true,
1633 extends: "libvndk_sp",
1634 support_system_process: true,
1635 },
1636 nocrt: true,
1637 }
1638
1639 cc_library {
1640 name: "libvendor",
1641 vendor: true,
1642 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1643 nocrt: true,
1644 }
1645 `)
1646}
1647
Logan Chiend3c59a22018-03-29 14:08:15 +08001648func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001649 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001650 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001651 testCc(t, `
1652 cc_library {
1653 name: "libvndk",
1654 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001655 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001656 vndk: {
1657 enabled: true,
1658 },
1659 nocrt: true,
1660 }
1661
1662 cc_library {
1663 name: "libvndk_ext",
1664 vendor: true,
1665 vndk: {
1666 enabled: true,
1667 extends: "libvndk",
1668 },
1669 shared_libs: ["libvendor"],
1670 nocrt: true,
1671 }
1672
1673 cc_library {
1674 name: "libvendor",
1675 vendor: true,
1676 nocrt: true,
1677 }
1678 `)
Logan Chienf3511742017-10-31 18:04:35 +08001679
Logan Chiend3c59a22018-03-29 14:08:15 +08001680 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1681 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001682 cc_library {
1683 name: "libvndk_sp",
1684 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001685 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001686 vndk: {
1687 enabled: true,
1688 support_system_process: true,
1689 },
1690 nocrt: true,
1691 }
1692
1693 cc_library {
1694 name: "libvndk_sp_ext",
1695 vendor: true,
1696 vndk: {
1697 enabled: true,
1698 extends: "libvndk_sp",
1699 support_system_process: true,
1700 },
1701 shared_libs: ["libvendor"], // Cause an error
1702 nocrt: true,
1703 }
1704
1705 cc_library {
1706 name: "libvendor",
1707 vendor: true,
1708 nocrt: true,
1709 }
1710 `)
1711}
1712
Justin Yun0ecf0b22020-02-28 15:07:59 +09001713func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001714 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001715 bp := `
1716 cc_library {
1717 name: "libvndk",
1718 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001719 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001720 vndk: {
1721 enabled: true,
1722 },
1723 nocrt: true,
1724 }
1725
1726 cc_library {
1727 name: "libvndk_ext_product",
1728 product_specific: true,
1729 vndk: {
1730 enabled: true,
1731 extends: "libvndk",
1732 },
1733 shared_libs: ["libproduct_for_vndklibs"],
1734 nocrt: true,
1735 }
1736
1737 cc_library {
1738 name: "libvndk_sp",
1739 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001740 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001741 vndk: {
1742 enabled: true,
1743 support_system_process: true,
1744 },
1745 nocrt: true,
1746 }
1747
1748 cc_library {
1749 name: "libvndk_sp_ext_product",
1750 product_specific: true,
1751 vndk: {
1752 enabled: true,
1753 extends: "libvndk_sp",
1754 support_system_process: true,
1755 },
1756 shared_libs: ["libproduct_for_vndklibs"],
1757 nocrt: true,
1758 }
1759
1760 cc_library {
1761 name: "libproduct",
1762 product_specific: true,
1763 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1764 nocrt: true,
1765 }
1766
1767 cc_library {
1768 name: "libproduct_for_vndklibs",
1769 product_specific: true,
1770 nocrt: true,
1771 }
1772 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001773 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001774 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001775 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001776
1777 testCcWithConfig(t, config)
1778}
1779
Logan Chiend3c59a22018-03-29 14:08:15 +08001780func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001781 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001782 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1783 // library.
1784 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1785 cc_library {
1786 name: "libvndk",
1787 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001788 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001789 vndk: {
1790 enabled: true,
1791 },
1792 nocrt: true,
1793 }
1794
1795 cc_library {
1796 name: "libvndk_sp",
1797 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001798 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001799 vndk: {
1800 enabled: true,
1801 support_system_process: true,
1802 },
1803 nocrt: true,
1804 }
1805
1806 cc_library {
1807 name: "libvndk_sp_ext",
1808 vendor: true,
1809 vndk: {
1810 enabled: true,
1811 extends: "libvndk_sp",
1812 support_system_process: true,
1813 },
1814 shared_libs: ["libvndk"], // Cause an error
1815 nocrt: true,
1816 }
1817 `)
1818
1819 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1820 // library.
1821 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1822 cc_library {
1823 name: "libvndk",
1824 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001825 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001826 vndk: {
1827 enabled: true,
1828 },
1829 nocrt: true,
1830 }
1831
1832 cc_library {
1833 name: "libvndk_ext",
1834 vendor: true,
1835 vndk: {
1836 enabled: true,
1837 extends: "libvndk",
1838 },
1839 nocrt: true,
1840 }
1841
1842 cc_library {
1843 name: "libvndk_sp",
1844 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001845 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001846 vndk: {
1847 enabled: true,
1848 support_system_process: true,
1849 },
1850 nocrt: true,
1851 }
1852
1853 cc_library {
1854 name: "libvndk_sp_ext",
1855 vendor: true,
1856 vndk: {
1857 enabled: true,
1858 extends: "libvndk_sp",
1859 support_system_process: true,
1860 },
1861 shared_libs: ["libvndk_ext"], // Cause an error
1862 nocrt: true,
1863 }
1864 `)
1865}
1866
1867func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001868 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001869 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1870 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001871 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1872 cc_library {
1873 name: "libvndk",
1874 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001875 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001876 vndk: {
1877 enabled: true,
1878 },
1879 nocrt: true,
1880 }
1881
1882 cc_library {
1883 name: "libvndk_ext",
1884 vendor: true,
1885 vndk: {
1886 enabled: true,
1887 extends: "libvndk",
1888 },
1889 nocrt: true,
1890 }
1891
1892 cc_library {
1893 name: "libvndk2",
1894 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001895 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001896 vndk: {
1897 enabled: true,
1898 },
1899 shared_libs: ["libvndk_ext"],
1900 nocrt: true,
1901 }
1902 `)
1903
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001904 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001905 cc_library {
1906 name: "libvndk",
1907 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001908 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001909 vndk: {
1910 enabled: true,
1911 },
1912 nocrt: true,
1913 }
1914
1915 cc_library {
1916 name: "libvndk_ext",
1917 vendor: true,
1918 vndk: {
1919 enabled: true,
1920 extends: "libvndk",
1921 },
1922 nocrt: true,
1923 }
1924
1925 cc_library {
1926 name: "libvndk2",
1927 vendor_available: true,
1928 vndk: {
1929 enabled: true,
1930 },
1931 target: {
1932 vendor: {
1933 shared_libs: ["libvndk_ext"],
1934 },
1935 },
1936 nocrt: true,
1937 }
1938 `)
1939
1940 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1941 cc_library {
1942 name: "libvndk_sp",
1943 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001944 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001945 vndk: {
1946 enabled: true,
1947 support_system_process: true,
1948 },
1949 nocrt: true,
1950 }
1951
1952 cc_library {
1953 name: "libvndk_sp_ext",
1954 vendor: true,
1955 vndk: {
1956 enabled: true,
1957 extends: "libvndk_sp",
1958 support_system_process: true,
1959 },
1960 nocrt: true,
1961 }
1962
1963 cc_library {
1964 name: "libvndk_sp_2",
1965 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001966 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001967 vndk: {
1968 enabled: true,
1969 support_system_process: true,
1970 },
1971 shared_libs: ["libvndk_sp_ext"],
1972 nocrt: true,
1973 }
1974 `)
1975
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001976 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001977 cc_library {
1978 name: "libvndk_sp",
1979 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001980 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001981 vndk: {
1982 enabled: true,
1983 },
1984 nocrt: true,
1985 }
1986
1987 cc_library {
1988 name: "libvndk_sp_ext",
1989 vendor: true,
1990 vndk: {
1991 enabled: true,
1992 extends: "libvndk_sp",
1993 },
1994 nocrt: true,
1995 }
1996
1997 cc_library {
1998 name: "libvndk_sp2",
1999 vendor_available: true,
2000 vndk: {
2001 enabled: true,
2002 },
2003 target: {
2004 vendor: {
2005 shared_libs: ["libvndk_sp_ext"],
2006 },
2007 },
2008 nocrt: true,
2009 }
2010 `)
2011}
2012
Justin Yun5f7f7e82019-11-18 19:52:14 +09002013func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002014 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002015 bp := `
2016 cc_library {
2017 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002018 llndk: {
2019 symbol_file: "libllndk.map.txt",
2020 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002021 }
2022 cc_library {
2023 name: "libvndk",
2024 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002025 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002026 vndk: {
2027 enabled: true,
2028 },
2029 nocrt: true,
2030 }
2031 cc_library {
2032 name: "libvndk_sp",
2033 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002034 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002035 vndk: {
2036 enabled: true,
2037 support_system_process: true,
2038 },
2039 nocrt: true,
2040 }
2041 cc_library {
2042 name: "libva",
2043 vendor_available: true,
2044 nocrt: true,
2045 }
2046 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002047 name: "libpa",
2048 product_available: true,
2049 nocrt: true,
2050 }
2051 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002052 name: "libboth_available",
2053 vendor_available: true,
2054 product_available: true,
2055 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002056 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002057 target: {
2058 vendor: {
2059 suffix: "-vendor",
2060 },
2061 product: {
2062 suffix: "-product",
2063 },
2064 }
2065 }
2066 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002067 name: "libproduct_va",
2068 product_specific: true,
2069 vendor_available: true,
2070 nocrt: true,
2071 }
2072 cc_library {
2073 name: "libprod",
2074 product_specific: true,
2075 shared_libs: [
2076 "libllndk",
2077 "libvndk",
2078 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002079 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002080 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002081 "libproduct_va",
2082 ],
2083 nocrt: true,
2084 }
2085 cc_library {
2086 name: "libvendor",
2087 vendor: true,
2088 shared_libs: [
2089 "libllndk",
2090 "libvndk",
2091 "libvndk_sp",
2092 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002093 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002094 "libproduct_va",
2095 ],
2096 nocrt: true,
2097 }
2098 `
2099
Paul Duffin8567f222021-03-23 00:02:06 +00002100 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002101
Jooyung Han261e1582020-10-20 18:54:21 +09002102 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2103 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002104
2105 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2106 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2107
2108 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2109 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002110
2111 ensureStringContains := func(t *testing.T, str string, substr string) {
2112 t.Helper()
2113 if !strings.Contains(str, substr) {
2114 t.Errorf("%q is not found in %v", substr, str)
2115 }
2116 }
2117 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2118 t.Helper()
2119 if strings.Contains(str, substr) {
2120 t.Errorf("%q is found in %v", substr, str)
2121 }
2122 }
2123
2124 // _static variant is used since _shared reuses *.o from the static variant
2125 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2126 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2127
2128 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2129 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2130 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2131 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002132 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun13decfb2021-03-08 19:25:55 +09002133
2134 product_cflags := product_static.Rule("cc").Args["cFlags"]
2135 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2136 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2137 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002138 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR_API__=202404")
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 Cross5a377182023-12-14 14:46:23 -08002557 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2558 actual := android.Paths(staticLibInfo.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 Cross5a377182023-12-14 14:46:23 -08002593 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2594 actual := android.Paths(staticLibInfo.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,
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09002653 product_available: true,
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002654 }
2655 cc_library_headers {
2656 name: "libexternal_llndk_headers",
2657 export_include_dirs: ["include_llndk"],
2658 llndk: {
2659 symbol_file: "libllndk.map.txt",
2660 },
2661 vendor_available: true,
2662 }
2663
2664 cc_library {
2665 name: "libllndk_with_override_headers",
2666 stubs: { versions: ["1", "2"] },
2667 llndk: {
2668 symbol_file: "libllndk.map.txt",
2669 override_export_include_dirs: ["include_llndk"],
2670 },
2671 export_include_dirs: ["include"],
2672 }
2673 `)
2674 actual := result.ModuleVariantsForTests("libllndk")
2675 for i := 0; i < len(actual); i++ {
2676 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2677 actual = append(actual[:i], actual[i+1:]...)
2678 i--
2679 }
2680 }
2681 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002682 "android_vendor.29_arm64_armv8-a_shared_current",
2683 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002684 "android_vendor.29_arm_armv7-a-neon_shared_current",
2685 "android_vendor.29_arm_armv7-a-neon_shared",
2686 }
2687 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2688
2689 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2690 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2691
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002692 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2693 t.Helper()
2694 m := result.ModuleForTests(module, variant).Module()
Colin Cross5a377182023-12-14 14:46:23 -08002695 f, _ := android.SingletonModuleProvider(result, m, FlagExporterInfoProvider)
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002696 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2697 expectedDirs, f.IncludeDirs)
2698 }
2699
2700 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2701 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2702 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2703 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2704 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2705 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2706}
2707
Jiyong Parka46a4d52017-12-14 19:54:34 +09002708func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002709 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002711 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002712 name: "libllndk_headers",
2713 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002714 llndk: {
2715 llndk_headers: true,
2716 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002717 }
2718 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002719 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002720 llndk: {
2721 symbol_file: "libllndk.map.txt",
2722 export_llndk_headers: ["libllndk_headers"],
2723 }
Colin Cross0477b422020-10-13 18:43:54 -07002724 }
2725
2726 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002727 name: "libvendor",
2728 shared_libs: ["libllndk"],
2729 vendor: true,
2730 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002731 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002732 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002733 }
2734 `)
2735
2736 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002737 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002738 cflags := cc.Args["cFlags"]
2739 if !strings.Contains(cflags, "-Imy_include") {
2740 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2741 }
2742}
2743
Logan Chien43d34c32017-12-20 01:17:32 +08002744func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2745 actual := module.Properties.AndroidMkRuntimeLibs
2746 if !reflect.DeepEqual(actual, expected) {
2747 t.Errorf("incorrect runtime_libs for shared libs"+
2748 "\nactual: %v"+
2749 "\nexpected: %v",
2750 actual,
2751 expected,
2752 )
2753 }
2754}
2755
2756const runtimeLibAndroidBp = `
2757 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002758 name: "liball_available",
2759 vendor_available: true,
2760 product_available: true,
2761 no_libcrt : true,
2762 nocrt : true,
2763 system_shared_libs : [],
2764 }
2765 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002766 name: "libvendor_available1",
2767 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002768 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002769 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002770 nocrt : true,
2771 system_shared_libs : [],
2772 }
2773 cc_library {
2774 name: "libvendor_available2",
2775 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002776 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002777 target: {
2778 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002779 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002780 }
2781 },
Yi Konge7fe9912019-06-02 00:53:50 -07002782 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002783 nocrt : true,
2784 system_shared_libs : [],
2785 }
2786 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002787 name: "libproduct_vendor",
2788 product_specific: true,
2789 vendor_available: true,
2790 no_libcrt : true,
2791 nocrt : true,
2792 system_shared_libs : [],
2793 }
2794 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002795 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002796 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002797 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002798 nocrt : true,
2799 system_shared_libs : [],
2800 }
2801 cc_library {
2802 name: "libvendor1",
2803 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002804 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002805 nocrt : true,
2806 system_shared_libs : [],
2807 }
2808 cc_library {
2809 name: "libvendor2",
2810 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002811 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002812 no_libcrt : true,
2813 nocrt : true,
2814 system_shared_libs : [],
2815 }
2816 cc_library {
2817 name: "libproduct_available1",
2818 product_available: true,
2819 runtime_libs: ["liball_available"],
2820 no_libcrt : true,
2821 nocrt : true,
2822 system_shared_libs : [],
2823 }
2824 cc_library {
2825 name: "libproduct1",
2826 product_specific: true,
2827 no_libcrt : true,
2828 nocrt : true,
2829 system_shared_libs : [],
2830 }
2831 cc_library {
2832 name: "libproduct2",
2833 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002834 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002835 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002836 nocrt : true,
2837 system_shared_libs : [],
2838 }
2839`
2840
2841func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002842 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002843 ctx := testCc(t, runtimeLibAndroidBp)
2844
2845 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002846 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002847
Justin Yun8a2600c2020-12-07 12:44:03 +09002848 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2849 checkRuntimeLibs(t, []string{"liball_available"}, module)
2850
2851 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2852 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002853
2854 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002855 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002856
2857 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2858 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002859 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002860
Justin Yun8a2600c2020-12-07 12:44:03 +09002861 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2862 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002863
2864 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002865 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002866
2867 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2868 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002869 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002870
2871 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2872 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2873
2874 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002875 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002876}
2877
2878func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002879 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002880 ctx := testCc(t, runtimeLibAndroidBp)
2881
Colin Cross7113d202019-11-20 16:39:12 -08002882 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002883 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2884 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002885
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002886 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002887 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002888 checkRuntimeLibs(t, nil, module)
2889}
2890
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002891func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002892 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002893 actual := module.Properties.AndroidMkStaticLibs
2894 if !reflect.DeepEqual(actual, expected) {
2895 t.Errorf("incorrect static_libs"+
2896 "\nactual: %v"+
2897 "\nexpected: %v",
2898 actual,
2899 expected,
2900 )
2901 }
2902}
2903
2904const staticLibAndroidBp = `
2905 cc_library {
2906 name: "lib1",
2907 }
2908 cc_library {
2909 name: "lib2",
2910 static_libs: ["lib1"],
2911 }
2912`
2913
2914func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002915 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002916 ctx := testCc(t, staticLibAndroidBp)
2917
2918 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002919 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002920 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002921 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002922
2923 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002924 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002925 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2926 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002927 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002928}
2929
Jiyong Parkd08b6972017-09-26 10:50:54 +09002930var compilerFlagsTestCases = []struct {
2931 in string
2932 out bool
2933}{
2934 {
2935 in: "a",
2936 out: false,
2937 },
2938 {
2939 in: "-a",
2940 out: true,
2941 },
2942 {
2943 in: "-Ipath/to/something",
2944 out: false,
2945 },
2946 {
2947 in: "-isystempath/to/something",
2948 out: false,
2949 },
2950 {
2951 in: "--coverage",
2952 out: false,
2953 },
2954 {
2955 in: "-include a/b",
2956 out: true,
2957 },
2958 {
2959 in: "-include a/b c/d",
2960 out: false,
2961 },
2962 {
2963 in: "-DMACRO",
2964 out: true,
2965 },
2966 {
2967 in: "-DMAC RO",
2968 out: false,
2969 },
2970 {
2971 in: "-a -b",
2972 out: false,
2973 },
2974 {
2975 in: "-DMACRO=definition",
2976 out: true,
2977 },
2978 {
2979 in: "-DMACRO=defi nition",
2980 out: true, // TODO(jiyong): this should be false
2981 },
2982 {
2983 in: "-DMACRO(x)=x + 1",
2984 out: true,
2985 },
2986 {
2987 in: "-DMACRO=\"defi nition\"",
2988 out: true,
2989 },
2990}
2991
2992type mockContext struct {
2993 BaseModuleContext
2994 result bool
2995}
2996
2997func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2998 // CheckBadCompilerFlags calls this function when the flag should be rejected
2999 ctx.result = false
3000}
3001
3002func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003003 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003004 for _, testCase := range compilerFlagsTestCases {
3005 ctx := &mockContext{result: true}
3006 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3007 if ctx.result != testCase.out {
3008 t.Errorf("incorrect output:")
3009 t.Errorf(" input: %#v", testCase.in)
3010 t.Errorf(" expected: %#v", testCase.out)
3011 t.Errorf(" got: %#v", ctx.result)
3012 }
3013 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003014}
Jiyong Park374510b2018-03-19 18:23:01 +09003015
Jiyong Park37b25202018-07-11 10:49:27 +09003016func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003017 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003018 ctx := testCc(t, `
3019 cc_library_shared {
3020 name: "librecovery",
3021 recovery: true,
3022 }
3023 cc_library_shared {
3024 name: "librecovery32",
3025 recovery: true,
3026 compile_multilib:"32",
3027 }
Jiyong Park5baac542018-08-28 09:55:37 +09003028 cc_library_shared {
3029 name: "libHalInRecovery",
3030 recovery_available: true,
3031 vendor: true,
3032 }
Jiyong Park37b25202018-07-11 10:49:27 +09003033 `)
3034
3035 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003036 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003037 if len(variants) != 1 || !android.InList(arm64, variants) {
3038 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3039 }
3040
3041 variants = ctx.ModuleVariantsForTests("librecovery32")
3042 if android.InList(arm64, variants) {
3043 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3044 }
Jiyong Park5baac542018-08-28 09:55:37 +09003045
3046 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3047 if !recoveryModule.Platform() {
3048 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3049 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003050}
Jiyong Park5baac542018-08-28 09:55:37 +09003051
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003052func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003053 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003054 bp := `
3055 cc_prebuilt_test_library_shared {
3056 name: "test_lib",
3057 relative_install_path: "foo/bar/baz",
3058 srcs: ["srcpath/dontusethispath/baz.so"],
3059 }
3060
3061 cc_test {
3062 name: "main_test",
3063 data_libs: ["test_lib"],
3064 gtest: false,
3065 }
3066 `
3067
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003068 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003069 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003070 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003071 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3072
3073 ctx := testCcWithConfig(t, config)
3074 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3075 testBinary := module.(*Module).linker.(*testBinary)
3076 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3077 if err != nil {
3078 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3079 }
3080 if len(outputFiles) != 1 {
3081 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3082 }
3083 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003084 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003085 }
3086
3087 outputPath := outputFiles[0].String()
3088
3089 if !strings.HasSuffix(outputPath, "/main_test") {
3090 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3091 }
Colin Crossaa255532020-07-03 13:18:24 -07003092 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003093 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3094 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3095 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3096 }
3097}
3098
Jiyong Park7ed9de32018-10-15 22:25:07 +09003099func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003100 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003101 ctx := testCc(t, `
3102 cc_library_shared {
3103 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003104 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003105 stubs: {
3106 symbol_file: "foo.map.txt",
3107 versions: ["1", "2", "3"],
3108 },
3109 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003110
Jiyong Park7ed9de32018-10-15 22:25:07 +09003111 cc_library_shared {
3112 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003113 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003114 shared_libs: ["libFoo#1"],
3115 }`)
3116
3117 variants := ctx.ModuleVariantsForTests("libFoo")
3118 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003119 "android_arm64_armv8-a_shared",
3120 "android_arm64_armv8-a_shared_1",
3121 "android_arm64_armv8-a_shared_2",
3122 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003123 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003124 "android_arm_armv7-a-neon_shared",
3125 "android_arm_armv7-a-neon_shared_1",
3126 "android_arm_armv7-a-neon_shared_2",
3127 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003128 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003129 }
3130 variantsMismatch := false
3131 if len(variants) != len(expectedVariants) {
3132 variantsMismatch = true
3133 } else {
3134 for _, v := range expectedVariants {
3135 if !inList(v, variants) {
3136 variantsMismatch = false
3137 }
3138 }
3139 }
3140 if variantsMismatch {
3141 t.Errorf("variants of libFoo expected:\n")
3142 for _, v := range expectedVariants {
3143 t.Errorf("%q\n", v)
3144 }
3145 t.Errorf(", but got:\n")
3146 for _, v := range variants {
3147 t.Errorf("%q\n", v)
3148 }
3149 }
3150
Colin Cross7113d202019-11-20 16:39:12 -08003151 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003152 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003153 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003154 if !strings.Contains(libFlags, libFoo1StubPath) {
3155 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3156 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003157
Colin Cross7113d202019-11-20 16:39:12 -08003158 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003159 cFlags := libBarCompileRule.Args["cFlags"]
3160 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3161 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3162 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3163 }
Jiyong Park37b25202018-07-11 10:49:27 +09003164}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003165
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003166func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3167 t.Parallel()
3168 ctx := testCc(t, `
3169 cc_library_shared {
3170 name: "libFoo",
3171 srcs: ["foo.c"],
3172 stubs: {
3173 symbol_file: "foo.map.txt",
3174 versions: ["current"],
3175 },
3176 apex_available: ["bar", "a1"],
3177 }
3178
3179 cc_library_shared {
3180 name: "libBar",
3181 srcs: ["bar.c"],
3182 shared_libs: ["libFoo"],
3183 apex_available: ["a1"],
3184 }
3185
3186 cc_library_shared {
3187 name: "libA1",
3188 srcs: ["a1.c"],
3189 shared_libs: ["libFoo"],
3190 apex_available: ["a1"],
3191 }
3192
3193 cc_library_shared {
3194 name: "libBarA1",
3195 srcs: ["bara1.c"],
3196 shared_libs: ["libFoo"],
3197 apex_available: ["bar", "a1"],
3198 }
3199
3200 cc_library_shared {
3201 name: "libAnyApex",
3202 srcs: ["anyApex.c"],
3203 shared_libs: ["libFoo"],
3204 apex_available: ["//apex_available:anyapex"],
3205 }
3206
3207 cc_library_shared {
3208 name: "libBaz",
3209 srcs: ["baz.c"],
3210 shared_libs: ["libFoo"],
3211 apex_available: ["baz"],
3212 }
3213
3214 cc_library_shared {
3215 name: "libQux",
3216 srcs: ["qux.c"],
3217 shared_libs: ["libFoo"],
3218 apex_available: ["qux", "bar"],
3219 }`)
3220
3221 variants := ctx.ModuleVariantsForTests("libFoo")
3222 expectedVariants := []string{
3223 "android_arm64_armv8-a_shared",
3224 "android_arm64_armv8-a_shared_current",
3225 "android_arm_armv7-a-neon_shared",
3226 "android_arm_armv7-a-neon_shared_current",
3227 }
3228 variantsMismatch := false
3229 if len(variants) != len(expectedVariants) {
3230 variantsMismatch = true
3231 } else {
3232 for _, v := range expectedVariants {
3233 if !inList(v, variants) {
3234 variantsMismatch = false
3235 }
3236 }
3237 }
3238 if variantsMismatch {
3239 t.Errorf("variants of libFoo expected:\n")
3240 for _, v := range expectedVariants {
3241 t.Errorf("%q\n", v)
3242 }
3243 t.Errorf(", but got:\n")
3244 for _, v := range variants {
3245 t.Errorf("%q\n", v)
3246 }
3247 }
3248
3249 linkAgainstFoo := []string{"libBarA1"}
3250 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3251
3252 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3253 for _, lib := range linkAgainstFoo {
3254 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3255 libFlags := libLinkRule.Args["libFlags"]
3256 if !strings.Contains(libFlags, libFooPath) {
3257 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3258 }
3259 }
3260
3261 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3262 for _, lib := range linkAgainstFooStubs {
3263 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3264 libFlags := libLinkRule.Args["libFlags"]
3265 if !strings.Contains(libFlags, libFooStubPath) {
3266 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3267 }
3268 }
3269}
3270
Jooyung Hanb04a4992020-03-13 18:57:35 +09003271func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003272 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003273 for _, tc := range []struct{ moduleName, expected string }{
3274 {"libc", "__LIBC_API__"},
3275 {"libfoo", "__LIBFOO_API__"},
3276 {"libfoo@1", "__LIBFOO_1_API__"},
3277 {"libfoo-v1", "__LIBFOO_V1_API__"},
3278 {"libfoo.v1", "__LIBFOO_V1_API__"},
3279 } {
3280 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3281 }
3282}
3283
Liz Kammer83cf81b2022-09-22 08:24:20 -04003284func pathsToBase(paths android.Paths) []string {
3285 var ret []string
3286 for _, p := range paths {
3287 ret = append(ret, p.Base())
3288 }
3289 return ret
3290}
3291
3292func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003293 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003294 ctx := testCc(t, `
3295 cc_library_static {
3296 name: "foo",
3297 srcs: ["foo.c"],
3298 }
3299
3300 cc_library_static {
3301 name: "bar",
3302 srcs: ["bar.c"],
3303 }
3304
3305 cc_library_shared {
3306 name: "qux",
3307 srcs: ["qux.c"],
3308 }
3309
3310 cc_library_static {
3311 name: "baz",
3312 srcs: ["baz.c"],
3313 static_libs: ["foo"],
3314 shared_libs: ["qux"],
3315 whole_static_libs: ["bar"],
3316 }`)
3317
3318 variant := "android_arm64_armv8-a_static"
3319 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3320
3321 // For static libraries, the object files of a whole static dep are included in the archive
3322 // directly
3323 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3324 t.Errorf("Expected input objects %q, got %q", w, g)
3325 }
3326
3327 // non whole static dependencies are not linked into the archive
3328 if len(arRule.Implicits) > 0 {
3329 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3330 }
3331}
3332
3333func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003334 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003335 ctx := testCc(t, `
3336 cc_library_static {
3337 name: "foo",
3338 srcs: ["foo.c"],
3339 }
3340
3341 cc_library_static {
3342 name: "bar",
3343 srcs: ["bar.c"],
3344 }
3345
3346 cc_library_shared {
3347 name: "qux",
3348 srcs: ["qux.c"],
3349 }
3350
3351 cc_library_shared {
3352 name: "baz",
3353 srcs: ["baz.c"],
3354 static_libs: ["foo"],
3355 shared_libs: ["qux"],
3356 whole_static_libs: ["bar"],
3357 }`)
3358
3359 variant := "android_arm64_armv8-a_shared"
3360 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3361 libFlags := linkRule.Args["libFlags"]
3362 // When dynamically linking, we expect static dependencies to be found on the command line
3363 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3364 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3365 }
3366 // When dynamically linking, we expect whole static dependencies to be found on the command line
3367 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3368 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3369 }
3370
3371 // When dynamically linking, we expect shared dependencies to be found on the command line
3372 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3373 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3374 }
3375
3376 // We should only have the objects from the shared library srcs, not the whole static dependencies
3377 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3378 t.Errorf("Expected input objects %q, got %q", w, g)
3379 }
3380}
3381
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003382func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003383 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003384 ctx := testCc(t, `
3385 cc_binary {
3386 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003387 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003388 static_executable: true,
3389 }`)
3390
Colin Cross7113d202019-11-20 16:39:12 -08003391 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003392 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3393 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003394 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003395 for _, lib := range systemStaticLibs {
3396 if !strings.Contains(libFlags, lib) {
3397 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3398 }
3399 }
3400 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3401 for _, lib := range systemSharedLibs {
3402 if strings.Contains(libFlags, lib) {
3403 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3404 }
3405 }
3406}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003407
3408func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003409 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003410 ctx := testCc(t, `
3411 cc_binary {
3412 name: "mybin",
3413 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003414 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003415 static_executable: true,
3416 stl: "none",
3417 }
3418
3419 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003420 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003421 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003422 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003423 stl: "none",
3424 }
3425
3426 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003427 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003428 srcs: ["foo.c"],
3429 stl: "none",
3430 stubs: {
3431 versions: ["1"],
3432 },
3433 }`)
3434
Colin Cross0de8a1e2020-09-18 14:15:30 -07003435 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3436 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003437 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003438
3439 if !reflect.DeepEqual(actual, expected) {
3440 t.Errorf("staticDeps orderings were not propagated correctly"+
3441 "\nactual: %v"+
3442 "\nexpected: %v",
3443 actual,
3444 expected,
3445 )
3446 }
3447}
Jooyung Han38002912019-05-16 04:01:54 +09003448
Jooyung Hand48f3c32019-08-23 11:18:57 +09003449func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003450 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003451 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3452 cc_library {
3453 name: "libA",
3454 srcs: ["foo.c"],
3455 shared_libs: ["libB"],
3456 stl: "none",
3457 }
3458
3459 cc_library {
3460 name: "libB",
3461 srcs: ["foo.c"],
3462 enabled: false,
3463 stl: "none",
3464 }
3465 `)
3466}
3467
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003468func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3469 bp := `
3470 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003471 name: "test_afl_fuzz_target",
3472 srcs: ["foo.c"],
3473 host_supported: true,
3474 static_libs: [
3475 "afl_fuzz_static_lib",
3476 ],
3477 shared_libs: [
3478 "afl_fuzz_shared_lib",
3479 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003480 fuzzing_frameworks: {
3481 afl: true,
3482 libfuzzer: false,
3483 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003484 }
3485 cc_library {
3486 name: "afl_fuzz_static_lib",
3487 host_supported: true,
3488 srcs: ["static_file.c"],
3489 }
3490 cc_library {
3491 name: "libfuzzer_only_static_lib",
3492 host_supported: true,
3493 srcs: ["static_file.c"],
3494 }
3495 cc_library {
3496 name: "afl_fuzz_shared_lib",
3497 host_supported: true,
3498 srcs: ["shared_file.c"],
3499 static_libs: [
3500 "second_static_lib",
3501 ],
3502 }
3503 cc_library_headers {
3504 name: "libafl_headers",
3505 vendor_available: true,
3506 host_supported: true,
3507 export_include_dirs: [
3508 "include",
3509 "instrumentation",
3510 ],
3511 }
3512 cc_object {
3513 name: "afl-compiler-rt",
3514 vendor_available: true,
3515 host_supported: true,
3516 cflags: [
3517 "-fPIC",
3518 ],
3519 srcs: [
3520 "instrumentation/afl-compiler-rt.o.c",
3521 ],
3522 }
3523 cc_library {
3524 name: "second_static_lib",
3525 host_supported: true,
3526 srcs: ["second_file.c"],
3527 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003528 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003529 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003530 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003531 srcs: [
3532 "aflpp_driver.c",
3533 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003534 }`
3535
3536 testEnv := map[string]string{
3537 "FUZZ_FRAMEWORK": "AFL",
3538 }
3539
3540 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003541
3542 checkPcGuardFlag := func(
3543 modName string, variantName string, shouldHave bool) {
3544 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3545
3546 cFlags, ok := cc.Args["cFlags"]
3547 if !ok {
3548 t.Errorf("Could not find cFlags for module %s and variant %s",
3549 modName, variantName)
3550 }
3551
3552 if strings.Contains(
3553 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3554 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3555 "Test failed for module %s and variant %s",
3556 !shouldHave, shouldHave, modName, variantName)
3557 }
3558 }
3559
Cory Barkera1da26f2022-06-07 20:12:06 +00003560 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003561 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003562
3563 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003564 checkPcGuardFlag(moduleName, variant+"_static", false)
3565 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003566
3567 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003568 checkPcGuardFlag(moduleName, variant+"_static", false)
3569 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003570
3571 ctx.ModuleForTests("afl_fuzz_shared_lib",
3572 "android_arm64_armv8-a_shared").Rule("cc")
3573 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003574 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3575}
3576
3577func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003578 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003579 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3580}
3581
3582func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003583 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003584 if runtime.GOOS != "linux" {
3585 t.Skip("requires linux")
3586 }
3587
3588 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003589}
3590
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003591// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3592// correctly.
3593func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003594 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003595 ctx := testCc(t, `
3596 cc_fuzz {
3597 name: "fuzz_smoke_test",
3598 srcs: ["foo.c"],
3599 }`)
3600
Paul Duffin075c4172019-12-19 19:06:13 +00003601 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003602 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3603}
3604
Jooyung Han38002912019-05-16 04:01:54 +09003605func assertString(t *testing.T, got, expected string) {
3606 t.Helper()
3607 if got != expected {
3608 t.Errorf("expected %q got %q", expected, got)
3609 }
3610}
3611
3612func assertArrayString(t *testing.T, got, expected []string) {
3613 t.Helper()
3614 if len(got) != len(expected) {
3615 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3616 return
3617 }
3618 for i := range got {
3619 if got[i] != expected[i] {
3620 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3621 i, expected[i], expected, got[i], got)
3622 return
3623 }
3624 }
3625}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003626
Jooyung Han0302a842019-10-30 18:43:49 +09003627func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3628 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08003629 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09003630}
3631
Colin Crosse1bb5d02019-09-24 14:55:04 -07003632func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003633 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003634 ctx := testCc(t, `
3635 cc_defaults {
3636 name: "defaults",
3637 srcs: ["foo.c"],
3638 static: {
3639 srcs: ["bar.c"],
3640 },
3641 shared: {
3642 srcs: ["baz.c"],
3643 },
3644 }
3645
3646 cc_library_static {
3647 name: "libstatic",
3648 defaults: ["defaults"],
3649 }
3650
3651 cc_library_shared {
3652 name: "libshared",
3653 defaults: ["defaults"],
3654 }
3655
3656 cc_library {
3657 name: "libboth",
3658 defaults: ["defaults"],
3659 }
3660
3661 cc_binary {
3662 name: "binary",
3663 defaults: ["defaults"],
3664 }`)
3665
Colin Cross7113d202019-11-20 16:39:12 -08003666 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003667 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3668 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3669 }
Colin Cross7113d202019-11-20 16:39:12 -08003670 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003671 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3672 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3673 }
Colin Cross7113d202019-11-20 16:39:12 -08003674 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003675 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3676 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3677 }
3678
Colin Cross7113d202019-11-20 16:39:12 -08003679 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003680 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3681 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3682 }
Colin Cross7113d202019-11-20 16:39:12 -08003683 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003684 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3685 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3686 }
3687}
Colin Crosseabaedd2020-02-06 17:01:55 -08003688
3689func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003690 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003691 bp := `
3692 cc_defaults {
3693 name: "libfoo_defaults",
3694 srcs: ["foo.c"],
3695 cppflags: ["-DFOO"],
3696 product_variables: {
3697 debuggable: {
3698 cppflags: ["-DBAR"],
3699 },
3700 },
3701 }
3702
3703 cc_library {
3704 name: "libfoo",
3705 defaults: ["libfoo_defaults"],
3706 }
3707 `
3708
Paul Duffin8567f222021-03-23 00:02:06 +00003709 result := android.GroupFixturePreparers(
3710 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003711 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003712
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003713 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3714 variables.Debuggable = BoolPtr(true)
3715 }),
3716 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003717
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003718 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003719 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003720}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003721
3722func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3723 t.Parallel()
3724 bp := `
3725 cc_library_static {
3726 name: "libfoo",
3727 srcs: ["foo.c"],
3728 whole_static_libs: ["libbar"],
3729 }
3730
3731 cc_library_static {
3732 name: "libbar",
3733 whole_static_libs: ["libmissing"],
3734 }
3735 `
3736
Paul Duffin8567f222021-03-23 00:02:06 +00003737 result := android.GroupFixturePreparers(
3738 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003739 android.PrepareForTestWithAllowMissingDependencies,
3740 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003741
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003742 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003743 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003744
Paul Duffine84b1332021-03-12 11:59:43 +00003745 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003746
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003747 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003748 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003749}
Colin Crosse9fe2942020-11-10 18:12:15 -08003750
3751func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003752 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003753 bp := `
3754 cc_binary {
3755 name: "bin",
3756 host_supported: true,
3757 shared_libs: ["libshared"],
3758 runtime_libs: ["libruntime"],
3759 srcs: [":gen"],
3760 }
3761
3762 cc_library_shared {
3763 name: "libshared",
3764 host_supported: true,
3765 shared_libs: ["libtransitive"],
3766 }
3767
3768 cc_library_shared {
3769 name: "libtransitive",
3770 host_supported: true,
3771 }
3772
3773 cc_library_shared {
3774 name: "libruntime",
3775 host_supported: true,
3776 }
3777
3778 cc_binary_host {
3779 name: "tool",
3780 srcs: ["foo.cpp"],
3781 }
3782
3783 genrule {
3784 name: "gen",
3785 tools: ["tool"],
3786 out: ["gen.cpp"],
3787 cmd: "$(location tool) $(out)",
3788 }
3789 `
3790
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003791 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003792 ctx := testCcWithConfig(t, config)
3793
3794 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3795 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3796 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3797 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3798 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3799
3800 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3801 t.Errorf("expected host bin dependency %q, got %q", w, g)
3802 }
3803
3804 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3805 t.Errorf("expected host bin dependency %q, got %q", w, g)
3806 }
3807
3808 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3809 t.Errorf("expected host bin dependency %q, got %q", w, g)
3810 }
3811
3812 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3813 t.Errorf("expected host bin dependency %q, got %q", w, g)
3814 }
3815
3816 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3817 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3818 }
3819
3820 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3821 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3822 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3823 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3824
3825 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3826 t.Errorf("expected device bin dependency %q, got %q", w, g)
3827 }
3828
3829 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3830 t.Errorf("expected device bin dependency %q, got %q", w, g)
3831 }
3832
3833 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3834 t.Errorf("expected device bin dependency %q, got %q", w, g)
3835 }
3836
3837 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3838 t.Errorf("expected device bin dependency %q, got %q", w, g)
3839 }
3840
3841 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3842 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3843 }
3844
3845}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003846
3847func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003848 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09003849 ctx := testCc(t, `
3850 cc_library_shared {
3851 name: "libclient",
3852 srcs: ["foo.c"],
3853 shared_libs: ["libfoo#1"],
3854 }
3855
3856 cc_library_shared {
3857 name: "libfoo",
3858 srcs: ["foo.c"],
3859 shared_libs: ["libbar"],
3860 export_shared_lib_headers: ["libbar"],
3861 stubs: {
3862 symbol_file: "foo.map.txt",
3863 versions: ["1", "2", "3"],
3864 },
3865 }
3866
3867 cc_library_shared {
3868 name: "libbar",
3869 export_include_dirs: ["include/libbar"],
3870 srcs: ["foo.c"],
3871 }`)
3872
3873 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3874
3875 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3876 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3877 }
3878}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003879
Vinh Tran09581952023-05-16 16:03:20 -04003880func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003881 t.Parallel()
3882 ctx := android.GroupFixturePreparers(
3883 prepareForCcTest,
3884 aidl_library.PrepareForTestWithAidlLibrary,
3885 android.MockFS{
3886 "package_bar/Android.bp": []byte(`
3887 aidl_library {
3888 name: "bar",
3889 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003890 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003891 strip_import_prefix: "x",
3892 }
3893 `)}.AddToFixture(),
3894 android.MockFS{
3895 "package_foo/Android.bp": []byte(`
3896 aidl_library {
3897 name: "foo",
3898 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003899 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003900 strip_import_prefix: "a",
3901 deps: ["bar"],
3902 }
3903 cc_library {
3904 name: "libfoo",
3905 aidl: {
3906 libs: ["foo"],
3907 }
3908 }
3909 `),
3910 }.AddToFixture(),
3911 ).RunTest(t).TestContext
3912
3913 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04003914
3915 android.AssertPathsRelativeToTopEquals(
3916 t,
3917 "aidl headers",
3918 []string{
3919 "package_bar/x/HeaderBar.aidl",
3920 "package_foo/a/HeaderFoo.aidl",
3921 "package_foo/a/b/Foo.aidl",
3922 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
3923 },
3924 libfoo.Rule("aidl_library").Implicits,
3925 )
3926
Colin Crossf61d03d2023-11-02 16:56:39 -07003927 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04003928 aidlCommand := manifest.Commands[0].GetCommand()
3929
3930 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
3931 if !strings.Contains(aidlCommand, expectedAidlFlags) {
3932 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
3933 }
3934
3935 outputs := strings.Join(libfoo.AllOutputs(), " ")
3936
Vinh Tran09581952023-05-16 16:03:20 -04003937 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
3938 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
3939 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003940 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
3941 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04003942 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
3943 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
3944 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003945 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
3946}
3947
Jooyung Hane197d8b2021-01-05 10:33:16 +09003948func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003949 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04003950 ctx := android.GroupFixturePreparers(
3951 prepareForCcTest,
3952 aidl_library.PrepareForTestWithAidlLibrary,
3953 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09003954 cc_library {
3955 name: "libfoo",
3956 srcs: ["a/Foo.aidl"],
3957 aidl: { flags: ["-Werror"], },
3958 }
3959 `)
3960
3961 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Colin Crossf61d03d2023-11-02 16:56:39 -07003962 manifest := android.RuleBuilderSboxProtoForTests(t, ctx.TestContext, libfoo.Output("aidl.sbox.textproto"))
Jooyung Hane197d8b2021-01-05 10:33:16 +09003963 aidlCommand := manifest.Commands[0].GetCommand()
3964 expectedAidlFlag := "-Werror"
3965 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3966 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3967 }
3968}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003969
Jooyung Han07f70c02021-11-06 07:08:45 +09003970func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003971 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09003972 for _, tc := range []struct {
3973 name string
3974 sdkVersion string
3975 variant string
3976 expected string
3977 }{
3978 {
3979 name: "default is current",
3980 sdkVersion: "",
3981 variant: "android_arm64_armv8-a_static",
3982 expected: "platform_apis",
3983 },
3984 {
3985 name: "use sdk_version",
3986 sdkVersion: `sdk_version: "29"`,
3987 variant: "android_arm64_armv8-a_static",
3988 expected: "platform_apis",
3989 },
3990 {
3991 name: "use sdk_version(sdk variant)",
3992 sdkVersion: `sdk_version: "29"`,
3993 variant: "android_arm64_armv8-a_sdk_static",
3994 expected: "29",
3995 },
3996 {
3997 name: "use min_sdk_version",
3998 sdkVersion: `min_sdk_version: "29"`,
3999 variant: "android_arm64_armv8-a_static",
4000 expected: "29",
4001 },
4002 } {
4003 t.Run(tc.name, func(t *testing.T) {
4004 ctx := testCc(t, `
4005 cc_library {
4006 name: "libfoo",
4007 stl: "none",
4008 srcs: ["a/Foo.aidl"],
4009 `+tc.sdkVersion+`
4010 }
4011 `)
4012 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
Colin Crossf61d03d2023-11-02 16:56:39 -07004013 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl.sbox.textproto"))
Jooyung Han07f70c02021-11-06 07:08:45 +09004014 aidlCommand := manifest.Commands[0].GetCommand()
4015 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4016 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4017 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4018 }
4019 })
4020 }
4021}
4022
Vinh Tran09581952023-05-16 16:03:20 -04004023func TestInvalidAidlProp(t *testing.T) {
4024 t.Parallel()
4025
4026 testCases := []struct {
4027 description string
4028 bp string
4029 }{
4030 {
4031 description: "Invalid use of aidl.libs and aidl.include_dirs",
4032 bp: `
4033 cc_library {
4034 name: "foo",
4035 aidl: {
4036 libs: ["foo_aidl"],
4037 include_dirs: ["bar/include"],
4038 }
4039 }
4040 `,
4041 },
4042 {
4043 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4044 bp: `
4045 cc_library {
4046 name: "foo",
4047 aidl: {
4048 libs: ["foo_aidl"],
4049 local_include_dirs: ["include"],
4050 }
4051 }
4052 `,
4053 },
4054 }
4055
4056 for _, testCase := range testCases {
4057 t.Run(testCase.description, func(t *testing.T) {
4058 bp := `
4059 aidl_library {
4060 name: "foo_aidl",
4061 srcs: ["Foo.aidl"],
4062 } ` + testCase.bp
4063 android.GroupFixturePreparers(
4064 prepareForCcTest,
4065 aidl_library.PrepareForTestWithAidlLibrary.
4066 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4067 ).RunTestWithBp(t, bp)
4068 })
4069 }
4070}
4071
Jiyong Parka008fb02021-03-16 17:15:53 +09004072func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004073 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004074 ctx := testCc(t, `
4075 cc_library_shared {
4076 name: "libfoo",
4077 srcs: ["foo.c"],
4078 min_sdk_version: "29",
4079 }`)
4080
4081 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4082 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4083}
4084
Vinh Tranf1924742022-06-24 16:40:11 -04004085func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004086 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004087 bp := `
4088 cc_library_shared {
4089 name: "libfoo",
4090 srcs: ["foo.c"],
4091 min_sdk_version: "S",
4092 }
4093 `
4094 result := android.GroupFixturePreparers(
4095 prepareForCcTest,
4096 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4097 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4098 }),
4099 ).RunTestWithBp(t, bp)
4100 ctx := result.TestContext
4101 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4102 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4103}
4104
Paul Duffin3cb603e2021-02-19 13:57:10 +00004105func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004106 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004107
4108 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4109 // embedded newline characters alone.
4110 trimIndentingSpaces := func(s string) string {
4111 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4112 }
4113
4114 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4115 t.Helper()
4116 expected = trimIndentingSpaces(expected)
4117 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4118 if expected != actual {
4119 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4120 }
4121 }
4122
4123 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4124
4125 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4126 t.Helper()
Colin Cross5a377182023-12-14 14:46:23 -08004127 exported, _ := android.SingletonModuleProvider(ctx, module, FlagExporterInfoProvider)
Paul Duffin3cb603e2021-02-19 13:57:10 +00004128 name := module.Name()
4129
4130 for _, checker := range checkers {
4131 checker(t, name, exported)
4132 }
4133 }
4134
4135 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4136 return func(t *testing.T, name string, exported FlagExporterInfo) {
4137 t.Helper()
4138 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4139 }
4140 }
4141
4142 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4143 return func(t *testing.T, name string, exported FlagExporterInfo) {
4144 t.Helper()
4145 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4146 }
4147 }
4148
4149 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4150 return func(t *testing.T, name string, exported FlagExporterInfo) {
4151 t.Helper()
4152 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4153 }
4154 }
4155
4156 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4157 return func(t *testing.T, name string, exported FlagExporterInfo) {
4158 t.Helper()
4159 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4160 }
4161 }
4162
4163 genRuleModules := `
4164 genrule {
4165 name: "genrule_foo",
4166 cmd: "generate-foo",
4167 out: [
4168 "generated_headers/foo/generated_header.h",
4169 ],
4170 export_include_dirs: [
4171 "generated_headers",
4172 ],
4173 }
4174
4175 genrule {
4176 name: "genrule_bar",
4177 cmd: "generate-bar",
4178 out: [
4179 "generated_headers/bar/generated_header.h",
4180 ],
4181 export_include_dirs: [
4182 "generated_headers",
4183 ],
4184 }
4185 `
4186
4187 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4188 ctx := testCc(t, genRuleModules+`
4189 cc_library {
4190 name: "libfoo",
4191 srcs: ["foo.c"],
4192 export_include_dirs: ["foo/standard"],
4193 export_system_include_dirs: ["foo/system"],
4194 generated_headers: ["genrule_foo"],
4195 export_generated_headers: ["genrule_foo"],
4196 }
4197
4198 cc_library {
4199 name: "libbar",
4200 srcs: ["bar.c"],
4201 shared_libs: ["libfoo"],
4202 export_include_dirs: ["bar/standard"],
4203 export_system_include_dirs: ["bar/system"],
4204 generated_headers: ["genrule_bar"],
4205 export_generated_headers: ["genrule_bar"],
4206 }
4207 `)
4208 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4209 checkIncludeDirs(t, ctx, foo,
4210 expectedIncludeDirs(`
4211 foo/standard
4212 .intermediates/genrule_foo/gen/generated_headers
4213 `),
4214 expectedSystemIncludeDirs(`foo/system`),
4215 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4216 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4217 )
4218
4219 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4220 checkIncludeDirs(t, ctx, bar,
4221 expectedIncludeDirs(`
4222 bar/standard
4223 .intermediates/genrule_bar/gen/generated_headers
4224 `),
4225 expectedSystemIncludeDirs(`bar/system`),
4226 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4227 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4228 )
4229 })
4230
4231 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4232 ctx := testCc(t, genRuleModules+`
4233 cc_library {
4234 name: "libfoo",
4235 srcs: ["foo.c"],
4236 export_include_dirs: ["foo/standard"],
4237 export_system_include_dirs: ["foo/system"],
4238 generated_headers: ["genrule_foo"],
4239 export_generated_headers: ["genrule_foo"],
4240 }
4241
4242 cc_library {
4243 name: "libbar",
4244 srcs: ["bar.c"],
4245 whole_static_libs: ["libfoo"],
4246 export_include_dirs: ["bar/standard"],
4247 export_system_include_dirs: ["bar/system"],
4248 generated_headers: ["genrule_bar"],
4249 export_generated_headers: ["genrule_bar"],
4250 }
4251 `)
4252 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4253 checkIncludeDirs(t, ctx, foo,
4254 expectedIncludeDirs(`
4255 foo/standard
4256 .intermediates/genrule_foo/gen/generated_headers
4257 `),
4258 expectedSystemIncludeDirs(`foo/system`),
4259 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4260 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4261 )
4262
4263 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4264 checkIncludeDirs(t, ctx, bar,
4265 expectedIncludeDirs(`
4266 bar/standard
4267 foo/standard
4268 .intermediates/genrule_foo/gen/generated_headers
4269 .intermediates/genrule_bar/gen/generated_headers
4270 `),
4271 expectedSystemIncludeDirs(`
4272 bar/system
4273 foo/system
4274 `),
4275 expectedGeneratedHeaders(`
4276 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4277 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4278 `),
4279 expectedOrderOnlyDeps(`
4280 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4281 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4282 `),
4283 )
4284 })
4285
Paul Duffin3cb603e2021-02-19 13:57:10 +00004286 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004287 ctx := android.GroupFixturePreparers(
4288 prepareForCcTest,
4289 aidl_library.PrepareForTestWithAidlLibrary,
4290 ).RunTestWithBp(t, `
4291 aidl_library {
4292 name: "libfoo_aidl",
4293 srcs: ["x/y/Bar.aidl"],
4294 strip_import_prefix: "x",
4295 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004296 cc_library_shared {
4297 name: "libfoo",
4298 srcs: [
4299 "foo.c",
4300 "b.aidl",
4301 "a.proto",
4302 ],
4303 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004304 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004305 export_aidl_headers: true,
4306 }
4307 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004308 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004309 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4310 checkIncludeDirs(t, ctx, foo,
4311 expectedIncludeDirs(`
4312 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004313 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004314 `),
4315 expectedSystemIncludeDirs(``),
4316 expectedGeneratedHeaders(`
4317 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4319 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004320 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4321 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4322 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004323 `),
4324 expectedOrderOnlyDeps(`
4325 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4326 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4327 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004328 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4329 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4330 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004331 `),
4332 )
4333 })
4334
Paul Duffin3cb603e2021-02-19 13:57:10 +00004335 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4336 ctx := testCc(t, genRuleModules+`
4337 cc_library_shared {
4338 name: "libfoo",
4339 srcs: [
4340 "foo.c",
4341 "b.aidl",
4342 "a.proto",
4343 ],
4344 proto: {
4345 export_proto_headers: true,
4346 }
4347 }
4348 `)
4349 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4350 checkIncludeDirs(t, ctx, foo,
4351 expectedIncludeDirs(`
4352 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4353 `),
4354 expectedSystemIncludeDirs(``),
4355 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004356 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4357 `),
4358 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004359 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4360 `),
4361 )
4362 })
4363
Paul Duffin33056e82021-02-19 13:49:08 +00004364 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004365 ctx := testCc(t, genRuleModules+`
4366 cc_library_shared {
4367 name: "libfoo",
4368 srcs: [
4369 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004370 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004371 "b.aidl",
4372 "a.proto",
4373 ],
4374 }
4375 `)
4376 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4377 checkIncludeDirs(t, ctx, foo,
4378 expectedIncludeDirs(`
4379 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4380 `),
4381 expectedSystemIncludeDirs(``),
4382 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004383 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004384 `),
4385 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004386 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4387 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004388 `),
4389 )
4390 })
4391}
Colin Crossae628182021-06-14 16:52:28 -07004392
4393func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004394 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004395 baseExpectedFlags := []string{
4396 "${config.ArmThumbCflags}",
4397 "${config.ArmCflags}",
4398 "${config.CommonGlobalCflags}",
4399 "${config.DeviceGlobalCflags}",
4400 "${config.ExternalCflags}",
4401 "${config.ArmToolchainCflags}",
4402 "${config.ArmArmv7ANeonCflags}",
4403 "${config.ArmGenericCflags}",
4404 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004405 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004406 }
4407
4408 expectedIncludes := []string{
4409 "external/foo/android_arm_export_include_dirs",
4410 "external/foo/lib32_export_include_dirs",
4411 "external/foo/arm_export_include_dirs",
4412 "external/foo/android_export_include_dirs",
4413 "external/foo/linux_export_include_dirs",
4414 "external/foo/export_include_dirs",
4415 "external/foo/android_arm_local_include_dirs",
4416 "external/foo/lib32_local_include_dirs",
4417 "external/foo/arm_local_include_dirs",
4418 "external/foo/android_local_include_dirs",
4419 "external/foo/linux_local_include_dirs",
4420 "external/foo/local_include_dirs",
4421 "external/foo",
4422 "external/foo/libheader1",
4423 "external/foo/libheader2",
4424 "external/foo/libwhole1",
4425 "external/foo/libwhole2",
4426 "external/foo/libstatic1",
4427 "external/foo/libstatic2",
4428 "external/foo/libshared1",
4429 "external/foo/libshared2",
4430 "external/foo/liblinux",
4431 "external/foo/libandroid",
4432 "external/foo/libarm",
4433 "external/foo/lib32",
4434 "external/foo/libandroid_arm",
4435 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004436 }
4437
4438 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4439 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4440
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004441 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004442 cstd := []string{"-std=gnu17", "-std=conly"}
Elliott Hughesc79d9e32022-01-13 14:56:02 -08004443 cppstd := []string{"-std=gnu++20", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004444
4445 lastIncludes := []string{
4446 "out/soong/ndk/sysroot/usr/include",
4447 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4448 }
4449
4450 combineSlices := func(slices ...[]string) []string {
4451 var ret []string
4452 for _, s := range slices {
4453 ret = append(ret, s...)
4454 }
4455 return ret
4456 }
4457
4458 testCases := []struct {
4459 name string
4460 src string
4461 expected []string
4462 }{
4463 {
4464 name: "c",
4465 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004466 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004467 },
4468 {
4469 name: "cc",
4470 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004471 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004472 },
4473 {
4474 name: "assemble",
4475 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004476 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004477 },
4478 }
4479
4480 for _, tc := range testCases {
4481 t.Run(tc.name, func(t *testing.T) {
4482 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004483 cc_library {
4484 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004485 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004486 cflags: ["-std=candcpp"],
4487 conlyflags: ["-std=conly"],
4488 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004489 local_include_dirs: ["local_include_dirs"],
4490 export_include_dirs: ["export_include_dirs"],
4491 export_system_include_dirs: ["export_system_include_dirs"],
4492 static_libs: ["libstatic1", "libstatic2"],
4493 whole_static_libs: ["libwhole1", "libwhole2"],
4494 shared_libs: ["libshared1", "libshared2"],
4495 header_libs: ["libheader1", "libheader2"],
4496 target: {
4497 android: {
4498 shared_libs: ["libandroid"],
4499 local_include_dirs: ["android_local_include_dirs"],
4500 export_include_dirs: ["android_export_include_dirs"],
4501 },
4502 android_arm: {
4503 shared_libs: ["libandroid_arm"],
4504 local_include_dirs: ["android_arm_local_include_dirs"],
4505 export_include_dirs: ["android_arm_export_include_dirs"],
4506 },
4507 linux: {
4508 shared_libs: ["liblinux"],
4509 local_include_dirs: ["linux_local_include_dirs"],
4510 export_include_dirs: ["linux_export_include_dirs"],
4511 },
4512 },
4513 multilib: {
4514 lib32: {
4515 shared_libs: ["lib32"],
4516 local_include_dirs: ["lib32_local_include_dirs"],
4517 export_include_dirs: ["lib32_export_include_dirs"],
4518 },
4519 },
4520 arch: {
4521 arm: {
4522 shared_libs: ["libarm"],
4523 local_include_dirs: ["arm_local_include_dirs"],
4524 export_include_dirs: ["arm_export_include_dirs"],
4525 },
4526 },
4527 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004528 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004529 }
4530
4531 cc_library_headers {
4532 name: "libheader1",
4533 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004534 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004535 stl: "none",
4536 }
4537
4538 cc_library_headers {
4539 name: "libheader2",
4540 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004541 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004542 stl: "none",
4543 }
Liz Kammer08572c62021-09-30 10:11:04 -04004544 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004545
Liz Kammer08572c62021-09-30 10:11:04 -04004546 libs := []string{
4547 "libstatic1",
4548 "libstatic2",
4549 "libwhole1",
4550 "libwhole2",
4551 "libshared1",
4552 "libshared2",
4553 "libandroid",
4554 "libandroid_arm",
4555 "liblinux",
4556 "lib32",
4557 "libarm",
4558 }
Colin Crossae628182021-06-14 16:52:28 -07004559
Liz Kammer08572c62021-09-30 10:11:04 -04004560 for _, lib := range libs {
4561 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004562 cc_library {
4563 name: "%s",
4564 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004565 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004566 stl: "none",
4567 }
4568 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004569 }
4570
4571 ctx := android.GroupFixturePreparers(
4572 PrepareForIntegrationTestWithCc,
4573 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4574 ).RunTest(t)
4575 // Use the arm variant instead of the arm64 variant so that it gets headers from
4576 // ndk_libandroid_support to test LateStaticLibs.
4577 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4578
4579 var includes []string
4580 flags := strings.Split(cflags, " ")
4581 for _, flag := range flags {
4582 if strings.HasPrefix(flag, "-I") {
4583 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4584 } else if flag == "-isystem" {
4585 // skip isystem, include next
4586 } else if len(flag) > 0 {
4587 includes = append(includes, flag)
4588 }
4589 }
4590
4591 android.AssertArrayString(t, "includes", tc.expected, includes)
4592 })
Colin Crossae628182021-06-14 16:52:28 -07004593 }
4594
Colin Crossae628182021-06-14 16:52:28 -07004595}
Alixb5f6d9e2022-04-20 23:00:58 +00004596
zijunzhao933e3802023-01-12 07:26:20 +00004597func TestAddnoOverride64GlobalCflags(t *testing.T) {
4598 t.Parallel()
4599 ctx := testCc(t, `
4600 cc_library_shared {
4601 name: "libclient",
4602 srcs: ["foo.c"],
4603 shared_libs: ["libfoo#1"],
4604 }
4605
4606 cc_library_shared {
4607 name: "libfoo",
4608 srcs: ["foo.c"],
4609 shared_libs: ["libbar"],
4610 export_shared_lib_headers: ["libbar"],
4611 stubs: {
4612 symbol_file: "foo.map.txt",
4613 versions: ["1", "2", "3"],
4614 },
4615 }
4616
4617 cc_library_shared {
4618 name: "libbar",
4619 export_include_dirs: ["include/libbar"],
4620 srcs: ["foo.c"],
4621 }`)
4622
4623 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4624
4625 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4626 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4627 }
4628}
4629
Alixb5f6d9e2022-04-20 23:00:58 +00004630func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004631 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004632 tests := []struct {
4633 name string
4634 clang bool
4635 BuildBrokenClangProperty bool
4636 err string
4637 }{
4638 {
4639 name: "error when clang is set to false",
4640 clang: false,
4641 err: "is no longer supported",
4642 },
4643 {
4644 name: "error when clang is set to true",
4645 clang: true,
4646 err: "property is deprecated, see Changes.md",
4647 },
4648 {
4649 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4650 clang: true,
4651 BuildBrokenClangProperty: true,
4652 },
4653 }
4654
4655 for _, test := range tests {
4656 t.Run(test.name, func(t *testing.T) {
4657 bp := fmt.Sprintf(`
4658 cc_library {
4659 name: "foo",
4660 clang: %t,
4661 }`, test.clang)
4662
4663 if test.err == "" {
4664 android.GroupFixturePreparers(
4665 prepareForCcTest,
4666 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4667 if test.BuildBrokenClangProperty {
4668 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4669 }
4670 }),
4671 ).RunTestWithBp(t, bp)
4672 } else {
4673 prepareForCcTest.
4674 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4675 RunTestWithBp(t, bp)
4676 }
4677 })
4678 }
4679}
Alix Espinoef47e542022-09-14 19:10:51 +00004680
4681func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004682 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004683 tests := []struct {
4684 name string
4685 clangAsFlags []string
4686 BuildBrokenClangAsFlags bool
4687 err string
4688 }{
4689 {
4690 name: "error when clang_asflags is set",
4691 clangAsFlags: []string{"-a", "-b"},
4692 err: "clang_asflags: property is deprecated",
4693 },
4694 {
4695 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4696 clangAsFlags: []string{"-a", "-b"},
4697 BuildBrokenClangAsFlags: true,
4698 },
4699 }
4700
4701 for _, test := range tests {
4702 t.Run(test.name, func(t *testing.T) {
4703 bp := fmt.Sprintf(`
4704 cc_library {
4705 name: "foo",
4706 clang_asflags: %s,
4707 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4708
4709 if test.err == "" {
4710 android.GroupFixturePreparers(
4711 prepareForCcTest,
4712 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4713 if test.BuildBrokenClangAsFlags {
4714 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4715 }
4716 }),
4717 ).RunTestWithBp(t, bp)
4718 } else {
4719 prepareForCcTest.
4720 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4721 RunTestWithBp(t, bp)
4722 }
4723 })
4724 }
4725}
4726
4727func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004728 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004729 tests := []struct {
4730 name string
4731 clangCFlags []string
4732 BuildBrokenClangCFlags bool
4733 err string
4734 }{
4735 {
4736 name: "error when clang_cflags is set",
4737 clangCFlags: []string{"-a", "-b"},
4738 err: "clang_cflags: property is deprecated",
4739 },
4740 {
4741 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4742 clangCFlags: []string{"-a", "-b"},
4743 BuildBrokenClangCFlags: true,
4744 },
4745 }
4746
4747 for _, test := range tests {
4748 t.Run(test.name, func(t *testing.T) {
4749 bp := fmt.Sprintf(`
4750 cc_library {
4751 name: "foo",
4752 clang_cflags: %s,
4753 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4754
4755 if test.err == "" {
4756 android.GroupFixturePreparers(
4757 prepareForCcTest,
4758 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4759 if test.BuildBrokenClangCFlags {
4760 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4761 }
4762 }),
4763 ).RunTestWithBp(t, bp)
4764 } else {
4765 prepareForCcTest.
4766 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4767 RunTestWithBp(t, bp)
4768 }
4769 })
4770 }
4771}
Wei Li5f5d2712023-12-11 15:40:29 -08004772
4773func TestStrippedAllOutputFile(t *testing.T) {
4774 t.Parallel()
4775 bp := `
4776 cc_library {
4777 name: "test_lib",
4778 srcs: ["test_lib.cpp"],
4779 dist: {
4780 targets: [ "dist_target" ],
4781 tag: "stripped_all",
4782 }
4783 }
4784 `
4785 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
4786 ctx := testCcWithConfig(t, config)
4787 module := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared").Module()
4788 outputFile, err := module.(android.OutputFileProducer).OutputFiles("stripped_all")
4789 if err != nil {
4790 t.Errorf("Expected cc_library to produce output files, error: %s", err)
4791 return
4792 }
4793 if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") {
4794 t.Errorf("Unexpected output file: %s", outputFile.Strings()[0])
4795 return
4796 }
4797}
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09004798
4799// TODO(b/316829758) Remove this test and do not set VNDK version from other tests
4800func TestImageVariantsWithoutVndk(t *testing.T) {
4801 t.Parallel()
4802
4803 bp := `
4804 cc_binary {
4805 name: "binfoo",
4806 srcs: ["binfoo.cc"],
4807 vendor_available: true,
4808 product_available: true,
4809 shared_libs: ["libbar"]
4810 }
4811 cc_library {
4812 name: "libbar",
4813 srcs: ["libbar.cc"],
4814 vendor_available: true,
4815 product_available: true,
4816 }
4817 `
4818
4819 ctx := prepareForCcTestWithoutVndk.RunTestWithBp(t, bp)
4820
4821 hasDep := func(m android.Module, wantDep android.Module) bool {
4822 t.Helper()
4823 var found bool
4824 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
4825 if dep == wantDep {
4826 found = true
4827 }
4828 })
4829 return found
4830 }
4831
4832 testDepWithVariant := func(imageVariant string) {
4833 imageVariantStr := ""
4834 if imageVariant != "core" {
4835 imageVariantStr = "_" + imageVariant
4836 }
4837 binFooModule := ctx.ModuleForTests("binfoo", "android"+imageVariantStr+"_arm64_armv8-a").Module()
4838 libBarModule := ctx.ModuleForTests("libbar", "android"+imageVariantStr+"_arm64_armv8-a_shared").Module()
4839 android.AssertBoolEquals(t, "binfoo should have dependency on libbar with image variant "+imageVariant, true, hasDep(binFooModule, libBarModule))
4840 }
4841
4842 testDepWithVariant("core")
4843 testDepWithVariant("vendor")
4844 testDepWithVariant("product")
4845}