blob: 5836b6c599f7e43337edb8b1c3d756f76d10996b [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")
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +080046 variables.KeepVndk = BoolPtr(true)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090047 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000048 }),
49)
50
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090051// TODO(b/316829758) Update prepareForCcTest with this configuration and remove prepareForCcTestWithoutVndk
52var prepareForCcTestWithoutVndk = android.GroupFixturePreparers(
53 PrepareForIntegrationTestWithCc,
54 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
55 variables.VendorApiLevel = StringPtr("202404")
56 }),
57)
58
Yu Liue4312402023-01-18 09:15:31 -080059var apexVariationName = "apex28"
60var apexVersion = "28"
61
62func registerTestMutators(ctx android.RegistrationContext) {
63 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
64 ctx.BottomUp("apex", testApexMutator).Parallel()
Yu Liue4312402023-01-18 09:15:31 -080065 })
66}
67
Yu Liue4312402023-01-18 09:15:31 -080068func testApexMutator(mctx android.BottomUpMutatorContext) {
69 modules := mctx.CreateVariations(apexVariationName)
70 apexInfo := android.ApexInfo{
71 ApexVariationName: apexVariationName,
72 MinSdkVersion: android.ApiLevelForTest(apexVersion),
73 }
74 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
75}
76
Paul Duffin8567f222021-03-23 00:02:06 +000077// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000078//
79// See testCc for an explanation as to how to stop using this deprecated method.
80//
81// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080082func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070083 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000084 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000085 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090086}
87
Paul Duffin8567f222021-03-23 00:02:06 +000088// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000089//
Paul Duffin8567f222021-03-23 00:02:06 +000090// 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 +000091// easier to customize the test behavior.
92//
93// 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 +000094// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000095// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
96// that it did not change the test behavior unexpectedly.
97//
98// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080099func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800100 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000101 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000102 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800103}
104
Paul Duffin8567f222021-03-23 00:02:06 +0000105// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000106//
107// See testCc for an explanation as to how to stop using this deprecated method.
108//
109// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900110func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800111 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800112
Paul Duffin8567f222021-03-23 00:02:06 +0000113 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000114 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
115 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800116}
117
Paul Duffin8567f222021-03-23 00:02:06 +0000118// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000119//
120// See testCc for an explanation as to how to stop using this deprecated method.
121//
122// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900123func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900124 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000125 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900127 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900128 testCcErrorWithConfig(t, pattern, config)
129 return
130}
131
Paul Duffin8567f222021-03-23 00:02:06 +0000132// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000133//
134// See testCc for an explanation as to how to stop using this deprecated method.
135//
136// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900138 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000139 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900140 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900141 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900142 testCcErrorWithConfig(t, pattern, config)
143 return
144}
145
Logan Chienf3511742017-10-31 18:04:35 +0800146const (
Colin Cross7113d202019-11-20 16:39:12 -0800147 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900148 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
149 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800150 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800151)
152
Paul Duffindb462dd2021-03-21 22:01:55 +0000153// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
154// running it in a fixture that requires all source files to exist.
155func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
156 android.GroupFixturePreparers(
157 PrepareForTestWithCcDefaultModules,
158 android.PrepareForTestDisallowNonExistentPaths,
159 ).RunTest(t)
160}
161
Jiyong Park6a43f042017-10-12 23:05:00 +0900162func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400163 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900164 ctx := testCc(t, `
165 cc_library {
166 name: "libTest",
167 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700168 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800169 nocrt: true,
170 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900171 vendor_available: true,
172 target: {
173 vendor: {
174 srcs: ["bar.c"],
175 },
176 },
177 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900178 `)
179
Logan Chienf3511742017-10-31 18:04:35 +0800180 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900181 var objs []string
182 for _, o := range ld.Inputs {
183 objs = append(objs, o.Base())
184 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800185 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900186 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
187 }
188}
189
Justin Yun7f99ec72021-04-12 13:19:28 +0900190func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
191 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
192 partitionDefined := false
193 checkPartition := func(specific bool, partition string) {
194 if specific {
195 if expected != partition && !partitionDefined {
196 // The variant is installed to the 'partition'
197 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
198 }
199 partitionDefined = true
200 } else {
201 // The variant is not installed to the 'partition'
202 if expected == partition {
203 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
204 }
205 }
206 }
207 socSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800208 return m.SocSpecific() || m.InstallInVendor()
Justin Yun7f99ec72021-04-12 13:19:28 +0900209 }
210 deviceSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800211 return m.DeviceSpecific() || m.InstallInOdm()
Justin Yun7f99ec72021-04-12 13:19:28 +0900212 }
213 productSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800214 return m.ProductSpecific() || m.InstallInProduct()
Justin Yun7f99ec72021-04-12 13:19:28 +0900215 }
216 systemExtSpecific := func(m *Module) bool {
217 return m.SystemExtSpecific()
218 }
219 checkPartition(socSpecific(mod), "vendor")
220 checkPartition(deviceSpecific(mod), "odm")
221 checkPartition(productSpecific(mod), "product")
222 checkPartition(systemExtSpecific(mod), "system_ext")
223 if !partitionDefined && expected != "system" {
224 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
225 " but installed to system partition", variant, name, expected)
226 }
227}
228
229func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400230 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900231 t.Helper()
232 ctx := prepareForCcTest.RunTestWithBp(t, `
233 cc_library {
234 name: "libsystem",
235 }
236 cc_library {
237 name: "libsystem_ext",
238 system_ext_specific: true,
239 }
240 cc_library {
241 name: "libproduct",
242 product_specific: true,
243 }
244 cc_library {
245 name: "libvendor",
246 vendor: true,
247 }
248 cc_library {
249 name: "libodm",
250 device_specific: true,
251 }
252 cc_library {
253 name: "liball_available",
254 vendor_available: true,
255 product_available: true,
256 }
257 cc_library {
258 name: "libsystem_ext_all_available",
259 system_ext_specific: true,
260 vendor_available: true,
261 product_available: true,
262 }
263 cc_library {
264 name: "liball_available_odm",
265 odm_available: true,
266 product_available: true,
267 }
268 cc_library {
269 name: "libproduct_vendoravailable",
270 product_specific: true,
271 vendor_available: true,
272 }
273 cc_library {
274 name: "libproduct_odmavailable",
275 product_specific: true,
276 odm_available: true,
277 }
278 `).TestContext
279
280 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
281 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
282 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
283 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
284 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
285
286 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
287 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
288 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
289
290 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
291 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
292 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
293
294 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
295 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
296 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
297
298 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
299 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
300
301 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
302 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
303}
304
Logan Chienf3511742017-10-31 18:04:35 +0800305func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900306 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800307
Logan Chiend3c59a22018-03-29 14:08:15 +0800308 t.Helper()
309
Justin Yun0ecf0b22020-02-28 15:07:59 +0900310 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800311
312 // Check library properties.
313 lib, ok := mod.compiler.(*libraryDecorator)
314 if !ok {
315 t.Errorf("%q must have libraryDecorator", name)
316 } else if lib.baseInstaller.subDir != subDir {
317 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
318 lib.baseInstaller.subDir)
319 }
320
321 // Check VNDK properties.
322 if mod.vndkdep == nil {
323 t.Fatalf("%q must have `vndkdep`", name)
324 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700325 if !mod.IsVndk() {
326 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800327 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400328 if mod.IsVndkSp() != isVndkSp {
329 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800330 }
331
332 // Check VNDK extension properties.
333 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500334 if mod.IsVndkExt() != isVndkExt {
335 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800336 }
337
338 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
339 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
340 }
341}
342
Colin Crossf61d03d2023-11-02 16:56:39 -0700343func checkWriteFileOutput(t *testing.T, ctx *android.TestContext, params android.TestingBuildParams, expected []string) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900344 t.Helper()
Colin Crossf61d03d2023-11-02 16:56:39 -0700345 content := android.ContentFromFileRuleForTests(t, ctx, params)
Colin Crosscf371cc2020-11-13 11:48:42 -0800346 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900347 assertArrayString(t, actual, expected)
348}
349
Jooyung Han097087b2019-10-22 19:32:18 +0900350func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
351 t.Helper()
352 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Colin Crossf61d03d2023-11-02 16:56:39 -0700353 checkWriteFileOutput(t, ctx, vndkSnapshot.Output(output), expected)
Jooyung Han2216fb12019-11-06 16:46:15 +0900354}
355
356func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
357 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800358 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800359 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900360}
361
Logan Chienf3511742017-10-31 18:04:35 +0800362func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400363 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800364 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800365 cc_library {
366 name: "libvndk",
367 vendor_available: true,
368 vndk: {
369 enabled: true,
370 },
371 nocrt: true,
372 }
373
374 cc_library {
375 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900376 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800377 vndk: {
378 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900379 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800380 },
381 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900382 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800383 }
384
385 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900386 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800387 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900388 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800389 vndk: {
390 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900391 },
392 nocrt: true,
393 target: {
394 vendor: {
395 cflags: ["-DTEST"],
396 },
397 product: {
398 cflags: ["-DTEST"],
399 },
400 },
401 }
402
403 cc_library {
404 name: "libvndk_sp",
405 vendor_available: true,
406 vndk: {
407 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800408 support_system_process: true,
409 },
410 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900411 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800412 }
413
414 cc_library {
415 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900416 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800417 vndk: {
418 enabled: true,
419 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900420 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800421 },
422 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900423 target: {
424 vendor: {
425 suffix: "-x",
426 },
427 },
Logan Chienf3511742017-10-31 18:04:35 +0800428 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900429
430 cc_library {
431 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900432 vendor_available: true,
433 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900434 vndk: {
435 enabled: true,
436 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900437 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900438 },
439 nocrt: true,
440 target: {
441 vendor: {
442 suffix: "-x",
443 },
444 product: {
445 suffix: "-x",
446 },
447 },
448 }
449
Justin Yun450ae722021-04-16 19:58:18 +0900450 cc_library {
451 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700452 llndk: {
453 symbol_file: "libllndk.map.txt",
454 export_llndk_headers: ["libllndk_headers"],
455 }
Justin Yun450ae722021-04-16 19:58:18 +0900456 }
457
Justin Yun611e8862021-05-24 18:17:33 +0900458 cc_library {
459 name: "libclang_rt.hwasan-llndk",
460 llndk: {
461 symbol_file: "libclang_rt.hwasan.map.txt",
462 }
463 }
464
Colin Cross627280f2021-04-26 16:53:58 -0700465 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900466 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700467 llndk: {
468 llndk_headers: true,
469 },
Justin Yun450ae722021-04-16 19:58:18 +0900470 export_include_dirs: ["include"],
471 }
472
Colin Crosse4e44bc2020-12-28 13:50:21 -0800473 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900474 name: "llndk.libraries.txt",
475 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800476 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900477 name: "vndkcore.libraries.txt",
478 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800479 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900480 name: "vndksp.libraries.txt",
481 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800482 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900483 name: "vndkprivate.libraries.txt",
484 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800485 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900486 name: "vndkproduct.libraries.txt",
487 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800488 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900489 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800490 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900491 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800492 `
493
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000494 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800495 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900496 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800497
498 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800499
Jooyung Han261e1582020-10-20 18:54:21 +0900500 // subdir == "" because VNDK libs are not supposed to be installed separately.
501 // They are installed as part of VNDK APEX instead.
502 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
503 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900504 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900505 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
506 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900507 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900508
Justin Yun6977e8a2020-10-29 18:24:11 +0900509 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
510 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900511
Inseob Kim1f086e22019-05-09 13:29:15 +0900512 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900513 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000514 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900515
516 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
517 "arm64", "armv8-a"))
518 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
519 "arm", "armv7-a-neon"))
520
521 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
522 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900523 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
524
Inseob Kim1f086e22019-05-09 13:29:15 +0900525 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
526 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900527 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900528
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900529 variant := "android_vendor.29_arm64_armv8-a_shared"
530 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900531
Inseob Kim7f283f42020-06-01 21:53:49 +0900532 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
533
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400534 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
535 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
536 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
537 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
538 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
539 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
540 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
541 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900542
Jooyung Han39edb6c2019-11-06 16:53:07 +0900543 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800544 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
545 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
546 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
547 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
548 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900549
Jooyung Han097087b2019-10-22 19:32:18 +0900550 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
551 "LLNDK: libc.so",
552 "LLNDK: libdl.so",
553 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900554 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900555 "LLNDK: libm.so",
556 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900557 "VNDK-SP: libvndk_sp-x.so",
558 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900559 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900560 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900561 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900562 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900563 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900564 "VNDK-private: libvndk-private.so",
565 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900566 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900567 "VNDK-product: libc++.so",
568 "VNDK-product: libvndk_product.so",
569 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900570 })
Justin Yun611e8862021-05-24 18:17:33 +0900571 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 +0900572 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
573 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
574 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 +0900575 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 +0900576 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
577}
578
Yo Chiangbba545e2020-06-09 16:15:37 +0800579func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400580 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800581 ctx := testCc(t, `
582 cc_library {
583 name: "libvndk_host_supported",
584 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900585 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800586 vndk: {
587 enabled: true,
588 },
589 host_supported: true,
590 }
591
592 cc_library {
593 name: "libvndk_host_supported_but_disabled_on_device",
594 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900595 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800596 vndk: {
597 enabled: true,
598 },
599 host_supported: true,
600 enabled: false,
601 target: {
602 host: {
603 enabled: true,
604 }
605 }
606 }
607
Colin Crosse4e44bc2020-12-28 13:50:21 -0800608 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800609 name: "vndkcore.libraries.txt",
610 }
611 `)
612
613 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
614}
615
Jooyung Han2216fb12019-11-06 16:46:15 +0900616func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400617 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800618 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800619 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900620 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800621 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800622 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000623 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800624 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900625 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900626 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800627 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900628
Colin Cross45bce852021-11-11 22:47:54 -0800629 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700630 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900631 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900632}
633
634func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400635 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800636 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900637 cc_library {
638 name: "libvndk",
639 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900640 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900641 vndk: {
642 enabled: true,
643 },
644 nocrt: true,
645 }
646
647 cc_library {
648 name: "libvndk_sp",
649 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900650 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900651 vndk: {
652 enabled: true,
653 support_system_process: true,
654 },
655 nocrt: true,
656 }
657
658 cc_library {
659 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900660 vendor_available: true,
661 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900662 vndk: {
663 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900664 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900665 },
666 nocrt: true,
667 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900668
Colin Crosse4e44bc2020-12-28 13:50:21 -0800669 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900670 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800671 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900672 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800673 `
674
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000675 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800676 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900677 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800678 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900679 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800680
681 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
682
683 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900684
Jooyung Han2216fb12019-11-06 16:46:15 +0900685 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900686}
687
Chris Parsons79d66a52020-06-05 17:26:16 -0400688func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400689 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400690 bp := `
691 cc_test_library {
692 name: "test_lib",
693 srcs: ["test_lib.cpp"],
694 gtest: false,
695 }
696
697 cc_test {
698 name: "main_test",
699 data_libs: ["test_lib"],
700 gtest: false,
701 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400702 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400703
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000704 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400705 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900706 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400707 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
708
709 ctx := testCcWithConfig(t, config)
710 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
711 testBinary := module.(*Module).linker.(*testBinary)
712 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
713 if err != nil {
714 t.Errorf("Expected cc_test to produce output files, error: %s", err)
715 return
716 }
717 if len(outputFiles) != 1 {
718 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
719 return
720 }
721 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800722 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons79d66a52020-06-05 17:26:16 -0400723 return
724 }
725
726 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400727 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400728
729 if !strings.HasSuffix(outputPath, "/main_test") {
730 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
731 return
732 }
733 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
734 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
735 return
736 }
737}
738
Chris Parsons216e10a2020-07-09 17:12:52 -0400739func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400740 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400741 bp := `
742 cc_test_library {
743 name: "test_lib",
744 srcs: ["test_lib.cpp"],
745 relative_install_path: "foo/bar/baz",
746 gtest: false,
747 }
748
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400749 cc_binary {
750 name: "test_bin",
751 relative_install_path: "foo/bar/baz",
752 compile_multilib: "both",
753 }
754
Chris Parsons216e10a2020-07-09 17:12:52 -0400755 cc_test {
756 name: "main_test",
757 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400758 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400759 gtest: false,
760 }
761 `
762
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000763 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400764 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900765 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400766 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
767
768 ctx := testCcWithConfig(t, config)
769 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
770 testBinary := module.(*Module).linker.(*testBinary)
771 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
772 if err != nil {
773 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
774 }
775 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400776 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400777 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400778 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800779 t.Fatalf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400780 }
781
782 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400783
784 if !strings.HasSuffix(outputPath, "/main_test") {
785 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
786 }
Colin Crossaa255532020-07-03 13:18:24 -0700787 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400788 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
789 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400790 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400791 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400792 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
793 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
794 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
795 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400796}
797
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000798func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400799 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000800 bp := `
801 cc_test {
802 name: "main_test",
803 srcs: ["main_test.cpp"],
804 test_suites: [
805 "suite_1",
806 "suite_2",
807 ],
808 gtest: false,
809 }
810 `
811
812 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
813 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
814
815 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
816 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
817 if len(compatEntries) != 2 {
818 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
819 }
820 if compatEntries[0] != "suite_1" {
821 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
822 " but was '%s'", compatEntries[0])
823 }
824 if compatEntries[1] != "suite_2" {
825 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
826 " but was '%s'", compatEntries[1])
827 }
828}
829
830func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400831 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000832 bp := `
833 cc_test_library {
834 name: "main_test_lib",
835 srcs: ["main_test_lib.cpp"],
836 test_suites: [
837 "suite_1",
838 "suite_2",
839 ],
840 gtest: false,
841 }
842 `
843
844 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
845 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
846
847 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
848 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
849 if len(compatEntries) != 2 {
850 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
851 }
852 if compatEntries[0] != "suite_1" {
853 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
854 " but was '%s'", compatEntries[0])
855 }
856 if compatEntries[1] != "suite_2" {
857 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
858 " but was '%s'", compatEntries[1])
859 }
860}
861
Justin Yun63e9ec72020-10-29 16:49:43 +0900862func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400863 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900864 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900865 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900866 cc_library {
867 name: "libvndk",
868 vndk: {
869 enabled: true,
870 },
871 nocrt: true,
872 }
873 `)
874
Justin Yunc0d8c492021-01-07 17:45:31 +0900875 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900876 cc_library {
877 name: "libvndk",
878 product_available: true,
879 vndk: {
880 enabled: true,
881 },
882 nocrt: true,
883 }
884 `)
885
Justin Yun6977e8a2020-10-29 18:24:11 +0900886 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
887 cc_library {
888 name: "libvndkprop",
889 vendor_available: true,
890 product_available: true,
891 vndk: {
892 enabled: true,
893 },
894 nocrt: true,
895 target: {
896 vendor: {
897 cflags: ["-DTEST",],
898 },
899 },
900 }
901 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900902}
903
Logan Chiend3c59a22018-03-29 14:08:15 +0800904func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400905 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800906 // Check whether an error is emitted when a VNDK lib depends on a system lib.
907 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
908 cc_library {
909 name: "libvndk",
910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900911 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800912 vndk: {
913 enabled: true,
914 },
915 shared_libs: ["libfwk"], // Cause error
916 nocrt: true,
917 }
918
919 cc_library {
920 name: "libfwk",
921 nocrt: true,
922 }
923 `)
924
925 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
926 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
927 cc_library {
928 name: "libvndk",
929 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900930 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800931 vndk: {
932 enabled: true,
933 },
934 shared_libs: ["libvendor"], // Cause error
935 nocrt: true,
936 }
937
938 cc_library {
939 name: "libvendor",
940 vendor: true,
941 nocrt: true,
942 }
943 `)
944
945 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
946 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
947 cc_library {
948 name: "libvndk_sp",
949 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900950 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800951 vndk: {
952 enabled: true,
953 support_system_process: true,
954 },
955 shared_libs: ["libfwk"], // Cause error
956 nocrt: true,
957 }
958
959 cc_library {
960 name: "libfwk",
961 nocrt: true,
962 }
963 `)
964
965 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
966 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
967 cc_library {
968 name: "libvndk_sp",
969 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900970 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800971 vndk: {
972 enabled: true,
973 support_system_process: true,
974 },
975 shared_libs: ["libvendor"], // Cause error
976 nocrt: true,
977 }
978
979 cc_library {
980 name: "libvendor",
981 vendor: true,
982 nocrt: true,
983 }
984 `)
985
986 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
987 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
988 cc_library {
989 name: "libvndk_sp",
990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900991 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800992 vndk: {
993 enabled: true,
994 support_system_process: true,
995 },
996 shared_libs: ["libvndk"], // Cause error
997 nocrt: true,
998 }
999
1000 cc_library {
1001 name: "libvndk",
1002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001003 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001004 vndk: {
1005 enabled: true,
1006 },
1007 nocrt: true,
1008 }
1009 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001010
1011 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1012 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1013 cc_library {
1014 name: "libvndk",
1015 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001016 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001017 vndk: {
1018 enabled: true,
1019 },
1020 shared_libs: ["libnonvndk"],
1021 nocrt: true,
1022 }
1023
1024 cc_library {
1025 name: "libnonvndk",
1026 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001027 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001028 nocrt: true,
1029 }
1030 `)
1031
1032 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1033 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1034 cc_library {
1035 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001036 vendor_available: true,
1037 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001038 vndk: {
1039 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001040 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001041 },
1042 shared_libs: ["libnonvndk"],
1043 nocrt: true,
1044 }
1045
1046 cc_library {
1047 name: "libnonvndk",
1048 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001049 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001050 nocrt: true,
1051 }
1052 `)
1053
1054 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1055 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1056 cc_library {
1057 name: "libvndksp",
1058 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001059 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001060 vndk: {
1061 enabled: true,
1062 support_system_process: true,
1063 },
1064 shared_libs: ["libnonvndk"],
1065 nocrt: true,
1066 }
1067
1068 cc_library {
1069 name: "libnonvndk",
1070 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001071 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001072 nocrt: true,
1073 }
1074 `)
1075
1076 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1077 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1078 cc_library {
1079 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001080 vendor_available: true,
1081 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 vndk: {
1083 enabled: true,
1084 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001085 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001086 },
1087 shared_libs: ["libnonvndk"],
1088 nocrt: true,
1089 }
1090
1091 cc_library {
1092 name: "libnonvndk",
1093 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001094 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001095 nocrt: true,
1096 }
1097 `)
1098}
1099
1100func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001101 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 // okay to link : LLNDK -> double_loadable VNDK
1103 testCc(t, `
1104 cc_library {
1105 name: "libllndk",
1106 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001107 llndk: {
1108 symbol_file: "libllndk.map.txt",
1109 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001110 }
1111
1112 cc_library {
1113 name: "libdoubleloadable",
1114 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001115 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001116 vndk: {
1117 enabled: true,
1118 },
1119 double_loadable: true,
1120 }
1121 `)
1122 // okay to link : LLNDK -> VNDK-SP
1123 testCc(t, `
1124 cc_library {
1125 name: "libllndk",
1126 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001127 llndk: {
1128 symbol_file: "libllndk.map.txt",
1129 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001130 }
1131
1132 cc_library {
1133 name: "libvndksp",
1134 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001135 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001136 vndk: {
1137 enabled: true,
1138 support_system_process: true,
1139 },
1140 }
1141 `)
1142 // okay to link : double_loadable -> double_loadable
1143 testCc(t, `
1144 cc_library {
1145 name: "libdoubleloadable1",
1146 shared_libs: ["libdoubleloadable2"],
1147 vendor_available: true,
1148 double_loadable: true,
1149 }
1150
1151 cc_library {
1152 name: "libdoubleloadable2",
1153 vendor_available: true,
1154 double_loadable: true,
1155 }
1156 `)
1157 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1158 testCc(t, `
1159 cc_library {
1160 name: "libdoubleloadable",
1161 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001162 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001163 vndk: {
1164 enabled: true,
1165 },
1166 double_loadable: true,
1167 shared_libs: ["libnondoubleloadable"],
1168 }
1169
1170 cc_library {
1171 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001172 vendor_available: true,
1173 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001174 vndk: {
1175 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001176 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001177 },
1178 double_loadable: true,
1179 }
1180 `)
1181 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1182 testCc(t, `
1183 cc_library {
1184 name: "libllndk",
1185 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001186 llndk: {
1187 symbol_file: "libllndk.map.txt",
1188 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001189 }
1190
1191 cc_library {
1192 name: "libcoreonly",
1193 shared_libs: ["libvendoravailable"],
1194 }
1195
1196 // indirect dependency of LLNDK
1197 cc_library {
1198 name: "libvendoravailable",
1199 vendor_available: true,
1200 double_loadable: true,
1201 }
1202 `)
1203}
1204
1205func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001206 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001207 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1208 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1209 cc_library {
1210 name: "libllndk",
1211 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001212 llndk: {
1213 symbol_file: "libllndk.map.txt",
1214 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001215 }
1216
1217 cc_library {
1218 name: "libnondoubleloadable",
1219 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001220 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 vndk: {
1222 enabled: true,
1223 },
1224 }
1225 `)
1226
1227 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1228 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1229 cc_library {
1230 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001231 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001232 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001233 llndk: {
1234 symbol_file: "libllndk.map.txt",
1235 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001236 }
1237
1238 cc_library {
1239 name: "libnondoubleloadable",
1240 vendor_available: true,
1241 }
1242 `)
1243
Jooyung Hana70f0672019-01-18 15:20:43 +09001244 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1245 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1246 cc_library {
1247 name: "libllndk",
1248 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001249 llndk: {
1250 symbol_file: "libllndk.map.txt",
1251 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001252 }
1253
1254 cc_library {
1255 name: "libcoreonly",
1256 shared_libs: ["libvendoravailable"],
1257 }
1258
1259 // indirect dependency of LLNDK
1260 cc_library {
1261 name: "libvendoravailable",
1262 vendor_available: true,
1263 }
1264 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001265
1266 // The error is not from 'client' but from 'libllndk'
1267 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1268 cc_library {
1269 name: "client",
1270 vendor_available: true,
1271 double_loadable: true,
1272 shared_libs: ["libllndk"],
1273 }
1274 cc_library {
1275 name: "libllndk",
1276 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001277 llndk: {
1278 symbol_file: "libllndk.map.txt",
1279 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001280 }
1281 cc_library {
1282 name: "libnondoubleloadable",
1283 vendor_available: true,
1284 }
1285 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001286}
1287
Jooyung Han479ca172020-10-19 18:51:07 +09001288func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001289 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001290 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1291 cc_library {
1292 name: "libvndksp",
1293 shared_libs: ["libanothervndksp"],
1294 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001295 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001296 vndk: {
1297 enabled: true,
1298 support_system_process: true,
1299 }
1300 }
1301
1302 cc_library {
1303 name: "libllndk",
1304 shared_libs: ["libanothervndksp"],
1305 }
1306
Jooyung Han479ca172020-10-19 18:51:07 +09001307 cc_library {
1308 name: "libanothervndksp",
1309 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001310 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001311 }
1312 `)
1313}
1314
Logan Chienf3511742017-10-31 18:04:35 +08001315func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001316 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001317 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001318 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001319 cc_library {
1320 name: "libvndk",
1321 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001322 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001323 vndk: {
1324 enabled: true,
1325 },
1326 nocrt: true,
1327 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001328 cc_library {
1329 name: "libvndk2",
1330 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001331 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001332 vndk: {
1333 enabled: true,
1334 },
1335 target: {
1336 vendor: {
1337 suffix: "-suffix",
1338 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001339 product: {
1340 suffix: "-suffix",
1341 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001342 },
1343 nocrt: true,
1344 }
Logan Chienf3511742017-10-31 18:04:35 +08001345
1346 cc_library {
1347 name: "libvndk_ext",
1348 vendor: true,
1349 vndk: {
1350 enabled: true,
1351 extends: "libvndk",
1352 },
1353 nocrt: true,
1354 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001355
1356 cc_library {
1357 name: "libvndk2_ext",
1358 vendor: true,
1359 vndk: {
1360 enabled: true,
1361 extends: "libvndk2",
1362 },
1363 nocrt: true,
1364 }
Logan Chienf3511742017-10-31 18:04:35 +08001365
Justin Yun0ecf0b22020-02-28 15:07:59 +09001366 cc_library {
1367 name: "libvndk_ext_product",
1368 product_specific: true,
1369 vndk: {
1370 enabled: true,
1371 extends: "libvndk",
1372 },
1373 nocrt: true,
1374 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001375
Justin Yun0ecf0b22020-02-28 15:07:59 +09001376 cc_library {
1377 name: "libvndk2_ext_product",
1378 product_specific: true,
1379 vndk: {
1380 enabled: true,
1381 extends: "libvndk2",
1382 },
1383 nocrt: true,
1384 }
1385 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001386 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001387 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001388 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001389
1390 ctx := testCcWithConfig(t, config)
1391
1392 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1393 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1394
1395 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1396 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1397
1398 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1399 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001400}
1401
Logan Chienf3511742017-10-31 18:04:35 +08001402func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001403 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001404 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001405 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001406 cc_library {
1407 name: "libvndk",
1408 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001409 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001410 vndk: {
1411 enabled: true,
1412 },
1413 nocrt: true,
1414 }
1415
1416 cc_library {
1417 name: "libvndk_ext",
1418 vndk: {
1419 enabled: true,
1420 extends: "libvndk",
1421 },
1422 nocrt: true,
1423 }
1424 `)
1425
1426 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1427 cc_library {
1428 name: "libvndk",
1429 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001430 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001431 vndk: {
1432 enabled: true,
1433 },
1434 nocrt: true,
1435 }
1436
1437 cc_library {
1438 name: "libvndk_ext",
1439 vendor: true,
1440 vndk: {
1441 enabled: true,
1442 },
1443 nocrt: true,
1444 }
1445 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001446
1447 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1448 cc_library {
1449 name: "libvndk",
1450 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001451 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001452 vndk: {
1453 enabled: true,
1454 },
1455 nocrt: true,
1456 }
1457
1458 cc_library {
1459 name: "libvndk_ext_product",
1460 product_specific: true,
1461 vndk: {
1462 enabled: true,
1463 },
1464 nocrt: true,
1465 }
1466 `)
1467
1468 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1469 cc_library {
1470 name: "libvndk",
1471 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001472 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001473 vndk: {
1474 enabled: true,
1475 },
1476 nocrt: true,
1477 }
1478
1479 cc_library {
1480 name: "libvndk_ext_product",
1481 product_specific: true,
1482 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001483 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001484 vndk: {
1485 enabled: true,
1486 extends: "libvndk",
1487 },
1488 nocrt: true,
1489 }
1490 `)
Logan Chienf3511742017-10-31 18:04:35 +08001491}
1492
1493func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001494 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001495 // This test ensures an error is emitted for inconsistent support_system_process.
1496 testCcError(t, "module \".*\" with mismatched support_system_process", `
1497 cc_library {
1498 name: "libvndk",
1499 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001500 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001501 vndk: {
1502 enabled: true,
1503 },
1504 nocrt: true,
1505 }
1506
1507 cc_library {
1508 name: "libvndk_sp_ext",
1509 vendor: true,
1510 vndk: {
1511 enabled: true,
1512 extends: "libvndk",
1513 support_system_process: true,
1514 },
1515 nocrt: true,
1516 }
1517 `)
1518
1519 testCcError(t, "module \".*\" with mismatched support_system_process", `
1520 cc_library {
1521 name: "libvndk_sp",
1522 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001523 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001524 vndk: {
1525 enabled: true,
1526 support_system_process: true,
1527 },
1528 nocrt: true,
1529 }
1530
1531 cc_library {
1532 name: "libvndk_ext",
1533 vendor: true,
1534 vndk: {
1535 enabled: true,
1536 extends: "libvndk_sp",
1537 },
1538 nocrt: true,
1539 }
1540 `)
1541}
1542
1543func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001544 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001545 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001546 // with `private: true`.
1547 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001548 cc_library {
1549 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001550 vendor_available: true,
1551 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001552 vndk: {
1553 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001554 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001555 },
1556 nocrt: true,
1557 }
1558
1559 cc_library {
1560 name: "libvndk_ext",
1561 vendor: true,
1562 vndk: {
1563 enabled: true,
1564 extends: "libvndk",
1565 },
1566 nocrt: true,
1567 }
1568 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001569
Justin Yunfd9e8042020-12-23 18:23:14 +09001570 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001571 cc_library {
1572 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001573 vendor_available: true,
1574 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001575 vndk: {
1576 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001577 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001578 },
1579 nocrt: true,
1580 }
1581
1582 cc_library {
1583 name: "libvndk_ext_product",
1584 product_specific: true,
1585 vndk: {
1586 enabled: true,
1587 extends: "libvndk",
1588 },
1589 nocrt: true,
1590 }
1591 `)
Logan Chienf3511742017-10-31 18:04:35 +08001592}
1593
Logan Chiend3c59a22018-03-29 14:08:15 +08001594func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001595 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001596 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001597 testCc(t, `
1598 cc_library {
1599 name: "libvndk",
1600 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001601 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001602 vndk: {
1603 enabled: true,
1604 },
1605 nocrt: true,
1606 }
1607
1608 cc_library {
1609 name: "libvndk_ext",
1610 vendor: true,
1611 vndk: {
1612 enabled: true,
1613 extends: "libvndk",
1614 },
1615 nocrt: true,
1616 }
1617
1618 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001619 name: "libvndk_sp",
1620 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001621 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001622 vndk: {
1623 enabled: true,
1624 support_system_process: true,
1625 },
1626 nocrt: true,
1627 }
1628
1629 cc_library {
1630 name: "libvndk_sp_ext",
1631 vendor: true,
1632 vndk: {
1633 enabled: true,
1634 extends: "libvndk_sp",
1635 support_system_process: true,
1636 },
1637 nocrt: true,
1638 }
1639
1640 cc_library {
1641 name: "libvendor",
1642 vendor: true,
1643 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1644 nocrt: true,
1645 }
1646 `)
1647}
1648
Logan Chiend3c59a22018-03-29 14:08:15 +08001649func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001650 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001651 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001652 testCc(t, `
1653 cc_library {
1654 name: "libvndk",
1655 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001656 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001657 vndk: {
1658 enabled: true,
1659 },
1660 nocrt: true,
1661 }
1662
1663 cc_library {
1664 name: "libvndk_ext",
1665 vendor: true,
1666 vndk: {
1667 enabled: true,
1668 extends: "libvndk",
1669 },
1670 shared_libs: ["libvendor"],
1671 nocrt: true,
1672 }
1673
1674 cc_library {
1675 name: "libvendor",
1676 vendor: true,
1677 nocrt: true,
1678 }
1679 `)
Logan Chienf3511742017-10-31 18:04:35 +08001680
Logan Chiend3c59a22018-03-29 14:08:15 +08001681 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1682 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001683 cc_library {
1684 name: "libvndk_sp",
1685 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001686 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001687 vndk: {
1688 enabled: true,
1689 support_system_process: true,
1690 },
1691 nocrt: true,
1692 }
1693
1694 cc_library {
1695 name: "libvndk_sp_ext",
1696 vendor: true,
1697 vndk: {
1698 enabled: true,
1699 extends: "libvndk_sp",
1700 support_system_process: true,
1701 },
1702 shared_libs: ["libvendor"], // Cause an error
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvendor",
1708 vendor: true,
1709 nocrt: true,
1710 }
1711 `)
1712}
1713
Justin Yun0ecf0b22020-02-28 15:07:59 +09001714func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001715 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716 bp := `
1717 cc_library {
1718 name: "libvndk",
1719 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001720 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001721 vndk: {
1722 enabled: true,
1723 },
1724 nocrt: true,
1725 }
1726
1727 cc_library {
1728 name: "libvndk_ext_product",
1729 product_specific: true,
1730 vndk: {
1731 enabled: true,
1732 extends: "libvndk",
1733 },
1734 shared_libs: ["libproduct_for_vndklibs"],
1735 nocrt: true,
1736 }
1737
1738 cc_library {
1739 name: "libvndk_sp",
1740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001741 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001742 vndk: {
1743 enabled: true,
1744 support_system_process: true,
1745 },
1746 nocrt: true,
1747 }
1748
1749 cc_library {
1750 name: "libvndk_sp_ext_product",
1751 product_specific: true,
1752 vndk: {
1753 enabled: true,
1754 extends: "libvndk_sp",
1755 support_system_process: true,
1756 },
1757 shared_libs: ["libproduct_for_vndklibs"],
1758 nocrt: true,
1759 }
1760
1761 cc_library {
1762 name: "libproduct",
1763 product_specific: true,
1764 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1765 nocrt: true,
1766 }
1767
1768 cc_library {
1769 name: "libproduct_for_vndklibs",
1770 product_specific: true,
1771 nocrt: true,
1772 }
1773 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001774 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001775 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001776 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001777
1778 testCcWithConfig(t, config)
1779}
1780
Logan Chiend3c59a22018-03-29 14:08:15 +08001781func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001782 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001783 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1784 // library.
1785 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1786 cc_library {
1787 name: "libvndk",
1788 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001789 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001790 vndk: {
1791 enabled: true,
1792 },
1793 nocrt: true,
1794 }
1795
1796 cc_library {
1797 name: "libvndk_sp",
1798 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001799 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001800 vndk: {
1801 enabled: true,
1802 support_system_process: true,
1803 },
1804 nocrt: true,
1805 }
1806
1807 cc_library {
1808 name: "libvndk_sp_ext",
1809 vendor: true,
1810 vndk: {
1811 enabled: true,
1812 extends: "libvndk_sp",
1813 support_system_process: true,
1814 },
1815 shared_libs: ["libvndk"], // Cause an error
1816 nocrt: true,
1817 }
1818 `)
1819
1820 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1821 // library.
1822 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1823 cc_library {
1824 name: "libvndk",
1825 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001826 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001827 vndk: {
1828 enabled: true,
1829 },
1830 nocrt: true,
1831 }
1832
1833 cc_library {
1834 name: "libvndk_ext",
1835 vendor: true,
1836 vndk: {
1837 enabled: true,
1838 extends: "libvndk",
1839 },
1840 nocrt: true,
1841 }
1842
1843 cc_library {
1844 name: "libvndk_sp",
1845 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001846 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001847 vndk: {
1848 enabled: true,
1849 support_system_process: true,
1850 },
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk_sp_ext",
1856 vendor: true,
1857 vndk: {
1858 enabled: true,
1859 extends: "libvndk_sp",
1860 support_system_process: true,
1861 },
1862 shared_libs: ["libvndk_ext"], // Cause an error
1863 nocrt: true,
1864 }
1865 `)
1866}
1867
1868func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001869 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001870 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1871 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001872 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1873 cc_library {
1874 name: "libvndk",
1875 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001876 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001877 vndk: {
1878 enabled: true,
1879 },
1880 nocrt: true,
1881 }
1882
1883 cc_library {
1884 name: "libvndk_ext",
1885 vendor: true,
1886 vndk: {
1887 enabled: true,
1888 extends: "libvndk",
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk2",
1895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001896 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001897 vndk: {
1898 enabled: true,
1899 },
1900 shared_libs: ["libvndk_ext"],
1901 nocrt: true,
1902 }
1903 `)
1904
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001905 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001906 cc_library {
1907 name: "libvndk",
1908 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001909 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001910 vndk: {
1911 enabled: true,
1912 },
1913 nocrt: true,
1914 }
1915
1916 cc_library {
1917 name: "libvndk_ext",
1918 vendor: true,
1919 vndk: {
1920 enabled: true,
1921 extends: "libvndk",
1922 },
1923 nocrt: true,
1924 }
1925
1926 cc_library {
1927 name: "libvndk2",
1928 vendor_available: true,
1929 vndk: {
1930 enabled: true,
1931 },
1932 target: {
1933 vendor: {
1934 shared_libs: ["libvndk_ext"],
1935 },
1936 },
1937 nocrt: true,
1938 }
1939 `)
1940
1941 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1942 cc_library {
1943 name: "libvndk_sp",
1944 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001945 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001946 vndk: {
1947 enabled: true,
1948 support_system_process: true,
1949 },
1950 nocrt: true,
1951 }
1952
1953 cc_library {
1954 name: "libvndk_sp_ext",
1955 vendor: true,
1956 vndk: {
1957 enabled: true,
1958 extends: "libvndk_sp",
1959 support_system_process: true,
1960 },
1961 nocrt: true,
1962 }
1963
1964 cc_library {
1965 name: "libvndk_sp_2",
1966 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001967 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001968 vndk: {
1969 enabled: true,
1970 support_system_process: true,
1971 },
1972 shared_libs: ["libvndk_sp_ext"],
1973 nocrt: true,
1974 }
1975 `)
1976
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001977 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001978 cc_library {
1979 name: "libvndk_sp",
1980 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001981 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001982 vndk: {
1983 enabled: true,
1984 },
1985 nocrt: true,
1986 }
1987
1988 cc_library {
1989 name: "libvndk_sp_ext",
1990 vendor: true,
1991 vndk: {
1992 enabled: true,
1993 extends: "libvndk_sp",
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp2",
2000 vendor_available: true,
2001 vndk: {
2002 enabled: true,
2003 },
2004 target: {
2005 vendor: {
2006 shared_libs: ["libvndk_sp_ext"],
2007 },
2008 },
2009 nocrt: true,
2010 }
2011 `)
2012}
2013
Justin Yun5f7f7e82019-11-18 19:52:14 +09002014func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002015 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002016 bp := `
2017 cc_library {
2018 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002019 llndk: {
2020 symbol_file: "libllndk.map.txt",
2021 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002022 }
2023 cc_library {
2024 name: "libvndk",
2025 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002026 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002027 vndk: {
2028 enabled: true,
2029 },
2030 nocrt: true,
2031 }
2032 cc_library {
2033 name: "libvndk_sp",
2034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002035 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002036 vndk: {
2037 enabled: true,
2038 support_system_process: true,
2039 },
2040 nocrt: true,
2041 }
2042 cc_library {
2043 name: "libva",
2044 vendor_available: true,
2045 nocrt: true,
2046 }
2047 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002048 name: "libpa",
2049 product_available: true,
2050 nocrt: true,
2051 }
2052 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002053 name: "libboth_available",
2054 vendor_available: true,
2055 product_available: true,
2056 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002057 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002058 target: {
2059 vendor: {
2060 suffix: "-vendor",
2061 },
2062 product: {
2063 suffix: "-product",
2064 },
2065 }
2066 }
2067 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002068 name: "libproduct_va",
2069 product_specific: true,
2070 vendor_available: true,
2071 nocrt: true,
2072 }
2073 cc_library {
2074 name: "libprod",
2075 product_specific: true,
2076 shared_libs: [
2077 "libllndk",
2078 "libvndk",
2079 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002080 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002081 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002082 "libproduct_va",
2083 ],
2084 nocrt: true,
2085 }
2086 cc_library {
2087 name: "libvendor",
2088 vendor: true,
2089 shared_libs: [
2090 "libllndk",
2091 "libvndk",
2092 "libvndk_sp",
2093 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002094 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002095 "libproduct_va",
2096 ],
2097 nocrt: true,
2098 }
2099 `
2100
Paul Duffin8567f222021-03-23 00:02:06 +00002101 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002102
Jooyung Han261e1582020-10-20 18:54:21 +09002103 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2104 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002105
2106 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2107 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2108
2109 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2110 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002111
2112 ensureStringContains := func(t *testing.T, str string, substr string) {
2113 t.Helper()
2114 if !strings.Contains(str, substr) {
2115 t.Errorf("%q is not found in %v", substr, str)
2116 }
2117 }
2118 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2119 t.Helper()
2120 if strings.Contains(str, substr) {
2121 t.Errorf("%q is found in %v", substr, str)
2122 }
2123 }
2124
2125 // _static variant is used since _shared reuses *.o from the static variant
2126 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2127 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2128
2129 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2130 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2131 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2132 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002133 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun13decfb2021-03-08 19:25:55 +09002134
2135 product_cflags := product_static.Rule("cc").Args["cFlags"]
2136 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2137 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2138 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002139 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002140}
2141
2142func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002143 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002144 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002145 cc_library {
2146 name: "libprod",
2147 product_specific: true,
2148 shared_libs: [
2149 "libvendor",
2150 ],
2151 nocrt: true,
2152 }
2153 cc_library {
2154 name: "libvendor",
2155 vendor: true,
2156 nocrt: true,
2157 }
2158 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002159 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002160 cc_library {
2161 name: "libprod",
2162 product_specific: true,
2163 shared_libs: [
2164 "libsystem",
2165 ],
2166 nocrt: true,
2167 }
2168 cc_library {
2169 name: "libsystem",
2170 nocrt: true,
2171 }
2172 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002173 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002174 cc_library {
2175 name: "libprod",
2176 product_specific: true,
2177 shared_libs: [
2178 "libva",
2179 ],
2180 nocrt: true,
2181 }
2182 cc_library {
2183 name: "libva",
2184 vendor_available: true,
2185 nocrt: true,
2186 }
2187 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002188 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002189 cc_library {
2190 name: "libprod",
2191 product_specific: true,
2192 shared_libs: [
2193 "libvndk_private",
2194 ],
2195 nocrt: true,
2196 }
2197 cc_library {
2198 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002199 vendor_available: true,
2200 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002201 vndk: {
2202 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002203 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002204 },
2205 nocrt: true,
2206 }
2207 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002208 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002209 cc_library {
2210 name: "libprod",
2211 product_specific: true,
2212 shared_libs: [
2213 "libsystem_ext",
2214 ],
2215 nocrt: true,
2216 }
2217 cc_library {
2218 name: "libsystem_ext",
2219 system_ext_specific: true,
2220 nocrt: true,
2221 }
2222 `)
2223 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2224 cc_library {
2225 name: "libsystem",
2226 shared_libs: [
2227 "libproduct_va",
2228 ],
2229 nocrt: true,
2230 }
2231 cc_library {
2232 name: "libproduct_va",
2233 product_specific: true,
2234 vendor_available: true,
2235 nocrt: true,
2236 }
2237 `)
2238}
2239
Jooyung Han38002912019-05-16 04:01:54 +09002240func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002241 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002242 bp := `
2243 cc_library {
2244 name: "libvndk",
2245 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002246 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002247 vndk: {
2248 enabled: true,
2249 },
2250 }
2251 cc_library {
2252 name: "libvndksp",
2253 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002254 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002255 vndk: {
2256 enabled: true,
2257 support_system_process: true,
2258 },
2259 }
2260 cc_library {
2261 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002262 vendor_available: true,
2263 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002264 vndk: {
2265 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002266 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002267 },
2268 }
2269 cc_library {
2270 name: "libvendor",
2271 vendor: true,
2272 }
2273 cc_library {
2274 name: "libvndkext",
2275 vendor: true,
2276 vndk: {
2277 enabled: true,
2278 extends: "libvndk",
2279 },
2280 }
2281 vndk_prebuilt_shared {
2282 name: "prevndk",
2283 version: "27",
2284 target_arch: "arm",
2285 binder32bit: true,
2286 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002287 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002288 vndk: {
2289 enabled: true,
2290 },
2291 arch: {
2292 arm: {
2293 srcs: ["liba.so"],
2294 },
2295 },
2296 }
2297 cc_library {
2298 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002299 llndk: {
2300 symbol_file: "libllndk.map.txt",
2301 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002302 }
2303 cc_library {
2304 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002305 llndk: {
2306 symbol_file: "libllndkprivate.map.txt",
2307 private: true,
2308 }
Colin Cross78212242021-01-06 14:51:30 -08002309 }
2310
2311 llndk_libraries_txt {
2312 name: "llndk.libraries.txt",
2313 }
2314 vndkcore_libraries_txt {
2315 name: "vndkcore.libraries.txt",
2316 }
2317 vndksp_libraries_txt {
2318 name: "vndksp.libraries.txt",
2319 }
2320 vndkprivate_libraries_txt {
2321 name: "vndkprivate.libraries.txt",
2322 }
2323 vndkcorevariant_libraries_txt {
2324 name: "vndkcorevariant.libraries.txt",
2325 insert_vndk_version: false,
2326 }
2327 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002328
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002329 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002330 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002331 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002332 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002333 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002334
Colin Cross78212242021-01-06 14:51:30 -08002335 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2336 []string{"libvndk.so", "libvndkprivate.so"})
2337 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2338 []string{"libc++.so", "libvndksp.so"})
2339 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2340 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2341 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2342 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002343
Colin Crossfb0c16e2019-11-20 17:12:35 -08002344 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002345
Jooyung Han38002912019-05-16 04:01:54 +09002346 tests := []struct {
2347 variant string
2348 name string
2349 expected string
2350 }{
2351 {vendorVariant, "libvndk", "native:vndk"},
2352 {vendorVariant, "libvndksp", "native:vndk"},
2353 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2354 {vendorVariant, "libvendor", "native:vendor"},
2355 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002356 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002357 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002358 {coreVariant, "libvndk", "native:platform"},
2359 {coreVariant, "libvndkprivate", "native:platform"},
2360 {coreVariant, "libllndk", "native:platform"},
2361 }
2362 for _, test := range tests {
2363 t.Run(test.name, func(t *testing.T) {
2364 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2365 assertString(t, module.makeLinkType, test.expected)
2366 })
2367 }
2368}
2369
Jeff Gaston294356f2017-09-27 17:05:30 -07002370var staticLinkDepOrderTestCases = []struct {
2371 // This is a string representation of a map[moduleName][]moduleDependency .
2372 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002373 inStatic string
2374
2375 // This is a string representation of a map[moduleName][]moduleDependency .
2376 // It models the dependencies declared in an Android.bp file.
2377 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002378
2379 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2380 // The keys of allOrdered specify which modules we would like to check.
2381 // The values of allOrdered specify the expected result (of the transitive closure of all
2382 // dependencies) for each module to test
2383 allOrdered string
2384
2385 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2386 // The keys of outOrdered specify which modules we would like to check.
2387 // The values of outOrdered specify the expected result (of the ordered linker command line)
2388 // for each module to test.
2389 outOrdered string
2390}{
2391 // Simple tests
2392 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002393 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002394 outOrdered: "",
2395 },
2396 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002397 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002398 outOrdered: "a:",
2399 },
2400 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002401 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002402 outOrdered: "a:b; b:",
2403 },
2404 // Tests of reordering
2405 {
2406 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002407 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002408 outOrdered: "a:b,c,d; b:d; c:d; d:",
2409 },
2410 {
2411 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002412 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002413 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2414 },
2415 {
2416 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002417 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002418 outOrdered: "a:d,b,e,c; d:b; e:c",
2419 },
2420 {
2421 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002422 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002423 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2424 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2425 },
2426 {
2427 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002428 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 -07002429 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2430 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2431 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002432 // shared dependencies
2433 {
2434 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2435 // So, we don't actually have to check that a shared dependency of c will change the order
2436 // of a library that depends statically on b and on c. We only need to check that if c has
2437 // a shared dependency on b, that that shows up in allOrdered.
2438 inShared: "c:b",
2439 allOrdered: "c:b",
2440 outOrdered: "c:",
2441 },
2442 {
2443 // This test doesn't actually include any shared dependencies but it's a reminder of what
2444 // the second phase of the above test would look like
2445 inStatic: "a:b,c; c:b",
2446 allOrdered: "a:c,b; c:b",
2447 outOrdered: "a:c,b; c:b",
2448 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002449 // tiebreakers for when two modules specifying different orderings and there is no dependency
2450 // to dictate an order
2451 {
2452 // 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 -08002453 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002454 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2455 },
2456 {
2457 // 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 -08002458 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 -07002459 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2460 },
2461 // Tests involving duplicate dependencies
2462 {
2463 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002464 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002465 outOrdered: "a:c,b",
2466 },
2467 {
2468 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002469 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002470 outOrdered: "a:d,c,b",
2471 },
2472 // Tests to confirm the nonexistence of infinite loops.
2473 // These cases should never happen, so as long as the test terminates and the
2474 // result is deterministic then that should be fine.
2475 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002476 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002477 outOrdered: "a:a",
2478 },
2479 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002480 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002481 allOrdered: "a:b,c; b:c,a; c:a,b",
2482 outOrdered: "a:b; b:c; c:a",
2483 },
2484 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002485 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002486 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2487 outOrdered: "a:c,b; b:a,c; c:b,a",
2488 },
2489}
2490
2491// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2492func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2493 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2494 strippedText := strings.Replace(text, " ", "", -1)
2495 if len(strippedText) < 1 {
2496 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2497 }
2498 allDeps = make(map[android.Path][]android.Path, 0)
2499
2500 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2501 moduleTexts := strings.Split(strippedText, ";")
2502
2503 outputForModuleName := func(moduleName string) android.Path {
2504 return android.PathForTesting(moduleName)
2505 }
2506
2507 for _, moduleText := range moduleTexts {
2508 // convert from "a:b,c" to ["a", "b,c"]
2509 components := strings.Split(moduleText, ":")
2510 if len(components) != 2 {
2511 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2512 }
2513 moduleName := components[0]
2514 moduleOutput := outputForModuleName(moduleName)
2515 modulesInOrder = append(modulesInOrder, moduleOutput)
2516
2517 depString := components[1]
2518 // convert from "b,c" to ["b", "c"]
2519 depNames := strings.Split(depString, ",")
2520 if len(depString) < 1 {
2521 depNames = []string{}
2522 }
2523 var deps []android.Path
2524 for _, depName := range depNames {
2525 deps = append(deps, outputForModuleName(depName))
2526 }
2527 allDeps[moduleOutput] = deps
2528 }
2529 return modulesInOrder, allDeps
2530}
2531
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002532func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002533 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002534 ctx := testCc(t, `
2535 cc_library {
2536 name: "a",
2537 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002538 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002539 }
2540 cc_library {
2541 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002542 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002543 }
2544 cc_library {
2545 name: "c",
2546 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002547 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002548 }
2549 cc_library {
2550 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002551 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 }
2553
2554 `)
2555
Colin Cross7113d202019-11-20 16:39:12 -08002556 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002557 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross5a377182023-12-14 14:46:23 -08002558 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2559 actual := android.Paths(staticLibInfo.TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002560 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002561
2562 if !reflect.DeepEqual(actual, expected) {
2563 t.Errorf("staticDeps orderings were not propagated correctly"+
2564 "\nactual: %v"+
2565 "\nexpected: %v",
2566 actual,
2567 expected,
2568 )
2569 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002570}
Jeff Gaston294356f2017-09-27 17:05:30 -07002571
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002572func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002573 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002574 ctx := testCc(t, `
2575 cc_library {
2576 name: "a",
2577 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002578 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002579 }
2580 cc_library {
2581 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002582 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002583 }
2584 cc_library {
2585 name: "c",
2586 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002587 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002588 }
2589
2590 `)
2591
Colin Cross7113d202019-11-20 16:39:12 -08002592 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002593 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross5a377182023-12-14 14:46:23 -08002594 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2595 actual := android.Paths(staticLibInfo.TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002596 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002597
2598 if !reflect.DeepEqual(actual, expected) {
2599 t.Errorf("staticDeps orderings did not account for shared libs"+
2600 "\nactual: %v"+
2601 "\nexpected: %v",
2602 actual,
2603 expected,
2604 )
2605 }
2606}
2607
Jooyung Hanb04a4992020-03-13 18:57:35 +09002608func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002609 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002610 if !reflect.DeepEqual(actual, expected) {
2611 t.Errorf(message+
2612 "\nactual: %v"+
2613 "\nexpected: %v",
2614 actual,
2615 expected,
2616 )
2617 }
2618}
2619
Jooyung Han61b66e92020-03-21 14:21:46 +00002620func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002621 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002622 result := prepareForCcTest.RunTestWithBp(t, `
2623 cc_library {
2624 name: "libllndk",
2625 stubs: { versions: ["1", "2"] },
2626 llndk: {
2627 symbol_file: "libllndk.map.txt",
2628 },
2629 export_include_dirs: ["include"],
2630 }
2631
2632 cc_prebuilt_library_shared {
2633 name: "libllndkprebuilt",
2634 stubs: { versions: ["1", "2"] },
2635 llndk: {
2636 symbol_file: "libllndkprebuilt.map.txt",
2637 },
2638 }
2639
2640 cc_library {
2641 name: "libllndk_with_external_headers",
2642 stubs: { versions: ["1", "2"] },
2643 llndk: {
2644 symbol_file: "libllndk.map.txt",
2645 export_llndk_headers: ["libexternal_llndk_headers"],
2646 },
2647 header_libs: ["libexternal_headers"],
2648 export_header_lib_headers: ["libexternal_headers"],
2649 }
2650 cc_library_headers {
2651 name: "libexternal_headers",
2652 export_include_dirs: ["include"],
2653 vendor_available: true,
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09002654 product_available: true,
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002655 }
2656 cc_library_headers {
2657 name: "libexternal_llndk_headers",
2658 export_include_dirs: ["include_llndk"],
2659 llndk: {
2660 symbol_file: "libllndk.map.txt",
2661 },
2662 vendor_available: true,
2663 }
2664
2665 cc_library {
2666 name: "libllndk_with_override_headers",
2667 stubs: { versions: ["1", "2"] },
2668 llndk: {
2669 symbol_file: "libllndk.map.txt",
2670 override_export_include_dirs: ["include_llndk"],
2671 },
2672 export_include_dirs: ["include"],
2673 }
2674 `)
2675 actual := result.ModuleVariantsForTests("libllndk")
2676 for i := 0; i < len(actual); i++ {
2677 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2678 actual = append(actual[:i], actual[i+1:]...)
2679 i--
2680 }
2681 }
2682 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002683 "android_vendor.29_arm64_armv8-a_shared_current",
2684 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002685 "android_vendor.29_arm_armv7-a-neon_shared_current",
2686 "android_vendor.29_arm_armv7-a-neon_shared",
2687 }
2688 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2689
2690 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han33eb6152024-03-11 15:46:48 +09002691 android.AssertSame(t, "use Vendor API level for default stubs", "202404", params.Args["apiLevel"])
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002692
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002693 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2694 t.Helper()
2695 m := result.ModuleForTests(module, variant).Module()
Colin Cross5a377182023-12-14 14:46:23 -08002696 f, _ := android.SingletonModuleProvider(result, m, FlagExporterInfoProvider)
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002697 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2698 expectedDirs, f.IncludeDirs)
2699 }
2700
2701 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2702 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2703 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2704 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2705 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2706 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2707}
2708
Jiyong Parka46a4d52017-12-14 19:54:34 +09002709func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002710 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002711 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002712 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002713 name: "libllndk_headers",
2714 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002715 llndk: {
2716 llndk_headers: true,
2717 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002718 }
2719 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002720 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002721 llndk: {
2722 symbol_file: "libllndk.map.txt",
2723 export_llndk_headers: ["libllndk_headers"],
2724 }
Colin Cross0477b422020-10-13 18:43:54 -07002725 }
2726
2727 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002728 name: "libvendor",
2729 shared_libs: ["libllndk"],
2730 vendor: true,
2731 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002732 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002733 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002734 }
2735 `)
2736
2737 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002738 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002739 cflags := cc.Args["cFlags"]
2740 if !strings.Contains(cflags, "-Imy_include") {
2741 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2742 }
2743}
2744
Logan Chien43d34c32017-12-20 01:17:32 +08002745func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2746 actual := module.Properties.AndroidMkRuntimeLibs
2747 if !reflect.DeepEqual(actual, expected) {
2748 t.Errorf("incorrect runtime_libs for shared libs"+
2749 "\nactual: %v"+
2750 "\nexpected: %v",
2751 actual,
2752 expected,
2753 )
2754 }
2755}
2756
2757const runtimeLibAndroidBp = `
2758 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002759 name: "liball_available",
2760 vendor_available: true,
2761 product_available: true,
2762 no_libcrt : true,
2763 nocrt : true,
2764 system_shared_libs : [],
2765 }
2766 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002767 name: "libvendor_available1",
2768 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002769 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002770 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002771 nocrt : true,
2772 system_shared_libs : [],
2773 }
2774 cc_library {
2775 name: "libvendor_available2",
2776 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002777 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002778 target: {
2779 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002780 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002781 }
2782 },
Yi Konge7fe9912019-06-02 00:53:50 -07002783 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002784 nocrt : true,
2785 system_shared_libs : [],
2786 }
2787 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002788 name: "libproduct_vendor",
2789 product_specific: true,
2790 vendor_available: true,
2791 no_libcrt : true,
2792 nocrt : true,
2793 system_shared_libs : [],
2794 }
2795 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002796 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002797 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002798 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002799 nocrt : true,
2800 system_shared_libs : [],
2801 }
2802 cc_library {
2803 name: "libvendor1",
2804 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002805 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002806 nocrt : true,
2807 system_shared_libs : [],
2808 }
2809 cc_library {
2810 name: "libvendor2",
2811 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002812 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002813 no_libcrt : true,
2814 nocrt : true,
2815 system_shared_libs : [],
2816 }
2817 cc_library {
2818 name: "libproduct_available1",
2819 product_available: true,
2820 runtime_libs: ["liball_available"],
2821 no_libcrt : true,
2822 nocrt : true,
2823 system_shared_libs : [],
2824 }
2825 cc_library {
2826 name: "libproduct1",
2827 product_specific: true,
2828 no_libcrt : true,
2829 nocrt : true,
2830 system_shared_libs : [],
2831 }
2832 cc_library {
2833 name: "libproduct2",
2834 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002835 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002836 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002837 nocrt : true,
2838 system_shared_libs : [],
2839 }
2840`
2841
2842func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002843 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002844 ctx := testCc(t, runtimeLibAndroidBp)
2845
2846 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002847 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002848
Justin Yun8a2600c2020-12-07 12:44:03 +09002849 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2850 checkRuntimeLibs(t, []string{"liball_available"}, module)
2851
2852 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2853 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002854
2855 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002856 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002857
2858 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2859 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002860 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002861
Justin Yun8a2600c2020-12-07 12:44:03 +09002862 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2863 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002864
2865 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002866 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002867
2868 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2869 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002870 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002871
2872 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2873 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2874
2875 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002876 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002877}
2878
2879func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002880 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002881 ctx := testCc(t, runtimeLibAndroidBp)
2882
Colin Cross7113d202019-11-20 16:39:12 -08002883 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002884 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2885 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002886
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002887 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002888 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002889 checkRuntimeLibs(t, nil, module)
2890}
2891
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002892func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002893 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002894 actual := module.Properties.AndroidMkStaticLibs
2895 if !reflect.DeepEqual(actual, expected) {
2896 t.Errorf("incorrect static_libs"+
2897 "\nactual: %v"+
2898 "\nexpected: %v",
2899 actual,
2900 expected,
2901 )
2902 }
2903}
2904
2905const staticLibAndroidBp = `
2906 cc_library {
2907 name: "lib1",
2908 }
2909 cc_library {
2910 name: "lib2",
2911 static_libs: ["lib1"],
2912 }
2913`
2914
2915func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002916 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002917 ctx := testCc(t, staticLibAndroidBp)
2918
2919 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002920 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002921 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002922 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002923
2924 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002925 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002926 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2927 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002928 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002929}
2930
Jiyong Parkd08b6972017-09-26 10:50:54 +09002931var compilerFlagsTestCases = []struct {
2932 in string
2933 out bool
2934}{
2935 {
2936 in: "a",
2937 out: false,
2938 },
2939 {
2940 in: "-a",
2941 out: true,
2942 },
2943 {
2944 in: "-Ipath/to/something",
2945 out: false,
2946 },
2947 {
2948 in: "-isystempath/to/something",
2949 out: false,
2950 },
2951 {
2952 in: "--coverage",
2953 out: false,
2954 },
2955 {
2956 in: "-include a/b",
2957 out: true,
2958 },
2959 {
2960 in: "-include a/b c/d",
2961 out: false,
2962 },
2963 {
2964 in: "-DMACRO",
2965 out: true,
2966 },
2967 {
2968 in: "-DMAC RO",
2969 out: false,
2970 },
2971 {
2972 in: "-a -b",
2973 out: false,
2974 },
2975 {
2976 in: "-DMACRO=definition",
2977 out: true,
2978 },
2979 {
2980 in: "-DMACRO=defi nition",
2981 out: true, // TODO(jiyong): this should be false
2982 },
2983 {
2984 in: "-DMACRO(x)=x + 1",
2985 out: true,
2986 },
2987 {
2988 in: "-DMACRO=\"defi nition\"",
2989 out: true,
2990 },
2991}
2992
2993type mockContext struct {
2994 BaseModuleContext
2995 result bool
2996}
2997
2998func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2999 // CheckBadCompilerFlags calls this function when the flag should be rejected
3000 ctx.result = false
3001}
3002
3003func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003004 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003005 for _, testCase := range compilerFlagsTestCases {
3006 ctx := &mockContext{result: true}
3007 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3008 if ctx.result != testCase.out {
3009 t.Errorf("incorrect output:")
3010 t.Errorf(" input: %#v", testCase.in)
3011 t.Errorf(" expected: %#v", testCase.out)
3012 t.Errorf(" got: %#v", ctx.result)
3013 }
3014 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003015}
Jiyong Park374510b2018-03-19 18:23:01 +09003016
Jiyong Park37b25202018-07-11 10:49:27 +09003017func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003018 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003019 ctx := testCc(t, `
3020 cc_library_shared {
3021 name: "librecovery",
3022 recovery: true,
3023 }
3024 cc_library_shared {
3025 name: "librecovery32",
3026 recovery: true,
3027 compile_multilib:"32",
3028 }
Jiyong Park5baac542018-08-28 09:55:37 +09003029 cc_library_shared {
3030 name: "libHalInRecovery",
3031 recovery_available: true,
3032 vendor: true,
3033 }
Jiyong Park37b25202018-07-11 10:49:27 +09003034 `)
3035
3036 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003037 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003038 if len(variants) != 1 || !android.InList(arm64, variants) {
3039 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3040 }
3041
3042 variants = ctx.ModuleVariantsForTests("librecovery32")
3043 if android.InList(arm64, variants) {
3044 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3045 }
Jiyong Park5baac542018-08-28 09:55:37 +09003046
3047 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3048 if !recoveryModule.Platform() {
3049 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3050 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003051}
Jiyong Park5baac542018-08-28 09:55:37 +09003052
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003053func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003054 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003055 bp := `
3056 cc_prebuilt_test_library_shared {
3057 name: "test_lib",
3058 relative_install_path: "foo/bar/baz",
3059 srcs: ["srcpath/dontusethispath/baz.so"],
3060 }
3061
3062 cc_test {
3063 name: "main_test",
3064 data_libs: ["test_lib"],
3065 gtest: false,
3066 }
3067 `
3068
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003069 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003070 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003071 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003072 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3073
3074 ctx := testCcWithConfig(t, config)
3075 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3076 testBinary := module.(*Module).linker.(*testBinary)
3077 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3078 if err != nil {
3079 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3080 }
3081 if len(outputFiles) != 1 {
3082 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3083 }
3084 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003085 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003086 }
3087
3088 outputPath := outputFiles[0].String()
3089
3090 if !strings.HasSuffix(outputPath, "/main_test") {
3091 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3092 }
Colin Crossaa255532020-07-03 13:18:24 -07003093 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003094 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3095 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3096 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3097 }
3098}
3099
Jiyong Park7ed9de32018-10-15 22:25:07 +09003100func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003101 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003102 ctx := testCc(t, `
3103 cc_library_shared {
3104 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003105 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003106 stubs: {
3107 symbol_file: "foo.map.txt",
3108 versions: ["1", "2", "3"],
3109 },
3110 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003111
Jiyong Park7ed9de32018-10-15 22:25:07 +09003112 cc_library_shared {
3113 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003114 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003115 shared_libs: ["libFoo#1"],
3116 }`)
3117
3118 variants := ctx.ModuleVariantsForTests("libFoo")
3119 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003120 "android_arm64_armv8-a_shared",
3121 "android_arm64_armv8-a_shared_1",
3122 "android_arm64_armv8-a_shared_2",
3123 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003124 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003125 "android_arm_armv7-a-neon_shared",
3126 "android_arm_armv7-a-neon_shared_1",
3127 "android_arm_armv7-a-neon_shared_2",
3128 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003129 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003130 }
3131 variantsMismatch := false
3132 if len(variants) != len(expectedVariants) {
3133 variantsMismatch = true
3134 } else {
3135 for _, v := range expectedVariants {
3136 if !inList(v, variants) {
3137 variantsMismatch = false
3138 }
3139 }
3140 }
3141 if variantsMismatch {
3142 t.Errorf("variants of libFoo expected:\n")
3143 for _, v := range expectedVariants {
3144 t.Errorf("%q\n", v)
3145 }
3146 t.Errorf(", but got:\n")
3147 for _, v := range variants {
3148 t.Errorf("%q\n", v)
3149 }
3150 }
3151
Colin Cross7113d202019-11-20 16:39:12 -08003152 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003153 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003154 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003155 if !strings.Contains(libFlags, libFoo1StubPath) {
3156 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3157 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003158
Colin Cross7113d202019-11-20 16:39:12 -08003159 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003160 cFlags := libBarCompileRule.Args["cFlags"]
3161 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3162 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3163 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3164 }
Jiyong Park37b25202018-07-11 10:49:27 +09003165}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003166
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003167func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3168 t.Parallel()
3169 ctx := testCc(t, `
3170 cc_library_shared {
3171 name: "libFoo",
3172 srcs: ["foo.c"],
3173 stubs: {
3174 symbol_file: "foo.map.txt",
3175 versions: ["current"],
3176 },
3177 apex_available: ["bar", "a1"],
3178 }
3179
3180 cc_library_shared {
3181 name: "libBar",
3182 srcs: ["bar.c"],
3183 shared_libs: ["libFoo"],
3184 apex_available: ["a1"],
3185 }
3186
3187 cc_library_shared {
3188 name: "libA1",
3189 srcs: ["a1.c"],
3190 shared_libs: ["libFoo"],
3191 apex_available: ["a1"],
3192 }
3193
3194 cc_library_shared {
3195 name: "libBarA1",
3196 srcs: ["bara1.c"],
3197 shared_libs: ["libFoo"],
3198 apex_available: ["bar", "a1"],
3199 }
3200
3201 cc_library_shared {
3202 name: "libAnyApex",
3203 srcs: ["anyApex.c"],
3204 shared_libs: ["libFoo"],
3205 apex_available: ["//apex_available:anyapex"],
3206 }
3207
3208 cc_library_shared {
3209 name: "libBaz",
3210 srcs: ["baz.c"],
3211 shared_libs: ["libFoo"],
3212 apex_available: ["baz"],
3213 }
3214
3215 cc_library_shared {
3216 name: "libQux",
3217 srcs: ["qux.c"],
3218 shared_libs: ["libFoo"],
3219 apex_available: ["qux", "bar"],
3220 }`)
3221
3222 variants := ctx.ModuleVariantsForTests("libFoo")
3223 expectedVariants := []string{
3224 "android_arm64_armv8-a_shared",
3225 "android_arm64_armv8-a_shared_current",
3226 "android_arm_armv7-a-neon_shared",
3227 "android_arm_armv7-a-neon_shared_current",
3228 }
3229 variantsMismatch := false
3230 if len(variants) != len(expectedVariants) {
3231 variantsMismatch = true
3232 } else {
3233 for _, v := range expectedVariants {
3234 if !inList(v, variants) {
3235 variantsMismatch = false
3236 }
3237 }
3238 }
3239 if variantsMismatch {
3240 t.Errorf("variants of libFoo expected:\n")
3241 for _, v := range expectedVariants {
3242 t.Errorf("%q\n", v)
3243 }
3244 t.Errorf(", but got:\n")
3245 for _, v := range variants {
3246 t.Errorf("%q\n", v)
3247 }
3248 }
3249
3250 linkAgainstFoo := []string{"libBarA1"}
3251 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3252
3253 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3254 for _, lib := range linkAgainstFoo {
3255 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3256 libFlags := libLinkRule.Args["libFlags"]
3257 if !strings.Contains(libFlags, libFooPath) {
3258 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3259 }
3260 }
3261
3262 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3263 for _, lib := range linkAgainstFooStubs {
3264 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3265 libFlags := libLinkRule.Args["libFlags"]
3266 if !strings.Contains(libFlags, libFooStubPath) {
3267 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3268 }
3269 }
3270}
3271
Jooyung Hanb04a4992020-03-13 18:57:35 +09003272func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003273 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003274 for _, tc := range []struct{ moduleName, expected string }{
3275 {"libc", "__LIBC_API__"},
3276 {"libfoo", "__LIBFOO_API__"},
3277 {"libfoo@1", "__LIBFOO_1_API__"},
3278 {"libfoo-v1", "__LIBFOO_V1_API__"},
3279 {"libfoo.v1", "__LIBFOO_V1_API__"},
3280 } {
3281 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3282 }
3283}
3284
Liz Kammer83cf81b2022-09-22 08:24:20 -04003285func pathsToBase(paths android.Paths) []string {
3286 var ret []string
3287 for _, p := range paths {
3288 ret = append(ret, p.Base())
3289 }
3290 return ret
3291}
3292
3293func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003294 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003295 ctx := testCc(t, `
3296 cc_library_static {
3297 name: "foo",
3298 srcs: ["foo.c"],
3299 }
3300
3301 cc_library_static {
3302 name: "bar",
3303 srcs: ["bar.c"],
3304 }
3305
3306 cc_library_shared {
3307 name: "qux",
3308 srcs: ["qux.c"],
3309 }
3310
3311 cc_library_static {
3312 name: "baz",
3313 srcs: ["baz.c"],
3314 static_libs: ["foo"],
3315 shared_libs: ["qux"],
3316 whole_static_libs: ["bar"],
3317 }`)
3318
3319 variant := "android_arm64_armv8-a_static"
3320 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3321
3322 // For static libraries, the object files of a whole static dep are included in the archive
3323 // directly
3324 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3325 t.Errorf("Expected input objects %q, got %q", w, g)
3326 }
3327
3328 // non whole static dependencies are not linked into the archive
3329 if len(arRule.Implicits) > 0 {
3330 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3331 }
3332}
3333
3334func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003335 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003336 ctx := testCc(t, `
3337 cc_library_static {
3338 name: "foo",
3339 srcs: ["foo.c"],
3340 }
3341
3342 cc_library_static {
3343 name: "bar",
3344 srcs: ["bar.c"],
3345 }
3346
3347 cc_library_shared {
3348 name: "qux",
3349 srcs: ["qux.c"],
3350 }
3351
3352 cc_library_shared {
3353 name: "baz",
3354 srcs: ["baz.c"],
3355 static_libs: ["foo"],
3356 shared_libs: ["qux"],
3357 whole_static_libs: ["bar"],
3358 }`)
3359
3360 variant := "android_arm64_armv8-a_shared"
3361 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3362 libFlags := linkRule.Args["libFlags"]
3363 // When dynamically linking, we expect static dependencies to be found on the command line
3364 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3365 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3366 }
3367 // When dynamically linking, we expect whole static dependencies to be found on the command line
3368 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3369 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3370 }
3371
3372 // When dynamically linking, we expect shared dependencies to be found on the command line
3373 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3374 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3375 }
3376
3377 // We should only have the objects from the shared library srcs, not the whole static dependencies
3378 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3379 t.Errorf("Expected input objects %q, got %q", w, g)
3380 }
3381}
3382
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003383func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003384 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003385 ctx := testCc(t, `
3386 cc_binary {
3387 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003388 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003389 static_executable: true,
3390 }`)
3391
Colin Cross7113d202019-11-20 16:39:12 -08003392 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003393 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3394 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003395 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003396 for _, lib := range systemStaticLibs {
3397 if !strings.Contains(libFlags, lib) {
3398 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3399 }
3400 }
3401 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3402 for _, lib := range systemSharedLibs {
3403 if strings.Contains(libFlags, lib) {
3404 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3405 }
3406 }
3407}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003408
3409func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003410 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003411 ctx := testCc(t, `
3412 cc_binary {
3413 name: "mybin",
3414 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003415 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003416 static_executable: true,
3417 stl: "none",
3418 }
3419
3420 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003421 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003422 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003423 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003424 stl: "none",
3425 }
3426
3427 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003428 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003429 srcs: ["foo.c"],
3430 stl: "none",
3431 stubs: {
3432 versions: ["1"],
3433 },
3434 }`)
3435
Colin Cross0de8a1e2020-09-18 14:15:30 -07003436 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3437 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003438 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003439
3440 if !reflect.DeepEqual(actual, expected) {
3441 t.Errorf("staticDeps orderings were not propagated correctly"+
3442 "\nactual: %v"+
3443 "\nexpected: %v",
3444 actual,
3445 expected,
3446 )
3447 }
3448}
Jooyung Han38002912019-05-16 04:01:54 +09003449
Jooyung Hand48f3c32019-08-23 11:18:57 +09003450func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003451 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003452 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3453 cc_library {
3454 name: "libA",
3455 srcs: ["foo.c"],
3456 shared_libs: ["libB"],
3457 stl: "none",
3458 }
3459
3460 cc_library {
3461 name: "libB",
3462 srcs: ["foo.c"],
3463 enabled: false,
3464 stl: "none",
3465 }
3466 `)
3467}
3468
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003469func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3470 bp := `
3471 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003472 name: "test_afl_fuzz_target",
3473 srcs: ["foo.c"],
3474 host_supported: true,
3475 static_libs: [
3476 "afl_fuzz_static_lib",
3477 ],
3478 shared_libs: [
3479 "afl_fuzz_shared_lib",
3480 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003481 fuzzing_frameworks: {
3482 afl: true,
3483 libfuzzer: false,
3484 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003485 }
3486 cc_library {
3487 name: "afl_fuzz_static_lib",
3488 host_supported: true,
3489 srcs: ["static_file.c"],
3490 }
3491 cc_library {
3492 name: "libfuzzer_only_static_lib",
3493 host_supported: true,
3494 srcs: ["static_file.c"],
3495 }
3496 cc_library {
3497 name: "afl_fuzz_shared_lib",
3498 host_supported: true,
3499 srcs: ["shared_file.c"],
3500 static_libs: [
3501 "second_static_lib",
3502 ],
3503 }
3504 cc_library_headers {
3505 name: "libafl_headers",
3506 vendor_available: true,
3507 host_supported: true,
3508 export_include_dirs: [
3509 "include",
3510 "instrumentation",
3511 ],
3512 }
3513 cc_object {
3514 name: "afl-compiler-rt",
3515 vendor_available: true,
3516 host_supported: true,
3517 cflags: [
3518 "-fPIC",
3519 ],
3520 srcs: [
3521 "instrumentation/afl-compiler-rt.o.c",
3522 ],
3523 }
3524 cc_library {
3525 name: "second_static_lib",
3526 host_supported: true,
3527 srcs: ["second_file.c"],
3528 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003529 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003530 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003531 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003532 srcs: [
3533 "aflpp_driver.c",
3534 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003535 }`
3536
3537 testEnv := map[string]string{
3538 "FUZZ_FRAMEWORK": "AFL",
3539 }
3540
3541 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003542
3543 checkPcGuardFlag := func(
3544 modName string, variantName string, shouldHave bool) {
3545 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3546
3547 cFlags, ok := cc.Args["cFlags"]
3548 if !ok {
3549 t.Errorf("Could not find cFlags for module %s and variant %s",
3550 modName, variantName)
3551 }
3552
3553 if strings.Contains(
3554 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3555 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3556 "Test failed for module %s and variant %s",
3557 !shouldHave, shouldHave, modName, variantName)
3558 }
3559 }
3560
Cory Barkera1da26f2022-06-07 20:12:06 +00003561 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003562 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003563
3564 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003565 checkPcGuardFlag(moduleName, variant+"_static", false)
3566 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003567
3568 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003569 checkPcGuardFlag(moduleName, variant+"_static", false)
3570 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003571
3572 ctx.ModuleForTests("afl_fuzz_shared_lib",
3573 "android_arm64_armv8-a_shared").Rule("cc")
3574 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003575 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3576}
3577
3578func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003579 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003580 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3581}
3582
3583func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003584 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003585 if runtime.GOOS != "linux" {
3586 t.Skip("requires linux")
3587 }
3588
3589 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003590}
3591
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003592// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3593// correctly.
3594func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003595 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003596 ctx := testCc(t, `
3597 cc_fuzz {
3598 name: "fuzz_smoke_test",
3599 srcs: ["foo.c"],
3600 }`)
3601
Paul Duffin075c4172019-12-19 19:06:13 +00003602 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003603 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3604}
3605
Jooyung Han38002912019-05-16 04:01:54 +09003606func assertString(t *testing.T, got, expected string) {
3607 t.Helper()
3608 if got != expected {
3609 t.Errorf("expected %q got %q", expected, got)
3610 }
3611}
3612
3613func assertArrayString(t *testing.T, got, expected []string) {
3614 t.Helper()
3615 if len(got) != len(expected) {
3616 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3617 return
3618 }
3619 for i := range got {
3620 if got[i] != expected[i] {
3621 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3622 i, expected[i], expected, got[i], got)
3623 return
3624 }
3625 }
3626}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003627
Jooyung Han0302a842019-10-30 18:43:49 +09003628func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3629 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08003630 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09003631}
3632
Colin Crosse1bb5d02019-09-24 14:55:04 -07003633func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003634 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003635 ctx := testCc(t, `
3636 cc_defaults {
3637 name: "defaults",
3638 srcs: ["foo.c"],
3639 static: {
3640 srcs: ["bar.c"],
3641 },
3642 shared: {
3643 srcs: ["baz.c"],
3644 },
3645 }
3646
3647 cc_library_static {
3648 name: "libstatic",
3649 defaults: ["defaults"],
3650 }
3651
3652 cc_library_shared {
3653 name: "libshared",
3654 defaults: ["defaults"],
3655 }
3656
3657 cc_library {
3658 name: "libboth",
3659 defaults: ["defaults"],
3660 }
3661
3662 cc_binary {
3663 name: "binary",
3664 defaults: ["defaults"],
3665 }`)
3666
Colin Cross7113d202019-11-20 16:39:12 -08003667 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003668 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3669 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3670 }
Colin Cross7113d202019-11-20 16:39:12 -08003671 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003672 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3673 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3674 }
Colin Cross7113d202019-11-20 16:39:12 -08003675 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003676 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3677 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3678 }
3679
Colin Cross7113d202019-11-20 16:39:12 -08003680 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003681 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3682 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3683 }
Colin Cross7113d202019-11-20 16:39:12 -08003684 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003685 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3686 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3687 }
3688}
Colin Crosseabaedd2020-02-06 17:01:55 -08003689
3690func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003691 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003692 bp := `
3693 cc_defaults {
3694 name: "libfoo_defaults",
3695 srcs: ["foo.c"],
3696 cppflags: ["-DFOO"],
3697 product_variables: {
3698 debuggable: {
3699 cppflags: ["-DBAR"],
3700 },
3701 },
3702 }
3703
3704 cc_library {
3705 name: "libfoo",
3706 defaults: ["libfoo_defaults"],
3707 }
3708 `
3709
Paul Duffin8567f222021-03-23 00:02:06 +00003710 result := android.GroupFixturePreparers(
3711 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003712 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003713
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003714 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3715 variables.Debuggable = BoolPtr(true)
3716 }),
3717 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003718
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003719 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003720 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003721}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003722
3723func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3724 t.Parallel()
3725 bp := `
3726 cc_library_static {
3727 name: "libfoo",
3728 srcs: ["foo.c"],
3729 whole_static_libs: ["libbar"],
3730 }
3731
3732 cc_library_static {
3733 name: "libbar",
3734 whole_static_libs: ["libmissing"],
3735 }
3736 `
3737
Paul Duffin8567f222021-03-23 00:02:06 +00003738 result := android.GroupFixturePreparers(
3739 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003740 android.PrepareForTestWithAllowMissingDependencies,
3741 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003742
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003743 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003744 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003745
Paul Duffine84b1332021-03-12 11:59:43 +00003746 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003747
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003748 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003749 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003750}
Colin Crosse9fe2942020-11-10 18:12:15 -08003751
3752func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003753 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003754 bp := `
3755 cc_binary {
3756 name: "bin",
3757 host_supported: true,
3758 shared_libs: ["libshared"],
3759 runtime_libs: ["libruntime"],
3760 srcs: [":gen"],
3761 }
3762
3763 cc_library_shared {
3764 name: "libshared",
3765 host_supported: true,
3766 shared_libs: ["libtransitive"],
3767 }
3768
3769 cc_library_shared {
3770 name: "libtransitive",
3771 host_supported: true,
3772 }
3773
3774 cc_library_shared {
3775 name: "libruntime",
3776 host_supported: true,
3777 }
3778
3779 cc_binary_host {
3780 name: "tool",
3781 srcs: ["foo.cpp"],
3782 }
3783
3784 genrule {
3785 name: "gen",
3786 tools: ["tool"],
3787 out: ["gen.cpp"],
3788 cmd: "$(location tool) $(out)",
3789 }
3790 `
3791
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003792 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003793 ctx := testCcWithConfig(t, config)
3794
3795 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3796 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3797 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3798 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3799 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3800
3801 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3802 t.Errorf("expected host bin dependency %q, got %q", w, g)
3803 }
3804
3805 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3806 t.Errorf("expected host bin dependency %q, got %q", w, g)
3807 }
3808
3809 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3810 t.Errorf("expected host bin dependency %q, got %q", w, g)
3811 }
3812
3813 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3814 t.Errorf("expected host bin dependency %q, got %q", w, g)
3815 }
3816
3817 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3818 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3819 }
3820
3821 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3822 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3823 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3824 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3825
3826 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3827 t.Errorf("expected device bin dependency %q, got %q", w, g)
3828 }
3829
3830 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3831 t.Errorf("expected device bin dependency %q, got %q", w, g)
3832 }
3833
3834 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3835 t.Errorf("expected device bin dependency %q, got %q", w, g)
3836 }
3837
3838 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3839 t.Errorf("expected device bin dependency %q, got %q", w, g)
3840 }
3841
3842 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3843 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3844 }
3845
3846}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003847
3848func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003849 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09003850 ctx := testCc(t, `
3851 cc_library_shared {
3852 name: "libclient",
3853 srcs: ["foo.c"],
3854 shared_libs: ["libfoo#1"],
3855 }
3856
3857 cc_library_shared {
3858 name: "libfoo",
3859 srcs: ["foo.c"],
3860 shared_libs: ["libbar"],
3861 export_shared_lib_headers: ["libbar"],
3862 stubs: {
3863 symbol_file: "foo.map.txt",
3864 versions: ["1", "2", "3"],
3865 },
3866 }
3867
3868 cc_library_shared {
3869 name: "libbar",
3870 export_include_dirs: ["include/libbar"],
3871 srcs: ["foo.c"],
3872 }`)
3873
3874 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3875
3876 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3877 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3878 }
3879}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003880
Vinh Tran09581952023-05-16 16:03:20 -04003881func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003882 t.Parallel()
3883 ctx := android.GroupFixturePreparers(
3884 prepareForCcTest,
3885 aidl_library.PrepareForTestWithAidlLibrary,
3886 android.MockFS{
3887 "package_bar/Android.bp": []byte(`
3888 aidl_library {
3889 name: "bar",
3890 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003891 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003892 strip_import_prefix: "x",
3893 }
3894 `)}.AddToFixture(),
3895 android.MockFS{
3896 "package_foo/Android.bp": []byte(`
3897 aidl_library {
3898 name: "foo",
3899 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003900 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003901 strip_import_prefix: "a",
3902 deps: ["bar"],
3903 }
3904 cc_library {
3905 name: "libfoo",
3906 aidl: {
3907 libs: ["foo"],
3908 }
3909 }
3910 `),
3911 }.AddToFixture(),
3912 ).RunTest(t).TestContext
3913
3914 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04003915
3916 android.AssertPathsRelativeToTopEquals(
3917 t,
3918 "aidl headers",
3919 []string{
3920 "package_bar/x/HeaderBar.aidl",
3921 "package_foo/a/HeaderFoo.aidl",
3922 "package_foo/a/b/Foo.aidl",
3923 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
3924 },
3925 libfoo.Rule("aidl_library").Implicits,
3926 )
3927
Colin Crossf61d03d2023-11-02 16:56:39 -07003928 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04003929 aidlCommand := manifest.Commands[0].GetCommand()
3930
3931 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
3932 if !strings.Contains(aidlCommand, expectedAidlFlags) {
3933 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
3934 }
3935
3936 outputs := strings.Join(libfoo.AllOutputs(), " ")
3937
Vinh Tran09581952023-05-16 16:03:20 -04003938 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
3939 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
3940 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003941 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
3942 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04003943 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
3944 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
3945 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003946 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
3947}
3948
Jooyung Hane197d8b2021-01-05 10:33:16 +09003949func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003950 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04003951 ctx := android.GroupFixturePreparers(
3952 prepareForCcTest,
3953 aidl_library.PrepareForTestWithAidlLibrary,
3954 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09003955 cc_library {
3956 name: "libfoo",
3957 srcs: ["a/Foo.aidl"],
3958 aidl: { flags: ["-Werror"], },
3959 }
3960 `)
3961
3962 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Colin Crossf61d03d2023-11-02 16:56:39 -07003963 manifest := android.RuleBuilderSboxProtoForTests(t, ctx.TestContext, libfoo.Output("aidl.sbox.textproto"))
Jooyung Hane197d8b2021-01-05 10:33:16 +09003964 aidlCommand := manifest.Commands[0].GetCommand()
3965 expectedAidlFlag := "-Werror"
3966 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3967 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3968 }
3969}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003970
Jooyung Han07f70c02021-11-06 07:08:45 +09003971func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003972 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09003973 for _, tc := range []struct {
3974 name string
3975 sdkVersion string
3976 variant string
3977 expected string
3978 }{
3979 {
3980 name: "default is current",
3981 sdkVersion: "",
3982 variant: "android_arm64_armv8-a_static",
3983 expected: "platform_apis",
3984 },
3985 {
3986 name: "use sdk_version",
3987 sdkVersion: `sdk_version: "29"`,
3988 variant: "android_arm64_armv8-a_static",
3989 expected: "platform_apis",
3990 },
3991 {
3992 name: "use sdk_version(sdk variant)",
3993 sdkVersion: `sdk_version: "29"`,
3994 variant: "android_arm64_armv8-a_sdk_static",
3995 expected: "29",
3996 },
3997 {
3998 name: "use min_sdk_version",
3999 sdkVersion: `min_sdk_version: "29"`,
4000 variant: "android_arm64_armv8-a_static",
4001 expected: "29",
4002 },
4003 } {
4004 t.Run(tc.name, func(t *testing.T) {
4005 ctx := testCc(t, `
4006 cc_library {
4007 name: "libfoo",
4008 stl: "none",
4009 srcs: ["a/Foo.aidl"],
4010 `+tc.sdkVersion+`
4011 }
4012 `)
4013 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
Colin Crossf61d03d2023-11-02 16:56:39 -07004014 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl.sbox.textproto"))
Jooyung Han07f70c02021-11-06 07:08:45 +09004015 aidlCommand := manifest.Commands[0].GetCommand()
4016 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4017 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4018 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4019 }
4020 })
4021 }
4022}
4023
Vinh Tran09581952023-05-16 16:03:20 -04004024func TestInvalidAidlProp(t *testing.T) {
4025 t.Parallel()
4026
4027 testCases := []struct {
4028 description string
4029 bp string
4030 }{
4031 {
4032 description: "Invalid use of aidl.libs and aidl.include_dirs",
4033 bp: `
4034 cc_library {
4035 name: "foo",
4036 aidl: {
4037 libs: ["foo_aidl"],
4038 include_dirs: ["bar/include"],
4039 }
4040 }
4041 `,
4042 },
4043 {
4044 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4045 bp: `
4046 cc_library {
4047 name: "foo",
4048 aidl: {
4049 libs: ["foo_aidl"],
4050 local_include_dirs: ["include"],
4051 }
4052 }
4053 `,
4054 },
4055 }
4056
4057 for _, testCase := range testCases {
4058 t.Run(testCase.description, func(t *testing.T) {
4059 bp := `
4060 aidl_library {
4061 name: "foo_aidl",
4062 srcs: ["Foo.aidl"],
4063 } ` + testCase.bp
4064 android.GroupFixturePreparers(
4065 prepareForCcTest,
4066 aidl_library.PrepareForTestWithAidlLibrary.
4067 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4068 ).RunTestWithBp(t, bp)
4069 })
4070 }
4071}
4072
Jiyong Parka008fb02021-03-16 17:15:53 +09004073func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004074 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004075 ctx := testCc(t, `
4076 cc_library_shared {
4077 name: "libfoo",
4078 srcs: ["foo.c"],
4079 min_sdk_version: "29",
4080 }`)
4081
4082 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4083 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4084}
4085
Vinh Tranf1924742022-06-24 16:40:11 -04004086func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004087 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004088 bp := `
4089 cc_library_shared {
4090 name: "libfoo",
4091 srcs: ["foo.c"],
4092 min_sdk_version: "S",
4093 }
4094 `
4095 result := android.GroupFixturePreparers(
4096 prepareForCcTest,
4097 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4098 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4099 }),
4100 ).RunTestWithBp(t, bp)
4101 ctx := result.TestContext
4102 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4103 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4104}
4105
Paul Duffin3cb603e2021-02-19 13:57:10 +00004106func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004107 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004108
4109 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4110 // embedded newline characters alone.
4111 trimIndentingSpaces := func(s string) string {
4112 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4113 }
4114
4115 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4116 t.Helper()
4117 expected = trimIndentingSpaces(expected)
4118 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4119 if expected != actual {
4120 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4121 }
4122 }
4123
4124 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4125
4126 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4127 t.Helper()
Colin Cross5a377182023-12-14 14:46:23 -08004128 exported, _ := android.SingletonModuleProvider(ctx, module, FlagExporterInfoProvider)
Paul Duffin3cb603e2021-02-19 13:57:10 +00004129 name := module.Name()
4130
4131 for _, checker := range checkers {
4132 checker(t, name, exported)
4133 }
4134 }
4135
4136 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4137 return func(t *testing.T, name string, exported FlagExporterInfo) {
4138 t.Helper()
4139 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4140 }
4141 }
4142
4143 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4144 return func(t *testing.T, name string, exported FlagExporterInfo) {
4145 t.Helper()
4146 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4147 }
4148 }
4149
4150 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4151 return func(t *testing.T, name string, exported FlagExporterInfo) {
4152 t.Helper()
4153 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4154 }
4155 }
4156
4157 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4158 return func(t *testing.T, name string, exported FlagExporterInfo) {
4159 t.Helper()
4160 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4161 }
4162 }
4163
4164 genRuleModules := `
4165 genrule {
4166 name: "genrule_foo",
4167 cmd: "generate-foo",
4168 out: [
4169 "generated_headers/foo/generated_header.h",
4170 ],
4171 export_include_dirs: [
4172 "generated_headers",
4173 ],
4174 }
4175
4176 genrule {
4177 name: "genrule_bar",
4178 cmd: "generate-bar",
4179 out: [
4180 "generated_headers/bar/generated_header.h",
4181 ],
4182 export_include_dirs: [
4183 "generated_headers",
4184 ],
4185 }
4186 `
4187
4188 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4189 ctx := testCc(t, genRuleModules+`
4190 cc_library {
4191 name: "libfoo",
4192 srcs: ["foo.c"],
4193 export_include_dirs: ["foo/standard"],
4194 export_system_include_dirs: ["foo/system"],
4195 generated_headers: ["genrule_foo"],
4196 export_generated_headers: ["genrule_foo"],
4197 }
4198
4199 cc_library {
4200 name: "libbar",
4201 srcs: ["bar.c"],
4202 shared_libs: ["libfoo"],
4203 export_include_dirs: ["bar/standard"],
4204 export_system_include_dirs: ["bar/system"],
4205 generated_headers: ["genrule_bar"],
4206 export_generated_headers: ["genrule_bar"],
4207 }
4208 `)
4209 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4210 checkIncludeDirs(t, ctx, foo,
4211 expectedIncludeDirs(`
4212 foo/standard
4213 .intermediates/genrule_foo/gen/generated_headers
4214 `),
4215 expectedSystemIncludeDirs(`foo/system`),
4216 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4217 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4218 )
4219
4220 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4221 checkIncludeDirs(t, ctx, bar,
4222 expectedIncludeDirs(`
4223 bar/standard
4224 .intermediates/genrule_bar/gen/generated_headers
4225 `),
4226 expectedSystemIncludeDirs(`bar/system`),
4227 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4228 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4229 )
4230 })
4231
4232 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4233 ctx := testCc(t, genRuleModules+`
4234 cc_library {
4235 name: "libfoo",
4236 srcs: ["foo.c"],
4237 export_include_dirs: ["foo/standard"],
4238 export_system_include_dirs: ["foo/system"],
4239 generated_headers: ["genrule_foo"],
4240 export_generated_headers: ["genrule_foo"],
4241 }
4242
4243 cc_library {
4244 name: "libbar",
4245 srcs: ["bar.c"],
4246 whole_static_libs: ["libfoo"],
4247 export_include_dirs: ["bar/standard"],
4248 export_system_include_dirs: ["bar/system"],
4249 generated_headers: ["genrule_bar"],
4250 export_generated_headers: ["genrule_bar"],
4251 }
4252 `)
4253 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4254 checkIncludeDirs(t, ctx, foo,
4255 expectedIncludeDirs(`
4256 foo/standard
4257 .intermediates/genrule_foo/gen/generated_headers
4258 `),
4259 expectedSystemIncludeDirs(`foo/system`),
4260 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4261 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4262 )
4263
4264 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4265 checkIncludeDirs(t, ctx, bar,
4266 expectedIncludeDirs(`
4267 bar/standard
4268 foo/standard
4269 .intermediates/genrule_foo/gen/generated_headers
4270 .intermediates/genrule_bar/gen/generated_headers
4271 `),
4272 expectedSystemIncludeDirs(`
4273 bar/system
4274 foo/system
4275 `),
4276 expectedGeneratedHeaders(`
4277 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4278 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4279 `),
4280 expectedOrderOnlyDeps(`
4281 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4282 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4283 `),
4284 )
4285 })
4286
Paul Duffin3cb603e2021-02-19 13:57:10 +00004287 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004288 ctx := android.GroupFixturePreparers(
4289 prepareForCcTest,
4290 aidl_library.PrepareForTestWithAidlLibrary,
4291 ).RunTestWithBp(t, `
4292 aidl_library {
4293 name: "libfoo_aidl",
4294 srcs: ["x/y/Bar.aidl"],
4295 strip_import_prefix: "x",
4296 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004297 cc_library_shared {
4298 name: "libfoo",
4299 srcs: [
4300 "foo.c",
4301 "b.aidl",
4302 "a.proto",
4303 ],
4304 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004305 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004306 export_aidl_headers: true,
4307 }
4308 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004309 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004310 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4311 checkIncludeDirs(t, ctx, foo,
4312 expectedIncludeDirs(`
4313 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004314 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004315 `),
4316 expectedSystemIncludeDirs(``),
4317 expectedGeneratedHeaders(`
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4319 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4320 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004321 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4322 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4323 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004324 `),
4325 expectedOrderOnlyDeps(`
4326 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4327 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4328 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004329 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4330 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4331 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004332 `),
4333 )
4334 })
4335
Paul Duffin3cb603e2021-02-19 13:57:10 +00004336 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4337 ctx := testCc(t, genRuleModules+`
4338 cc_library_shared {
4339 name: "libfoo",
4340 srcs: [
4341 "foo.c",
4342 "b.aidl",
4343 "a.proto",
4344 ],
4345 proto: {
4346 export_proto_headers: true,
4347 }
4348 }
4349 `)
4350 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4351 checkIncludeDirs(t, ctx, foo,
4352 expectedIncludeDirs(`
4353 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4354 `),
4355 expectedSystemIncludeDirs(``),
4356 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004357 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4358 `),
4359 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004360 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4361 `),
4362 )
4363 })
4364
Paul Duffin33056e82021-02-19 13:49:08 +00004365 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004366 ctx := testCc(t, genRuleModules+`
4367 cc_library_shared {
4368 name: "libfoo",
4369 srcs: [
4370 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004371 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004372 "b.aidl",
4373 "a.proto",
4374 ],
4375 }
4376 `)
4377 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4378 checkIncludeDirs(t, ctx, foo,
4379 expectedIncludeDirs(`
4380 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4381 `),
4382 expectedSystemIncludeDirs(``),
4383 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004384 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004385 `),
4386 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004387 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4388 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004389 `),
4390 )
4391 })
4392}
Colin Crossae628182021-06-14 16:52:28 -07004393
4394func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004395 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004396 baseExpectedFlags := []string{
4397 "${config.ArmThumbCflags}",
4398 "${config.ArmCflags}",
4399 "${config.CommonGlobalCflags}",
4400 "${config.DeviceGlobalCflags}",
4401 "${config.ExternalCflags}",
4402 "${config.ArmToolchainCflags}",
4403 "${config.ArmArmv7ANeonCflags}",
4404 "${config.ArmGenericCflags}",
4405 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004406 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004407 }
4408
4409 expectedIncludes := []string{
4410 "external/foo/android_arm_export_include_dirs",
4411 "external/foo/lib32_export_include_dirs",
4412 "external/foo/arm_export_include_dirs",
4413 "external/foo/android_export_include_dirs",
4414 "external/foo/linux_export_include_dirs",
4415 "external/foo/export_include_dirs",
4416 "external/foo/android_arm_local_include_dirs",
4417 "external/foo/lib32_local_include_dirs",
4418 "external/foo/arm_local_include_dirs",
4419 "external/foo/android_local_include_dirs",
4420 "external/foo/linux_local_include_dirs",
4421 "external/foo/local_include_dirs",
4422 "external/foo",
4423 "external/foo/libheader1",
4424 "external/foo/libheader2",
4425 "external/foo/libwhole1",
4426 "external/foo/libwhole2",
4427 "external/foo/libstatic1",
4428 "external/foo/libstatic2",
4429 "external/foo/libshared1",
4430 "external/foo/libshared2",
4431 "external/foo/liblinux",
4432 "external/foo/libandroid",
4433 "external/foo/libarm",
4434 "external/foo/lib32",
4435 "external/foo/libandroid_arm",
4436 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004437 }
4438
4439 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4440 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4441
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004442 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004443 cstd := []string{"-std=gnu17", "-std=conly"}
Elliott Hughesc79d9e32022-01-13 14:56:02 -08004444 cppstd := []string{"-std=gnu++20", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004445
4446 lastIncludes := []string{
4447 "out/soong/ndk/sysroot/usr/include",
4448 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4449 }
4450
4451 combineSlices := func(slices ...[]string) []string {
4452 var ret []string
4453 for _, s := range slices {
4454 ret = append(ret, s...)
4455 }
4456 return ret
4457 }
4458
4459 testCases := []struct {
4460 name string
4461 src string
4462 expected []string
4463 }{
4464 {
4465 name: "c",
4466 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004467 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004468 },
4469 {
4470 name: "cc",
4471 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004472 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004473 },
4474 {
4475 name: "assemble",
4476 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004477 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004478 },
4479 }
4480
4481 for _, tc := range testCases {
4482 t.Run(tc.name, func(t *testing.T) {
4483 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004484 cc_library {
4485 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004486 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004487 cflags: ["-std=candcpp"],
4488 conlyflags: ["-std=conly"],
4489 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004490 local_include_dirs: ["local_include_dirs"],
4491 export_include_dirs: ["export_include_dirs"],
4492 export_system_include_dirs: ["export_system_include_dirs"],
4493 static_libs: ["libstatic1", "libstatic2"],
4494 whole_static_libs: ["libwhole1", "libwhole2"],
4495 shared_libs: ["libshared1", "libshared2"],
4496 header_libs: ["libheader1", "libheader2"],
4497 target: {
4498 android: {
4499 shared_libs: ["libandroid"],
4500 local_include_dirs: ["android_local_include_dirs"],
4501 export_include_dirs: ["android_export_include_dirs"],
4502 },
4503 android_arm: {
4504 shared_libs: ["libandroid_arm"],
4505 local_include_dirs: ["android_arm_local_include_dirs"],
4506 export_include_dirs: ["android_arm_export_include_dirs"],
4507 },
4508 linux: {
4509 shared_libs: ["liblinux"],
4510 local_include_dirs: ["linux_local_include_dirs"],
4511 export_include_dirs: ["linux_export_include_dirs"],
4512 },
4513 },
4514 multilib: {
4515 lib32: {
4516 shared_libs: ["lib32"],
4517 local_include_dirs: ["lib32_local_include_dirs"],
4518 export_include_dirs: ["lib32_export_include_dirs"],
4519 },
4520 },
4521 arch: {
4522 arm: {
4523 shared_libs: ["libarm"],
4524 local_include_dirs: ["arm_local_include_dirs"],
4525 export_include_dirs: ["arm_export_include_dirs"],
4526 },
4527 },
4528 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004529 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004530 }
4531
4532 cc_library_headers {
4533 name: "libheader1",
4534 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004535 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004536 stl: "none",
4537 }
4538
4539 cc_library_headers {
4540 name: "libheader2",
4541 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004542 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004543 stl: "none",
4544 }
Liz Kammer08572c62021-09-30 10:11:04 -04004545 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004546
Liz Kammer08572c62021-09-30 10:11:04 -04004547 libs := []string{
4548 "libstatic1",
4549 "libstatic2",
4550 "libwhole1",
4551 "libwhole2",
4552 "libshared1",
4553 "libshared2",
4554 "libandroid",
4555 "libandroid_arm",
4556 "liblinux",
4557 "lib32",
4558 "libarm",
4559 }
Colin Crossae628182021-06-14 16:52:28 -07004560
Liz Kammer08572c62021-09-30 10:11:04 -04004561 for _, lib := range libs {
4562 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004563 cc_library {
4564 name: "%s",
4565 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004566 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004567 stl: "none",
4568 }
4569 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004570 }
4571
4572 ctx := android.GroupFixturePreparers(
4573 PrepareForIntegrationTestWithCc,
4574 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4575 ).RunTest(t)
4576 // Use the arm variant instead of the arm64 variant so that it gets headers from
4577 // ndk_libandroid_support to test LateStaticLibs.
4578 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4579
4580 var includes []string
4581 flags := strings.Split(cflags, " ")
4582 for _, flag := range flags {
4583 if strings.HasPrefix(flag, "-I") {
4584 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4585 } else if flag == "-isystem" {
4586 // skip isystem, include next
4587 } else if len(flag) > 0 {
4588 includes = append(includes, flag)
4589 }
4590 }
4591
4592 android.AssertArrayString(t, "includes", tc.expected, includes)
4593 })
Colin Crossae628182021-06-14 16:52:28 -07004594 }
4595
Colin Crossae628182021-06-14 16:52:28 -07004596}
Alixb5f6d9e2022-04-20 23:00:58 +00004597
zijunzhao933e3802023-01-12 07:26:20 +00004598func TestAddnoOverride64GlobalCflags(t *testing.T) {
4599 t.Parallel()
4600 ctx := testCc(t, `
4601 cc_library_shared {
4602 name: "libclient",
4603 srcs: ["foo.c"],
4604 shared_libs: ["libfoo#1"],
4605 }
4606
4607 cc_library_shared {
4608 name: "libfoo",
4609 srcs: ["foo.c"],
4610 shared_libs: ["libbar"],
4611 export_shared_lib_headers: ["libbar"],
4612 stubs: {
4613 symbol_file: "foo.map.txt",
4614 versions: ["1", "2", "3"],
4615 },
4616 }
4617
4618 cc_library_shared {
4619 name: "libbar",
4620 export_include_dirs: ["include/libbar"],
4621 srcs: ["foo.c"],
4622 }`)
4623
4624 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4625
4626 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4627 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4628 }
4629}
4630
Alixb5f6d9e2022-04-20 23:00:58 +00004631func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004632 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004633 tests := []struct {
4634 name string
4635 clang bool
4636 BuildBrokenClangProperty bool
4637 err string
4638 }{
4639 {
4640 name: "error when clang is set to false",
4641 clang: false,
4642 err: "is no longer supported",
4643 },
4644 {
4645 name: "error when clang is set to true",
4646 clang: true,
4647 err: "property is deprecated, see Changes.md",
4648 },
4649 {
4650 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4651 clang: true,
4652 BuildBrokenClangProperty: true,
4653 },
4654 }
4655
4656 for _, test := range tests {
4657 t.Run(test.name, func(t *testing.T) {
4658 bp := fmt.Sprintf(`
4659 cc_library {
4660 name: "foo",
4661 clang: %t,
4662 }`, test.clang)
4663
4664 if test.err == "" {
4665 android.GroupFixturePreparers(
4666 prepareForCcTest,
4667 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4668 if test.BuildBrokenClangProperty {
4669 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4670 }
4671 }),
4672 ).RunTestWithBp(t, bp)
4673 } else {
4674 prepareForCcTest.
4675 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4676 RunTestWithBp(t, bp)
4677 }
4678 })
4679 }
4680}
Alix Espinoef47e542022-09-14 19:10:51 +00004681
4682func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004683 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004684 tests := []struct {
4685 name string
4686 clangAsFlags []string
4687 BuildBrokenClangAsFlags bool
4688 err string
4689 }{
4690 {
4691 name: "error when clang_asflags is set",
4692 clangAsFlags: []string{"-a", "-b"},
4693 err: "clang_asflags: property is deprecated",
4694 },
4695 {
4696 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4697 clangAsFlags: []string{"-a", "-b"},
4698 BuildBrokenClangAsFlags: true,
4699 },
4700 }
4701
4702 for _, test := range tests {
4703 t.Run(test.name, func(t *testing.T) {
4704 bp := fmt.Sprintf(`
4705 cc_library {
4706 name: "foo",
4707 clang_asflags: %s,
4708 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4709
4710 if test.err == "" {
4711 android.GroupFixturePreparers(
4712 prepareForCcTest,
4713 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4714 if test.BuildBrokenClangAsFlags {
4715 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4716 }
4717 }),
4718 ).RunTestWithBp(t, bp)
4719 } else {
4720 prepareForCcTest.
4721 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4722 RunTestWithBp(t, bp)
4723 }
4724 })
4725 }
4726}
4727
4728func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004729 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004730 tests := []struct {
4731 name string
4732 clangCFlags []string
4733 BuildBrokenClangCFlags bool
4734 err string
4735 }{
4736 {
4737 name: "error when clang_cflags is set",
4738 clangCFlags: []string{"-a", "-b"},
4739 err: "clang_cflags: property is deprecated",
4740 },
4741 {
4742 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4743 clangCFlags: []string{"-a", "-b"},
4744 BuildBrokenClangCFlags: true,
4745 },
4746 }
4747
4748 for _, test := range tests {
4749 t.Run(test.name, func(t *testing.T) {
4750 bp := fmt.Sprintf(`
4751 cc_library {
4752 name: "foo",
4753 clang_cflags: %s,
4754 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4755
4756 if test.err == "" {
4757 android.GroupFixturePreparers(
4758 prepareForCcTest,
4759 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4760 if test.BuildBrokenClangCFlags {
4761 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4762 }
4763 }),
4764 ).RunTestWithBp(t, bp)
4765 } else {
4766 prepareForCcTest.
4767 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4768 RunTestWithBp(t, bp)
4769 }
4770 })
4771 }
4772}
Wei Li5f5d2712023-12-11 15:40:29 -08004773
4774func TestStrippedAllOutputFile(t *testing.T) {
4775 t.Parallel()
4776 bp := `
4777 cc_library {
4778 name: "test_lib",
4779 srcs: ["test_lib.cpp"],
4780 dist: {
4781 targets: [ "dist_target" ],
4782 tag: "stripped_all",
4783 }
4784 }
4785 `
4786 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
4787 ctx := testCcWithConfig(t, config)
4788 module := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared").Module()
4789 outputFile, err := module.(android.OutputFileProducer).OutputFiles("stripped_all")
4790 if err != nil {
4791 t.Errorf("Expected cc_library to produce output files, error: %s", err)
4792 return
4793 }
4794 if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") {
4795 t.Errorf("Unexpected output file: %s", outputFile.Strings()[0])
4796 return
4797 }
4798}
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09004799
4800// TODO(b/316829758) Remove this test and do not set VNDK version from other tests
4801func TestImageVariantsWithoutVndk(t *testing.T) {
4802 t.Parallel()
4803
4804 bp := `
4805 cc_binary {
4806 name: "binfoo",
4807 srcs: ["binfoo.cc"],
4808 vendor_available: true,
4809 product_available: true,
4810 shared_libs: ["libbar"]
4811 }
4812 cc_library {
4813 name: "libbar",
4814 srcs: ["libbar.cc"],
4815 vendor_available: true,
4816 product_available: true,
4817 }
4818 `
4819
4820 ctx := prepareForCcTestWithoutVndk.RunTestWithBp(t, bp)
4821
4822 hasDep := func(m android.Module, wantDep android.Module) bool {
4823 t.Helper()
4824 var found bool
4825 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
4826 if dep == wantDep {
4827 found = true
4828 }
4829 })
4830 return found
4831 }
4832
4833 testDepWithVariant := func(imageVariant string) {
4834 imageVariantStr := ""
4835 if imageVariant != "core" {
4836 imageVariantStr = "_" + imageVariant
4837 }
4838 binFooModule := ctx.ModuleForTests("binfoo", "android"+imageVariantStr+"_arm64_armv8-a").Module()
4839 libBarModule := ctx.ModuleForTests("libbar", "android"+imageVariantStr+"_arm64_armv8-a_shared").Module()
4840 android.AssertBoolEquals(t, "binfoo should have dependency on libbar with image variant "+imageVariant, true, hasDep(binFooModule, libBarModule))
4841 }
4842
4843 testDepWithVariant("core")
4844 testDepWithVariant("vendor")
4845 testDepWithVariant("product")
4846}
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +08004847
4848func TestVendorSdkVersionWithoutVndk(t *testing.T) {
4849 t.Parallel()
4850
4851 bp := `
4852 cc_library {
4853 name: "libfoo",
4854 srcs: ["libfoo.cc"],
4855 vendor_available: true,
4856 }
4857
4858 cc_library {
4859 name: "libbar",
4860 srcs: ["libbar.cc"],
4861 vendor_available: true,
4862 min_sdk_version: "29",
4863 }
4864 `
4865
4866 ctx := prepareForCcTestWithoutVndk.RunTestWithBp(t, bp)
4867 testSdkVersionFlag := func(module, version string) {
4868 flags := ctx.ModuleForTests(module, "android_vendor_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
4869 android.AssertStringDoesContain(t, "min sdk version", flags, "-target aarch64-linux-android"+version)
4870 }
4871
4872 testSdkVersionFlag("libfoo", "10000")
4873 testSdkVersionFlag("libbar", "29")
4874
4875 ctx = android.GroupFixturePreparers(
4876 prepareForCcTestWithoutVndk,
4877 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4878 if variables.BuildFlags == nil {
4879 variables.BuildFlags = make(map[string]string)
4880 }
4881 variables.BuildFlags["RELEASE_BOARD_API_LEVEL_FROZEN"] = "true"
4882 }),
4883 ).RunTestWithBp(t, bp)
4884 testSdkVersionFlag("libfoo", "30")
4885 testSdkVersionFlag("libbar", "29")
4886}