blob: d1b728ea224886c370ffb1213ebaef2cc985e518 [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",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002684 "android_vendor.29_arm_armv7-a-neon_shared",
2685 }
2686 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2687
2688 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han33eb6152024-03-11 15:46:48 +09002689 android.AssertSame(t, "use Vendor API level for default stubs", "202404", params.Args["apiLevel"])
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002690
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002691 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2692 t.Helper()
2693 m := result.ModuleForTests(module, variant).Module()
Colin Cross5a377182023-12-14 14:46:23 -08002694 f, _ := android.SingletonModuleProvider(result, m, FlagExporterInfoProvider)
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002695 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2696 expectedDirs, f.IncludeDirs)
2697 }
2698
2699 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2700 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2701 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2702 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2703 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2704 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2705}
2706
Jiyong Parka46a4d52017-12-14 19:54:34 +09002707func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002708 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002709 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002710 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002711 name: "libllndk_headers",
2712 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002713 llndk: {
2714 llndk_headers: true,
2715 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002716 }
2717 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002718 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002719 llndk: {
2720 symbol_file: "libllndk.map.txt",
2721 export_llndk_headers: ["libllndk_headers"],
2722 }
Colin Cross0477b422020-10-13 18:43:54 -07002723 }
2724
2725 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002726 name: "libvendor",
2727 shared_libs: ["libllndk"],
2728 vendor: true,
2729 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002730 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002731 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002732 }
2733 `)
2734
2735 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002736 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002737 cflags := cc.Args["cFlags"]
2738 if !strings.Contains(cflags, "-Imy_include") {
2739 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2740 }
2741}
2742
Logan Chien43d34c32017-12-20 01:17:32 +08002743func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2744 actual := module.Properties.AndroidMkRuntimeLibs
2745 if !reflect.DeepEqual(actual, expected) {
2746 t.Errorf("incorrect runtime_libs for shared libs"+
2747 "\nactual: %v"+
2748 "\nexpected: %v",
2749 actual,
2750 expected,
2751 )
2752 }
2753}
2754
2755const runtimeLibAndroidBp = `
2756 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002757 name: "liball_available",
2758 vendor_available: true,
2759 product_available: true,
2760 no_libcrt : true,
2761 nocrt : true,
2762 system_shared_libs : [],
2763 }
2764 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002765 name: "libvendor_available1",
2766 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002767 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002768 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002769 nocrt : true,
2770 system_shared_libs : [],
2771 }
2772 cc_library {
2773 name: "libvendor_available2",
2774 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002775 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002776 target: {
2777 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002778 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002779 }
2780 },
Yi Konge7fe9912019-06-02 00:53:50 -07002781 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002782 nocrt : true,
2783 system_shared_libs : [],
2784 }
2785 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002786 name: "libproduct_vendor",
2787 product_specific: true,
2788 vendor_available: true,
2789 no_libcrt : true,
2790 nocrt : true,
2791 system_shared_libs : [],
2792 }
2793 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002794 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002795 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002796 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002797 nocrt : true,
2798 system_shared_libs : [],
2799 }
2800 cc_library {
2801 name: "libvendor1",
2802 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002803 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002804 nocrt : true,
2805 system_shared_libs : [],
2806 }
2807 cc_library {
2808 name: "libvendor2",
2809 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002810 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002811 no_libcrt : true,
2812 nocrt : true,
2813 system_shared_libs : [],
2814 }
2815 cc_library {
2816 name: "libproduct_available1",
2817 product_available: true,
2818 runtime_libs: ["liball_available"],
2819 no_libcrt : true,
2820 nocrt : true,
2821 system_shared_libs : [],
2822 }
2823 cc_library {
2824 name: "libproduct1",
2825 product_specific: true,
2826 no_libcrt : true,
2827 nocrt : true,
2828 system_shared_libs : [],
2829 }
2830 cc_library {
2831 name: "libproduct2",
2832 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002833 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002834 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002835 nocrt : true,
2836 system_shared_libs : [],
2837 }
2838`
2839
2840func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002841 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002842 ctx := testCc(t, runtimeLibAndroidBp)
2843
2844 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002845 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002846
Justin Yun8a2600c2020-12-07 12:44:03 +09002847 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2848 checkRuntimeLibs(t, []string{"liball_available"}, module)
2849
2850 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2851 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002852
2853 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002854 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002855
2856 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2857 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002858 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002859
Justin Yun8a2600c2020-12-07 12:44:03 +09002860 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2861 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002862
2863 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002864 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002865
2866 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2867 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002868 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002869
2870 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2871 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2872
2873 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002874 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002875}
2876
2877func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002878 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002879 ctx := testCc(t, runtimeLibAndroidBp)
2880
Colin Cross7113d202019-11-20 16:39:12 -08002881 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002882 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2883 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002884
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002885 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002886 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002887 checkRuntimeLibs(t, nil, module)
2888}
2889
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002890func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002891 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002892 actual := module.Properties.AndroidMkStaticLibs
2893 if !reflect.DeepEqual(actual, expected) {
2894 t.Errorf("incorrect static_libs"+
2895 "\nactual: %v"+
2896 "\nexpected: %v",
2897 actual,
2898 expected,
2899 )
2900 }
2901}
2902
2903const staticLibAndroidBp = `
2904 cc_library {
2905 name: "lib1",
2906 }
2907 cc_library {
2908 name: "lib2",
2909 static_libs: ["lib1"],
2910 }
2911`
2912
2913func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002914 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002915 ctx := testCc(t, staticLibAndroidBp)
2916
2917 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002918 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002919 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002920 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002921
2922 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002923 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002924 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2925 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002926 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002927}
2928
Jiyong Parkd08b6972017-09-26 10:50:54 +09002929var compilerFlagsTestCases = []struct {
2930 in string
2931 out bool
2932}{
2933 {
2934 in: "a",
2935 out: false,
2936 },
2937 {
2938 in: "-a",
2939 out: true,
2940 },
2941 {
2942 in: "-Ipath/to/something",
2943 out: false,
2944 },
2945 {
2946 in: "-isystempath/to/something",
2947 out: false,
2948 },
2949 {
2950 in: "--coverage",
2951 out: false,
2952 },
2953 {
2954 in: "-include a/b",
2955 out: true,
2956 },
2957 {
2958 in: "-include a/b c/d",
2959 out: false,
2960 },
2961 {
2962 in: "-DMACRO",
2963 out: true,
2964 },
2965 {
2966 in: "-DMAC RO",
2967 out: false,
2968 },
2969 {
2970 in: "-a -b",
2971 out: false,
2972 },
2973 {
2974 in: "-DMACRO=definition",
2975 out: true,
2976 },
2977 {
2978 in: "-DMACRO=defi nition",
2979 out: true, // TODO(jiyong): this should be false
2980 },
2981 {
2982 in: "-DMACRO(x)=x + 1",
2983 out: true,
2984 },
2985 {
2986 in: "-DMACRO=\"defi nition\"",
2987 out: true,
2988 },
2989}
2990
2991type mockContext struct {
2992 BaseModuleContext
2993 result bool
2994}
2995
2996func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2997 // CheckBadCompilerFlags calls this function when the flag should be rejected
2998 ctx.result = false
2999}
3000
3001func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003002 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003003 for _, testCase := range compilerFlagsTestCases {
3004 ctx := &mockContext{result: true}
3005 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3006 if ctx.result != testCase.out {
3007 t.Errorf("incorrect output:")
3008 t.Errorf(" input: %#v", testCase.in)
3009 t.Errorf(" expected: %#v", testCase.out)
3010 t.Errorf(" got: %#v", ctx.result)
3011 }
3012 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003013}
Jiyong Park374510b2018-03-19 18:23:01 +09003014
Jiyong Park37b25202018-07-11 10:49:27 +09003015func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003016 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003017 ctx := testCc(t, `
3018 cc_library_shared {
3019 name: "librecovery",
3020 recovery: true,
3021 }
3022 cc_library_shared {
3023 name: "librecovery32",
3024 recovery: true,
3025 compile_multilib:"32",
3026 }
Jiyong Park5baac542018-08-28 09:55:37 +09003027 cc_library_shared {
3028 name: "libHalInRecovery",
3029 recovery_available: true,
3030 vendor: true,
3031 }
Jiyong Park37b25202018-07-11 10:49:27 +09003032 `)
3033
3034 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003035 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003036 if len(variants) != 1 || !android.InList(arm64, variants) {
3037 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3038 }
3039
3040 variants = ctx.ModuleVariantsForTests("librecovery32")
3041 if android.InList(arm64, variants) {
3042 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3043 }
Jiyong Park5baac542018-08-28 09:55:37 +09003044
3045 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3046 if !recoveryModule.Platform() {
3047 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3048 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003049}
Jiyong Park5baac542018-08-28 09:55:37 +09003050
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003051func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003052 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003053 bp := `
3054 cc_prebuilt_test_library_shared {
3055 name: "test_lib",
3056 relative_install_path: "foo/bar/baz",
3057 srcs: ["srcpath/dontusethispath/baz.so"],
3058 }
3059
3060 cc_test {
3061 name: "main_test",
3062 data_libs: ["test_lib"],
3063 gtest: false,
3064 }
3065 `
3066
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003067 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003068 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003069 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003070 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3071
3072 ctx := testCcWithConfig(t, config)
3073 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3074 testBinary := module.(*Module).linker.(*testBinary)
3075 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3076 if err != nil {
3077 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3078 }
3079 if len(outputFiles) != 1 {
3080 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3081 }
3082 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003083 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003084 }
3085
3086 outputPath := outputFiles[0].String()
3087
3088 if !strings.HasSuffix(outputPath, "/main_test") {
3089 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3090 }
Colin Crossaa255532020-07-03 13:18:24 -07003091 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003092 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3093 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3094 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3095 }
3096}
3097
Jiyong Park7ed9de32018-10-15 22:25:07 +09003098func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003099 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003100 ctx := testCc(t, `
3101 cc_library_shared {
3102 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003103 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003104 stubs: {
3105 symbol_file: "foo.map.txt",
3106 versions: ["1", "2", "3"],
3107 },
3108 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003109
Jiyong Park7ed9de32018-10-15 22:25:07 +09003110 cc_library_shared {
3111 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003112 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003113 shared_libs: ["libFoo#1"],
3114 }`)
3115
3116 variants := ctx.ModuleVariantsForTests("libFoo")
3117 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003118 "android_arm64_armv8-a_shared",
3119 "android_arm64_armv8-a_shared_1",
3120 "android_arm64_armv8-a_shared_2",
3121 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003122 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003123 "android_arm_armv7-a-neon_shared",
3124 "android_arm_armv7-a-neon_shared_1",
3125 "android_arm_armv7-a-neon_shared_2",
3126 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003127 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003128 }
3129 variantsMismatch := false
3130 if len(variants) != len(expectedVariants) {
3131 variantsMismatch = true
3132 } else {
3133 for _, v := range expectedVariants {
3134 if !inList(v, variants) {
3135 variantsMismatch = false
3136 }
3137 }
3138 }
3139 if variantsMismatch {
3140 t.Errorf("variants of libFoo expected:\n")
3141 for _, v := range expectedVariants {
3142 t.Errorf("%q\n", v)
3143 }
3144 t.Errorf(", but got:\n")
3145 for _, v := range variants {
3146 t.Errorf("%q\n", v)
3147 }
3148 }
3149
Colin Cross7113d202019-11-20 16:39:12 -08003150 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003151 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003152 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003153 if !strings.Contains(libFlags, libFoo1StubPath) {
3154 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3155 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003156
Colin Cross7113d202019-11-20 16:39:12 -08003157 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003158 cFlags := libBarCompileRule.Args["cFlags"]
3159 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3160 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3161 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3162 }
Jiyong Park37b25202018-07-11 10:49:27 +09003163}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003164
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003165func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3166 t.Parallel()
3167 ctx := testCc(t, `
3168 cc_library_shared {
3169 name: "libFoo",
3170 srcs: ["foo.c"],
3171 stubs: {
3172 symbol_file: "foo.map.txt",
3173 versions: ["current"],
3174 },
3175 apex_available: ["bar", "a1"],
3176 }
3177
3178 cc_library_shared {
3179 name: "libBar",
3180 srcs: ["bar.c"],
3181 shared_libs: ["libFoo"],
3182 apex_available: ["a1"],
3183 }
3184
3185 cc_library_shared {
3186 name: "libA1",
3187 srcs: ["a1.c"],
3188 shared_libs: ["libFoo"],
3189 apex_available: ["a1"],
3190 }
3191
3192 cc_library_shared {
3193 name: "libBarA1",
3194 srcs: ["bara1.c"],
3195 shared_libs: ["libFoo"],
3196 apex_available: ["bar", "a1"],
3197 }
3198
3199 cc_library_shared {
3200 name: "libAnyApex",
3201 srcs: ["anyApex.c"],
3202 shared_libs: ["libFoo"],
3203 apex_available: ["//apex_available:anyapex"],
3204 }
3205
3206 cc_library_shared {
3207 name: "libBaz",
3208 srcs: ["baz.c"],
3209 shared_libs: ["libFoo"],
3210 apex_available: ["baz"],
3211 }
3212
3213 cc_library_shared {
3214 name: "libQux",
3215 srcs: ["qux.c"],
3216 shared_libs: ["libFoo"],
3217 apex_available: ["qux", "bar"],
3218 }`)
3219
3220 variants := ctx.ModuleVariantsForTests("libFoo")
3221 expectedVariants := []string{
3222 "android_arm64_armv8-a_shared",
3223 "android_arm64_armv8-a_shared_current",
3224 "android_arm_armv7-a-neon_shared",
3225 "android_arm_armv7-a-neon_shared_current",
3226 }
3227 variantsMismatch := false
3228 if len(variants) != len(expectedVariants) {
3229 variantsMismatch = true
3230 } else {
3231 for _, v := range expectedVariants {
3232 if !inList(v, variants) {
3233 variantsMismatch = false
3234 }
3235 }
3236 }
3237 if variantsMismatch {
3238 t.Errorf("variants of libFoo expected:\n")
3239 for _, v := range expectedVariants {
3240 t.Errorf("%q\n", v)
3241 }
3242 t.Errorf(", but got:\n")
3243 for _, v := range variants {
3244 t.Errorf("%q\n", v)
3245 }
3246 }
3247
3248 linkAgainstFoo := []string{"libBarA1"}
3249 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3250
3251 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3252 for _, lib := range linkAgainstFoo {
3253 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3254 libFlags := libLinkRule.Args["libFlags"]
3255 if !strings.Contains(libFlags, libFooPath) {
3256 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3257 }
3258 }
3259
3260 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3261 for _, lib := range linkAgainstFooStubs {
3262 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3263 libFlags := libLinkRule.Args["libFlags"]
3264 if !strings.Contains(libFlags, libFooStubPath) {
3265 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3266 }
3267 }
3268}
3269
Jooyung Hanb04a4992020-03-13 18:57:35 +09003270func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003271 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003272 for _, tc := range []struct{ moduleName, expected string }{
3273 {"libc", "__LIBC_API__"},
3274 {"libfoo", "__LIBFOO_API__"},
3275 {"libfoo@1", "__LIBFOO_1_API__"},
3276 {"libfoo-v1", "__LIBFOO_V1_API__"},
3277 {"libfoo.v1", "__LIBFOO_V1_API__"},
3278 } {
3279 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3280 }
3281}
3282
Liz Kammer83cf81b2022-09-22 08:24:20 -04003283func pathsToBase(paths android.Paths) []string {
3284 var ret []string
3285 for _, p := range paths {
3286 ret = append(ret, p.Base())
3287 }
3288 return ret
3289}
3290
3291func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003292 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003293 ctx := testCc(t, `
3294 cc_library_static {
3295 name: "foo",
3296 srcs: ["foo.c"],
3297 }
3298
3299 cc_library_static {
3300 name: "bar",
3301 srcs: ["bar.c"],
3302 }
3303
3304 cc_library_shared {
3305 name: "qux",
3306 srcs: ["qux.c"],
3307 }
3308
3309 cc_library_static {
3310 name: "baz",
3311 srcs: ["baz.c"],
3312 static_libs: ["foo"],
3313 shared_libs: ["qux"],
3314 whole_static_libs: ["bar"],
3315 }`)
3316
3317 variant := "android_arm64_armv8-a_static"
3318 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3319
3320 // For static libraries, the object files of a whole static dep are included in the archive
3321 // directly
3322 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3323 t.Errorf("Expected input objects %q, got %q", w, g)
3324 }
3325
3326 // non whole static dependencies are not linked into the archive
3327 if len(arRule.Implicits) > 0 {
3328 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3329 }
3330}
3331
3332func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003333 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003334 ctx := testCc(t, `
3335 cc_library_static {
3336 name: "foo",
3337 srcs: ["foo.c"],
3338 }
3339
3340 cc_library_static {
3341 name: "bar",
3342 srcs: ["bar.c"],
3343 }
3344
3345 cc_library_shared {
3346 name: "qux",
3347 srcs: ["qux.c"],
3348 }
3349
3350 cc_library_shared {
3351 name: "baz",
3352 srcs: ["baz.c"],
3353 static_libs: ["foo"],
3354 shared_libs: ["qux"],
3355 whole_static_libs: ["bar"],
3356 }`)
3357
3358 variant := "android_arm64_armv8-a_shared"
3359 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3360 libFlags := linkRule.Args["libFlags"]
3361 // When dynamically linking, we expect static dependencies to be found on the command line
3362 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3363 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3364 }
3365 // When dynamically linking, we expect whole static dependencies to be found on the command line
3366 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3367 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3368 }
3369
3370 // When dynamically linking, we expect shared dependencies to be found on the command line
3371 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3372 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3373 }
3374
3375 // We should only have the objects from the shared library srcs, not the whole static dependencies
3376 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3377 t.Errorf("Expected input objects %q, got %q", w, g)
3378 }
3379}
3380
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003381func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003382 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003383 ctx := testCc(t, `
3384 cc_binary {
3385 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003386 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003387 static_executable: true,
3388 }`)
3389
Colin Cross7113d202019-11-20 16:39:12 -08003390 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003391 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3392 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003393 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003394 for _, lib := range systemStaticLibs {
3395 if !strings.Contains(libFlags, lib) {
3396 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3397 }
3398 }
3399 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3400 for _, lib := range systemSharedLibs {
3401 if strings.Contains(libFlags, lib) {
3402 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3403 }
3404 }
3405}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003406
3407func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003408 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003409 ctx := testCc(t, `
3410 cc_binary {
3411 name: "mybin",
3412 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003413 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003414 static_executable: true,
3415 stl: "none",
3416 }
3417
3418 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003419 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003420 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003421 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003422 stl: "none",
3423 }
3424
3425 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003426 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003427 srcs: ["foo.c"],
3428 stl: "none",
3429 stubs: {
3430 versions: ["1"],
3431 },
3432 }`)
3433
Colin Cross0de8a1e2020-09-18 14:15:30 -07003434 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3435 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003436 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003437
3438 if !reflect.DeepEqual(actual, expected) {
3439 t.Errorf("staticDeps orderings were not propagated correctly"+
3440 "\nactual: %v"+
3441 "\nexpected: %v",
3442 actual,
3443 expected,
3444 )
3445 }
3446}
Jooyung Han38002912019-05-16 04:01:54 +09003447
Jooyung Hand48f3c32019-08-23 11:18:57 +09003448func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003449 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003450 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3451 cc_library {
3452 name: "libA",
3453 srcs: ["foo.c"],
3454 shared_libs: ["libB"],
3455 stl: "none",
3456 }
3457
3458 cc_library {
3459 name: "libB",
3460 srcs: ["foo.c"],
3461 enabled: false,
3462 stl: "none",
3463 }
3464 `)
3465}
3466
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003467func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3468 bp := `
3469 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003470 name: "test_afl_fuzz_target",
3471 srcs: ["foo.c"],
3472 host_supported: true,
3473 static_libs: [
3474 "afl_fuzz_static_lib",
3475 ],
3476 shared_libs: [
3477 "afl_fuzz_shared_lib",
3478 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003479 fuzzing_frameworks: {
3480 afl: true,
3481 libfuzzer: false,
3482 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003483 }
3484 cc_library {
3485 name: "afl_fuzz_static_lib",
3486 host_supported: true,
3487 srcs: ["static_file.c"],
3488 }
3489 cc_library {
3490 name: "libfuzzer_only_static_lib",
3491 host_supported: true,
3492 srcs: ["static_file.c"],
3493 }
3494 cc_library {
3495 name: "afl_fuzz_shared_lib",
3496 host_supported: true,
3497 srcs: ["shared_file.c"],
3498 static_libs: [
3499 "second_static_lib",
3500 ],
3501 }
3502 cc_library_headers {
3503 name: "libafl_headers",
3504 vendor_available: true,
3505 host_supported: true,
3506 export_include_dirs: [
3507 "include",
3508 "instrumentation",
3509 ],
3510 }
3511 cc_object {
3512 name: "afl-compiler-rt",
3513 vendor_available: true,
3514 host_supported: true,
3515 cflags: [
3516 "-fPIC",
3517 ],
3518 srcs: [
3519 "instrumentation/afl-compiler-rt.o.c",
3520 ],
3521 }
3522 cc_library {
3523 name: "second_static_lib",
3524 host_supported: true,
3525 srcs: ["second_file.c"],
3526 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003527 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003528 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003529 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003530 srcs: [
3531 "aflpp_driver.c",
3532 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003533 }`
3534
3535 testEnv := map[string]string{
3536 "FUZZ_FRAMEWORK": "AFL",
3537 }
3538
3539 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003540
3541 checkPcGuardFlag := func(
3542 modName string, variantName string, shouldHave bool) {
3543 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3544
3545 cFlags, ok := cc.Args["cFlags"]
3546 if !ok {
3547 t.Errorf("Could not find cFlags for module %s and variant %s",
3548 modName, variantName)
3549 }
3550
3551 if strings.Contains(
3552 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3553 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3554 "Test failed for module %s and variant %s",
3555 !shouldHave, shouldHave, modName, variantName)
3556 }
3557 }
3558
Cory Barkera1da26f2022-06-07 20:12:06 +00003559 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003560 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003561
3562 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003563 checkPcGuardFlag(moduleName, variant+"_static", false)
3564 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003565
3566 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003567 checkPcGuardFlag(moduleName, variant+"_static", false)
3568 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003569
3570 ctx.ModuleForTests("afl_fuzz_shared_lib",
3571 "android_arm64_armv8-a_shared").Rule("cc")
3572 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003573 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3574}
3575
3576func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003577 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003578 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3579}
3580
3581func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003582 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003583 if runtime.GOOS != "linux" {
3584 t.Skip("requires linux")
3585 }
3586
3587 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003588}
3589
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003590// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3591// correctly.
3592func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003593 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003594 ctx := testCc(t, `
3595 cc_fuzz {
3596 name: "fuzz_smoke_test",
3597 srcs: ["foo.c"],
3598 }`)
3599
Paul Duffin075c4172019-12-19 19:06:13 +00003600 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003601 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3602}
3603
Jooyung Han38002912019-05-16 04:01:54 +09003604func assertString(t *testing.T, got, expected string) {
3605 t.Helper()
3606 if got != expected {
3607 t.Errorf("expected %q got %q", expected, got)
3608 }
3609}
3610
3611func assertArrayString(t *testing.T, got, expected []string) {
3612 t.Helper()
3613 if len(got) != len(expected) {
3614 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3615 return
3616 }
3617 for i := range got {
3618 if got[i] != expected[i] {
3619 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3620 i, expected[i], expected, got[i], got)
3621 return
3622 }
3623 }
3624}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003625
Jooyung Han0302a842019-10-30 18:43:49 +09003626func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3627 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08003628 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09003629}
3630
Colin Crosse1bb5d02019-09-24 14:55:04 -07003631func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003632 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003633 ctx := testCc(t, `
3634 cc_defaults {
3635 name: "defaults",
3636 srcs: ["foo.c"],
3637 static: {
3638 srcs: ["bar.c"],
3639 },
3640 shared: {
3641 srcs: ["baz.c"],
3642 },
3643 }
3644
3645 cc_library_static {
3646 name: "libstatic",
3647 defaults: ["defaults"],
3648 }
3649
3650 cc_library_shared {
3651 name: "libshared",
3652 defaults: ["defaults"],
3653 }
3654
3655 cc_library {
3656 name: "libboth",
3657 defaults: ["defaults"],
3658 }
3659
3660 cc_binary {
3661 name: "binary",
3662 defaults: ["defaults"],
3663 }`)
3664
Colin Cross7113d202019-11-20 16:39:12 -08003665 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003666 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3667 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3668 }
Colin Cross7113d202019-11-20 16:39:12 -08003669 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003670 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3671 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3672 }
Colin Cross7113d202019-11-20 16:39:12 -08003673 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003674 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3675 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3676 }
3677
Colin Cross7113d202019-11-20 16:39:12 -08003678 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003679 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3680 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3681 }
Colin Cross7113d202019-11-20 16:39:12 -08003682 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003683 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3684 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3685 }
3686}
Colin Crosseabaedd2020-02-06 17:01:55 -08003687
3688func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003689 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003690 bp := `
3691 cc_defaults {
3692 name: "libfoo_defaults",
3693 srcs: ["foo.c"],
3694 cppflags: ["-DFOO"],
3695 product_variables: {
3696 debuggable: {
3697 cppflags: ["-DBAR"],
3698 },
3699 },
3700 }
3701
3702 cc_library {
3703 name: "libfoo",
3704 defaults: ["libfoo_defaults"],
3705 }
3706 `
3707
Paul Duffin8567f222021-03-23 00:02:06 +00003708 result := android.GroupFixturePreparers(
3709 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003710 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003711
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003712 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3713 variables.Debuggable = BoolPtr(true)
3714 }),
3715 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003716
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003717 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003718 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003719}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003720
3721func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3722 t.Parallel()
3723 bp := `
3724 cc_library_static {
3725 name: "libfoo",
3726 srcs: ["foo.c"],
3727 whole_static_libs: ["libbar"],
3728 }
3729
3730 cc_library_static {
3731 name: "libbar",
3732 whole_static_libs: ["libmissing"],
3733 }
3734 `
3735
Paul Duffin8567f222021-03-23 00:02:06 +00003736 result := android.GroupFixturePreparers(
3737 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003738 android.PrepareForTestWithAllowMissingDependencies,
3739 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003740
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003741 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003742 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003743
Paul Duffine84b1332021-03-12 11:59:43 +00003744 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003745
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003746 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003747 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003748}
Colin Crosse9fe2942020-11-10 18:12:15 -08003749
3750func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003751 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003752 bp := `
3753 cc_binary {
3754 name: "bin",
3755 host_supported: true,
3756 shared_libs: ["libshared"],
3757 runtime_libs: ["libruntime"],
3758 srcs: [":gen"],
3759 }
3760
3761 cc_library_shared {
3762 name: "libshared",
3763 host_supported: true,
3764 shared_libs: ["libtransitive"],
3765 }
3766
3767 cc_library_shared {
3768 name: "libtransitive",
3769 host_supported: true,
3770 }
3771
3772 cc_library_shared {
3773 name: "libruntime",
3774 host_supported: true,
3775 }
3776
3777 cc_binary_host {
3778 name: "tool",
3779 srcs: ["foo.cpp"],
3780 }
3781
3782 genrule {
3783 name: "gen",
3784 tools: ["tool"],
3785 out: ["gen.cpp"],
3786 cmd: "$(location tool) $(out)",
3787 }
3788 `
3789
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003790 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003791 ctx := testCcWithConfig(t, config)
3792
3793 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3794 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3795 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3796 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3797 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3798
3799 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3800 t.Errorf("expected host bin dependency %q, got %q", w, g)
3801 }
3802
3803 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3804 t.Errorf("expected host bin dependency %q, got %q", w, g)
3805 }
3806
3807 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3808 t.Errorf("expected host bin dependency %q, got %q", w, g)
3809 }
3810
3811 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3812 t.Errorf("expected host bin dependency %q, got %q", w, g)
3813 }
3814
3815 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3816 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3817 }
3818
3819 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3820 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3821 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3822 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3823
3824 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3825 t.Errorf("expected device bin dependency %q, got %q", w, g)
3826 }
3827
3828 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3829 t.Errorf("expected device bin dependency %q, got %q", w, g)
3830 }
3831
3832 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3833 t.Errorf("expected device bin dependency %q, got %q", w, g)
3834 }
3835
3836 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3837 t.Errorf("expected device bin dependency %q, got %q", w, g)
3838 }
3839
3840 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3841 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3842 }
3843
3844}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003845
3846func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003847 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09003848 ctx := testCc(t, `
3849 cc_library_shared {
3850 name: "libclient",
3851 srcs: ["foo.c"],
3852 shared_libs: ["libfoo#1"],
3853 }
3854
3855 cc_library_shared {
3856 name: "libfoo",
3857 srcs: ["foo.c"],
3858 shared_libs: ["libbar"],
3859 export_shared_lib_headers: ["libbar"],
3860 stubs: {
3861 symbol_file: "foo.map.txt",
3862 versions: ["1", "2", "3"],
3863 },
3864 }
3865
3866 cc_library_shared {
3867 name: "libbar",
3868 export_include_dirs: ["include/libbar"],
3869 srcs: ["foo.c"],
3870 }`)
3871
3872 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3873
3874 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3875 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3876 }
3877}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003878
Vinh Tran09581952023-05-16 16:03:20 -04003879func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003880 t.Parallel()
3881 ctx := android.GroupFixturePreparers(
3882 prepareForCcTest,
3883 aidl_library.PrepareForTestWithAidlLibrary,
3884 android.MockFS{
3885 "package_bar/Android.bp": []byte(`
3886 aidl_library {
3887 name: "bar",
3888 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003889 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003890 strip_import_prefix: "x",
3891 }
3892 `)}.AddToFixture(),
3893 android.MockFS{
3894 "package_foo/Android.bp": []byte(`
3895 aidl_library {
3896 name: "foo",
3897 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003898 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003899 strip_import_prefix: "a",
3900 deps: ["bar"],
3901 }
3902 cc_library {
3903 name: "libfoo",
3904 aidl: {
3905 libs: ["foo"],
3906 }
3907 }
3908 `),
3909 }.AddToFixture(),
3910 ).RunTest(t).TestContext
3911
3912 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04003913
3914 android.AssertPathsRelativeToTopEquals(
3915 t,
3916 "aidl headers",
3917 []string{
3918 "package_bar/x/HeaderBar.aidl",
3919 "package_foo/a/HeaderFoo.aidl",
3920 "package_foo/a/b/Foo.aidl",
3921 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
3922 },
3923 libfoo.Rule("aidl_library").Implicits,
3924 )
3925
Colin Crossf61d03d2023-11-02 16:56:39 -07003926 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04003927 aidlCommand := manifest.Commands[0].GetCommand()
3928
3929 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
3930 if !strings.Contains(aidlCommand, expectedAidlFlags) {
3931 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
3932 }
3933
3934 outputs := strings.Join(libfoo.AllOutputs(), " ")
3935
Vinh Tran09581952023-05-16 16:03:20 -04003936 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
3937 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
3938 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003939 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
3940 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04003941 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
3942 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
3943 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003944 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
3945}
3946
Jooyung Hane197d8b2021-01-05 10:33:16 +09003947func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003948 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04003949 ctx := android.GroupFixturePreparers(
3950 prepareForCcTest,
3951 aidl_library.PrepareForTestWithAidlLibrary,
3952 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09003953 cc_library {
3954 name: "libfoo",
3955 srcs: ["a/Foo.aidl"],
3956 aidl: { flags: ["-Werror"], },
3957 }
3958 `)
3959
3960 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Colin Crossf61d03d2023-11-02 16:56:39 -07003961 manifest := android.RuleBuilderSboxProtoForTests(t, ctx.TestContext, libfoo.Output("aidl.sbox.textproto"))
Jooyung Hane197d8b2021-01-05 10:33:16 +09003962 aidlCommand := manifest.Commands[0].GetCommand()
3963 expectedAidlFlag := "-Werror"
3964 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3965 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3966 }
3967}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003968
Jooyung Han07f70c02021-11-06 07:08:45 +09003969func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003970 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09003971 for _, tc := range []struct {
3972 name string
3973 sdkVersion string
3974 variant string
3975 expected string
3976 }{
3977 {
3978 name: "default is current",
3979 sdkVersion: "",
3980 variant: "android_arm64_armv8-a_static",
3981 expected: "platform_apis",
3982 },
3983 {
3984 name: "use sdk_version",
3985 sdkVersion: `sdk_version: "29"`,
3986 variant: "android_arm64_armv8-a_static",
3987 expected: "platform_apis",
3988 },
3989 {
3990 name: "use sdk_version(sdk variant)",
3991 sdkVersion: `sdk_version: "29"`,
3992 variant: "android_arm64_armv8-a_sdk_static",
3993 expected: "29",
3994 },
3995 {
3996 name: "use min_sdk_version",
3997 sdkVersion: `min_sdk_version: "29"`,
3998 variant: "android_arm64_armv8-a_static",
3999 expected: "29",
4000 },
4001 } {
4002 t.Run(tc.name, func(t *testing.T) {
4003 ctx := testCc(t, `
4004 cc_library {
4005 name: "libfoo",
4006 stl: "none",
4007 srcs: ["a/Foo.aidl"],
4008 `+tc.sdkVersion+`
4009 }
4010 `)
4011 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
Colin Crossf61d03d2023-11-02 16:56:39 -07004012 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl.sbox.textproto"))
Jooyung Han07f70c02021-11-06 07:08:45 +09004013 aidlCommand := manifest.Commands[0].GetCommand()
4014 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4015 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4016 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4017 }
4018 })
4019 }
4020}
4021
Vinh Tran09581952023-05-16 16:03:20 -04004022func TestInvalidAidlProp(t *testing.T) {
4023 t.Parallel()
4024
4025 testCases := []struct {
4026 description string
4027 bp string
4028 }{
4029 {
4030 description: "Invalid use of aidl.libs and aidl.include_dirs",
4031 bp: `
4032 cc_library {
4033 name: "foo",
4034 aidl: {
4035 libs: ["foo_aidl"],
4036 include_dirs: ["bar/include"],
4037 }
4038 }
4039 `,
4040 },
4041 {
4042 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4043 bp: `
4044 cc_library {
4045 name: "foo",
4046 aidl: {
4047 libs: ["foo_aidl"],
4048 local_include_dirs: ["include"],
4049 }
4050 }
4051 `,
4052 },
4053 }
4054
4055 for _, testCase := range testCases {
4056 t.Run(testCase.description, func(t *testing.T) {
4057 bp := `
4058 aidl_library {
4059 name: "foo_aidl",
4060 srcs: ["Foo.aidl"],
4061 } ` + testCase.bp
4062 android.GroupFixturePreparers(
4063 prepareForCcTest,
4064 aidl_library.PrepareForTestWithAidlLibrary.
4065 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4066 ).RunTestWithBp(t, bp)
4067 })
4068 }
4069}
4070
Jiyong Parka008fb02021-03-16 17:15:53 +09004071func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004072 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004073 ctx := testCc(t, `
4074 cc_library_shared {
4075 name: "libfoo",
4076 srcs: ["foo.c"],
4077 min_sdk_version: "29",
4078 }`)
4079
4080 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4081 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4082}
4083
Vinh Tranf1924742022-06-24 16:40:11 -04004084func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004085 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004086 bp := `
4087 cc_library_shared {
4088 name: "libfoo",
4089 srcs: ["foo.c"],
4090 min_sdk_version: "S",
4091 }
4092 `
4093 result := android.GroupFixturePreparers(
4094 prepareForCcTest,
4095 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4096 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4097 }),
4098 ).RunTestWithBp(t, bp)
4099 ctx := result.TestContext
4100 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4101 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4102}
4103
Paul Duffin3cb603e2021-02-19 13:57:10 +00004104func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004105 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004106
4107 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4108 // embedded newline characters alone.
4109 trimIndentingSpaces := func(s string) string {
4110 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4111 }
4112
4113 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4114 t.Helper()
4115 expected = trimIndentingSpaces(expected)
4116 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4117 if expected != actual {
4118 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4119 }
4120 }
4121
4122 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4123
4124 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4125 t.Helper()
Colin Cross5a377182023-12-14 14:46:23 -08004126 exported, _ := android.SingletonModuleProvider(ctx, module, FlagExporterInfoProvider)
Paul Duffin3cb603e2021-02-19 13:57:10 +00004127 name := module.Name()
4128
4129 for _, checker := range checkers {
4130 checker(t, name, exported)
4131 }
4132 }
4133
4134 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4135 return func(t *testing.T, name string, exported FlagExporterInfo) {
4136 t.Helper()
4137 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4138 }
4139 }
4140
4141 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4142 return func(t *testing.T, name string, exported FlagExporterInfo) {
4143 t.Helper()
4144 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4145 }
4146 }
4147
4148 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4149 return func(t *testing.T, name string, exported FlagExporterInfo) {
4150 t.Helper()
4151 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4152 }
4153 }
4154
4155 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4156 return func(t *testing.T, name string, exported FlagExporterInfo) {
4157 t.Helper()
4158 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4159 }
4160 }
4161
4162 genRuleModules := `
4163 genrule {
4164 name: "genrule_foo",
4165 cmd: "generate-foo",
4166 out: [
4167 "generated_headers/foo/generated_header.h",
4168 ],
4169 export_include_dirs: [
4170 "generated_headers",
4171 ],
4172 }
4173
4174 genrule {
4175 name: "genrule_bar",
4176 cmd: "generate-bar",
4177 out: [
4178 "generated_headers/bar/generated_header.h",
4179 ],
4180 export_include_dirs: [
4181 "generated_headers",
4182 ],
4183 }
4184 `
4185
4186 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4187 ctx := testCc(t, genRuleModules+`
4188 cc_library {
4189 name: "libfoo",
4190 srcs: ["foo.c"],
4191 export_include_dirs: ["foo/standard"],
4192 export_system_include_dirs: ["foo/system"],
4193 generated_headers: ["genrule_foo"],
4194 export_generated_headers: ["genrule_foo"],
4195 }
4196
4197 cc_library {
4198 name: "libbar",
4199 srcs: ["bar.c"],
4200 shared_libs: ["libfoo"],
4201 export_include_dirs: ["bar/standard"],
4202 export_system_include_dirs: ["bar/system"],
4203 generated_headers: ["genrule_bar"],
4204 export_generated_headers: ["genrule_bar"],
4205 }
4206 `)
4207 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4208 checkIncludeDirs(t, ctx, foo,
4209 expectedIncludeDirs(`
4210 foo/standard
4211 .intermediates/genrule_foo/gen/generated_headers
4212 `),
4213 expectedSystemIncludeDirs(`foo/system`),
4214 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4215 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4216 )
4217
4218 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4219 checkIncludeDirs(t, ctx, bar,
4220 expectedIncludeDirs(`
4221 bar/standard
4222 .intermediates/genrule_bar/gen/generated_headers
4223 `),
4224 expectedSystemIncludeDirs(`bar/system`),
4225 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4226 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4227 )
4228 })
4229
4230 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4231 ctx := testCc(t, genRuleModules+`
4232 cc_library {
4233 name: "libfoo",
4234 srcs: ["foo.c"],
4235 export_include_dirs: ["foo/standard"],
4236 export_system_include_dirs: ["foo/system"],
4237 generated_headers: ["genrule_foo"],
4238 export_generated_headers: ["genrule_foo"],
4239 }
4240
4241 cc_library {
4242 name: "libbar",
4243 srcs: ["bar.c"],
4244 whole_static_libs: ["libfoo"],
4245 export_include_dirs: ["bar/standard"],
4246 export_system_include_dirs: ["bar/system"],
4247 generated_headers: ["genrule_bar"],
4248 export_generated_headers: ["genrule_bar"],
4249 }
4250 `)
4251 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4252 checkIncludeDirs(t, ctx, foo,
4253 expectedIncludeDirs(`
4254 foo/standard
4255 .intermediates/genrule_foo/gen/generated_headers
4256 `),
4257 expectedSystemIncludeDirs(`foo/system`),
4258 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4259 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4260 )
4261
4262 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4263 checkIncludeDirs(t, ctx, bar,
4264 expectedIncludeDirs(`
4265 bar/standard
4266 foo/standard
4267 .intermediates/genrule_foo/gen/generated_headers
4268 .intermediates/genrule_bar/gen/generated_headers
4269 `),
4270 expectedSystemIncludeDirs(`
4271 bar/system
4272 foo/system
4273 `),
4274 expectedGeneratedHeaders(`
4275 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4276 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4277 `),
4278 expectedOrderOnlyDeps(`
4279 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4280 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4281 `),
4282 )
4283 })
4284
Paul Duffin3cb603e2021-02-19 13:57:10 +00004285 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004286 ctx := android.GroupFixturePreparers(
4287 prepareForCcTest,
4288 aidl_library.PrepareForTestWithAidlLibrary,
4289 ).RunTestWithBp(t, `
4290 aidl_library {
4291 name: "libfoo_aidl",
4292 srcs: ["x/y/Bar.aidl"],
4293 strip_import_prefix: "x",
4294 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004295 cc_library_shared {
4296 name: "libfoo",
4297 srcs: [
4298 "foo.c",
4299 "b.aidl",
4300 "a.proto",
4301 ],
4302 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004303 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004304 export_aidl_headers: true,
4305 }
4306 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004307 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004308 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4309 checkIncludeDirs(t, ctx, foo,
4310 expectedIncludeDirs(`
4311 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004312 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004313 `),
4314 expectedSystemIncludeDirs(``),
4315 expectedGeneratedHeaders(`
4316 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4317 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004319 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4320 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4321 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004322 `),
4323 expectedOrderOnlyDeps(`
4324 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4325 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4326 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004327 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4328 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4329 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004330 `),
4331 )
4332 })
4333
Paul Duffin3cb603e2021-02-19 13:57:10 +00004334 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4335 ctx := testCc(t, genRuleModules+`
4336 cc_library_shared {
4337 name: "libfoo",
4338 srcs: [
4339 "foo.c",
4340 "b.aidl",
4341 "a.proto",
4342 ],
4343 proto: {
4344 export_proto_headers: true,
4345 }
4346 }
4347 `)
4348 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4349 checkIncludeDirs(t, ctx, foo,
4350 expectedIncludeDirs(`
4351 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4352 `),
4353 expectedSystemIncludeDirs(``),
4354 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004355 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4356 `),
4357 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004358 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4359 `),
4360 )
4361 })
4362
Paul Duffin33056e82021-02-19 13:49:08 +00004363 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004364 ctx := testCc(t, genRuleModules+`
4365 cc_library_shared {
4366 name: "libfoo",
4367 srcs: [
4368 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004369 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004370 "b.aidl",
4371 "a.proto",
4372 ],
4373 }
4374 `)
4375 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4376 checkIncludeDirs(t, ctx, foo,
4377 expectedIncludeDirs(`
4378 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4379 `),
4380 expectedSystemIncludeDirs(``),
4381 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004382 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004383 `),
4384 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004385 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4386 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004387 `),
4388 )
4389 })
4390}
Colin Crossae628182021-06-14 16:52:28 -07004391
4392func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004393 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004394 baseExpectedFlags := []string{
4395 "${config.ArmThumbCflags}",
4396 "${config.ArmCflags}",
4397 "${config.CommonGlobalCflags}",
4398 "${config.DeviceGlobalCflags}",
4399 "${config.ExternalCflags}",
4400 "${config.ArmToolchainCflags}",
4401 "${config.ArmArmv7ANeonCflags}",
4402 "${config.ArmGenericCflags}",
4403 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004404 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004405 }
4406
4407 expectedIncludes := []string{
4408 "external/foo/android_arm_export_include_dirs",
4409 "external/foo/lib32_export_include_dirs",
4410 "external/foo/arm_export_include_dirs",
4411 "external/foo/android_export_include_dirs",
4412 "external/foo/linux_export_include_dirs",
4413 "external/foo/export_include_dirs",
4414 "external/foo/android_arm_local_include_dirs",
4415 "external/foo/lib32_local_include_dirs",
4416 "external/foo/arm_local_include_dirs",
4417 "external/foo/android_local_include_dirs",
4418 "external/foo/linux_local_include_dirs",
4419 "external/foo/local_include_dirs",
4420 "external/foo",
4421 "external/foo/libheader1",
4422 "external/foo/libheader2",
4423 "external/foo/libwhole1",
4424 "external/foo/libwhole2",
4425 "external/foo/libstatic1",
4426 "external/foo/libstatic2",
4427 "external/foo/libshared1",
4428 "external/foo/libshared2",
4429 "external/foo/liblinux",
4430 "external/foo/libandroid",
4431 "external/foo/libarm",
4432 "external/foo/lib32",
4433 "external/foo/libandroid_arm",
4434 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004435 }
4436
4437 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4438 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4439
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004440 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004441 cstd := []string{"-std=gnu17", "-std=conly"}
Elliott Hughesc79d9e32022-01-13 14:56:02 -08004442 cppstd := []string{"-std=gnu++20", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004443
4444 lastIncludes := []string{
4445 "out/soong/ndk/sysroot/usr/include",
4446 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4447 }
4448
4449 combineSlices := func(slices ...[]string) []string {
4450 var ret []string
4451 for _, s := range slices {
4452 ret = append(ret, s...)
4453 }
4454 return ret
4455 }
4456
4457 testCases := []struct {
4458 name string
4459 src string
4460 expected []string
4461 }{
4462 {
4463 name: "c",
4464 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004465 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004466 },
4467 {
4468 name: "cc",
4469 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004470 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004471 },
4472 {
4473 name: "assemble",
4474 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004475 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004476 },
4477 }
4478
4479 for _, tc := range testCases {
4480 t.Run(tc.name, func(t *testing.T) {
4481 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004482 cc_library {
4483 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004484 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004485 cflags: ["-std=candcpp"],
4486 conlyflags: ["-std=conly"],
4487 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004488 local_include_dirs: ["local_include_dirs"],
4489 export_include_dirs: ["export_include_dirs"],
4490 export_system_include_dirs: ["export_system_include_dirs"],
4491 static_libs: ["libstatic1", "libstatic2"],
4492 whole_static_libs: ["libwhole1", "libwhole2"],
4493 shared_libs: ["libshared1", "libshared2"],
4494 header_libs: ["libheader1", "libheader2"],
4495 target: {
4496 android: {
4497 shared_libs: ["libandroid"],
4498 local_include_dirs: ["android_local_include_dirs"],
4499 export_include_dirs: ["android_export_include_dirs"],
4500 },
4501 android_arm: {
4502 shared_libs: ["libandroid_arm"],
4503 local_include_dirs: ["android_arm_local_include_dirs"],
4504 export_include_dirs: ["android_arm_export_include_dirs"],
4505 },
4506 linux: {
4507 shared_libs: ["liblinux"],
4508 local_include_dirs: ["linux_local_include_dirs"],
4509 export_include_dirs: ["linux_export_include_dirs"],
4510 },
4511 },
4512 multilib: {
4513 lib32: {
4514 shared_libs: ["lib32"],
4515 local_include_dirs: ["lib32_local_include_dirs"],
4516 export_include_dirs: ["lib32_export_include_dirs"],
4517 },
4518 },
4519 arch: {
4520 arm: {
4521 shared_libs: ["libarm"],
4522 local_include_dirs: ["arm_local_include_dirs"],
4523 export_include_dirs: ["arm_export_include_dirs"],
4524 },
4525 },
4526 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004527 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004528 }
4529
4530 cc_library_headers {
4531 name: "libheader1",
4532 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004533 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004534 stl: "none",
4535 }
4536
4537 cc_library_headers {
4538 name: "libheader2",
4539 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004540 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004541 stl: "none",
4542 }
Liz Kammer08572c62021-09-30 10:11:04 -04004543 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004544
Liz Kammer08572c62021-09-30 10:11:04 -04004545 libs := []string{
4546 "libstatic1",
4547 "libstatic2",
4548 "libwhole1",
4549 "libwhole2",
4550 "libshared1",
4551 "libshared2",
4552 "libandroid",
4553 "libandroid_arm",
4554 "liblinux",
4555 "lib32",
4556 "libarm",
4557 }
Colin Crossae628182021-06-14 16:52:28 -07004558
Liz Kammer08572c62021-09-30 10:11:04 -04004559 for _, lib := range libs {
4560 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004561 cc_library {
4562 name: "%s",
4563 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004564 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004565 stl: "none",
4566 }
4567 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004568 }
4569
4570 ctx := android.GroupFixturePreparers(
4571 PrepareForIntegrationTestWithCc,
4572 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4573 ).RunTest(t)
4574 // Use the arm variant instead of the arm64 variant so that it gets headers from
4575 // ndk_libandroid_support to test LateStaticLibs.
4576 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4577
4578 var includes []string
4579 flags := strings.Split(cflags, " ")
4580 for _, flag := range flags {
4581 if strings.HasPrefix(flag, "-I") {
4582 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4583 } else if flag == "-isystem" {
4584 // skip isystem, include next
4585 } else if len(flag) > 0 {
4586 includes = append(includes, flag)
4587 }
4588 }
4589
4590 android.AssertArrayString(t, "includes", tc.expected, includes)
4591 })
Colin Crossae628182021-06-14 16:52:28 -07004592 }
4593
Colin Crossae628182021-06-14 16:52:28 -07004594}
Alixb5f6d9e2022-04-20 23:00:58 +00004595
zijunzhao933e3802023-01-12 07:26:20 +00004596func TestAddnoOverride64GlobalCflags(t *testing.T) {
4597 t.Parallel()
4598 ctx := testCc(t, `
4599 cc_library_shared {
4600 name: "libclient",
4601 srcs: ["foo.c"],
4602 shared_libs: ["libfoo#1"],
4603 }
4604
4605 cc_library_shared {
4606 name: "libfoo",
4607 srcs: ["foo.c"],
4608 shared_libs: ["libbar"],
4609 export_shared_lib_headers: ["libbar"],
4610 stubs: {
4611 symbol_file: "foo.map.txt",
4612 versions: ["1", "2", "3"],
4613 },
4614 }
4615
4616 cc_library_shared {
4617 name: "libbar",
4618 export_include_dirs: ["include/libbar"],
4619 srcs: ["foo.c"],
4620 }`)
4621
4622 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4623
4624 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4625 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4626 }
4627}
4628
Alixb5f6d9e2022-04-20 23:00:58 +00004629func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004630 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004631 tests := []struct {
4632 name string
4633 clang bool
4634 BuildBrokenClangProperty bool
4635 err string
4636 }{
4637 {
4638 name: "error when clang is set to false",
4639 clang: false,
4640 err: "is no longer supported",
4641 },
4642 {
4643 name: "error when clang is set to true",
4644 clang: true,
4645 err: "property is deprecated, see Changes.md",
4646 },
4647 {
4648 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4649 clang: true,
4650 BuildBrokenClangProperty: true,
4651 },
4652 }
4653
4654 for _, test := range tests {
4655 t.Run(test.name, func(t *testing.T) {
4656 bp := fmt.Sprintf(`
4657 cc_library {
4658 name: "foo",
4659 clang: %t,
4660 }`, test.clang)
4661
4662 if test.err == "" {
4663 android.GroupFixturePreparers(
4664 prepareForCcTest,
4665 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4666 if test.BuildBrokenClangProperty {
4667 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4668 }
4669 }),
4670 ).RunTestWithBp(t, bp)
4671 } else {
4672 prepareForCcTest.
4673 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4674 RunTestWithBp(t, bp)
4675 }
4676 })
4677 }
4678}
Alix Espinoef47e542022-09-14 19:10:51 +00004679
4680func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004681 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004682 tests := []struct {
4683 name string
4684 clangAsFlags []string
4685 BuildBrokenClangAsFlags bool
4686 err string
4687 }{
4688 {
4689 name: "error when clang_asflags is set",
4690 clangAsFlags: []string{"-a", "-b"},
4691 err: "clang_asflags: property is deprecated",
4692 },
4693 {
4694 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4695 clangAsFlags: []string{"-a", "-b"},
4696 BuildBrokenClangAsFlags: true,
4697 },
4698 }
4699
4700 for _, test := range tests {
4701 t.Run(test.name, func(t *testing.T) {
4702 bp := fmt.Sprintf(`
4703 cc_library {
4704 name: "foo",
4705 clang_asflags: %s,
4706 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4707
4708 if test.err == "" {
4709 android.GroupFixturePreparers(
4710 prepareForCcTest,
4711 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4712 if test.BuildBrokenClangAsFlags {
4713 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4714 }
4715 }),
4716 ).RunTestWithBp(t, bp)
4717 } else {
4718 prepareForCcTest.
4719 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4720 RunTestWithBp(t, bp)
4721 }
4722 })
4723 }
4724}
4725
4726func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004727 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004728 tests := []struct {
4729 name string
4730 clangCFlags []string
4731 BuildBrokenClangCFlags bool
4732 err string
4733 }{
4734 {
4735 name: "error when clang_cflags is set",
4736 clangCFlags: []string{"-a", "-b"},
4737 err: "clang_cflags: property is deprecated",
4738 },
4739 {
4740 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4741 clangCFlags: []string{"-a", "-b"},
4742 BuildBrokenClangCFlags: true,
4743 },
4744 }
4745
4746 for _, test := range tests {
4747 t.Run(test.name, func(t *testing.T) {
4748 bp := fmt.Sprintf(`
4749 cc_library {
4750 name: "foo",
4751 clang_cflags: %s,
4752 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4753
4754 if test.err == "" {
4755 android.GroupFixturePreparers(
4756 prepareForCcTest,
4757 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4758 if test.BuildBrokenClangCFlags {
4759 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4760 }
4761 }),
4762 ).RunTestWithBp(t, bp)
4763 } else {
4764 prepareForCcTest.
4765 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4766 RunTestWithBp(t, bp)
4767 }
4768 })
4769 }
4770}
Wei Li5f5d2712023-12-11 15:40:29 -08004771
4772func TestStrippedAllOutputFile(t *testing.T) {
4773 t.Parallel()
4774 bp := `
4775 cc_library {
4776 name: "test_lib",
4777 srcs: ["test_lib.cpp"],
4778 dist: {
4779 targets: [ "dist_target" ],
4780 tag: "stripped_all",
4781 }
4782 }
4783 `
4784 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
4785 ctx := testCcWithConfig(t, config)
4786 module := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared").Module()
4787 outputFile, err := module.(android.OutputFileProducer).OutputFiles("stripped_all")
4788 if err != nil {
4789 t.Errorf("Expected cc_library to produce output files, error: %s", err)
4790 return
4791 }
4792 if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") {
4793 t.Errorf("Unexpected output file: %s", outputFile.Strings()[0])
4794 return
4795 }
4796}
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +09004797
4798// TODO(b/316829758) Remove this test and do not set VNDK version from other tests
4799func TestImageVariantsWithoutVndk(t *testing.T) {
4800 t.Parallel()
4801
4802 bp := `
4803 cc_binary {
4804 name: "binfoo",
4805 srcs: ["binfoo.cc"],
4806 vendor_available: true,
4807 product_available: true,
4808 shared_libs: ["libbar"]
4809 }
4810 cc_library {
4811 name: "libbar",
4812 srcs: ["libbar.cc"],
4813 vendor_available: true,
4814 product_available: true,
4815 }
4816 `
4817
4818 ctx := prepareForCcTestWithoutVndk.RunTestWithBp(t, bp)
4819
4820 hasDep := func(m android.Module, wantDep android.Module) bool {
4821 t.Helper()
4822 var found bool
4823 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
4824 if dep == wantDep {
4825 found = true
4826 }
4827 })
4828 return found
4829 }
4830
4831 testDepWithVariant := func(imageVariant string) {
4832 imageVariantStr := ""
4833 if imageVariant != "core" {
4834 imageVariantStr = "_" + imageVariant
4835 }
4836 binFooModule := ctx.ModuleForTests("binfoo", "android"+imageVariantStr+"_arm64_armv8-a").Module()
4837 libBarModule := ctx.ModuleForTests("libbar", "android"+imageVariantStr+"_arm64_armv8-a_shared").Module()
4838 android.AssertBoolEquals(t, "binfoo should have dependency on libbar with image variant "+imageVariant, true, hasDep(binFooModule, libBarModule))
4839 }
4840
4841 testDepWithVariant("core")
4842 testDepWithVariant("vendor")
4843 testDepWithVariant("product")
4844}
Yi-Yo Chiang88960aa2024-01-19 15:02:29 +08004845
4846func TestVendorSdkVersionWithoutVndk(t *testing.T) {
4847 t.Parallel()
4848
4849 bp := `
4850 cc_library {
4851 name: "libfoo",
4852 srcs: ["libfoo.cc"],
4853 vendor_available: true,
4854 }
4855
4856 cc_library {
4857 name: "libbar",
4858 srcs: ["libbar.cc"],
4859 vendor_available: true,
4860 min_sdk_version: "29",
4861 }
4862 `
4863
4864 ctx := prepareForCcTestWithoutVndk.RunTestWithBp(t, bp)
4865 testSdkVersionFlag := func(module, version string) {
4866 flags := ctx.ModuleForTests(module, "android_vendor_arm64_armv8-a_static").Rule("cc").Args["cFlags"]
4867 android.AssertStringDoesContain(t, "min sdk version", flags, "-target aarch64-linux-android"+version)
4868 }
4869
4870 testSdkVersionFlag("libfoo", "10000")
4871 testSdkVersionFlag("libbar", "29")
4872
4873 ctx = android.GroupFixturePreparers(
4874 prepareForCcTestWithoutVndk,
4875 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4876 if variables.BuildFlags == nil {
4877 variables.BuildFlags = make(map[string]string)
4878 }
4879 variables.BuildFlags["RELEASE_BOARD_API_LEVEL_FROZEN"] = "true"
4880 }),
4881 ).RunTestWithBp(t, bp)
4882 testSdkVersionFlag("libfoo", "30")
4883 testSdkVersionFlag("libbar", "29")
4884}