blob: cc5254773649e44bb15848957eee8915759f1c2c [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
Vinh Tran367d89d2023-04-28 11:21:25 -040027 "android/soong/aidl_library"
Colin Crosse1bb5d02019-09-24 14:55:04 -070028 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050029 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070030)
31
Yu Liue4312402023-01-18 09:15:31 -080032func init() {
33 registerTestMutators(android.InitRegistrationContext)
34}
35
Jiyong Park6a43f042017-10-12 23:05:00 +090036func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000037 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090038}
39
Paul Duffin2e6f90e2021-03-22 23:20:25 +000040var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000041 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000042 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Justin Yun41cbb5e2023-11-29 17:58:16 +090043 variables.VendorApiLevel = StringPtr("202404")
Paul Duffin02a3d652021-02-24 18:51:54 +000044 variables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090045 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000046 }),
47)
48
Yu Liue4312402023-01-18 09:15:31 -080049var ccLibInApex = "cc_lib_in_apex"
50var apexVariationName = "apex28"
51var apexVersion = "28"
52
53func registerTestMutators(ctx android.RegistrationContext) {
54 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
55 ctx.BottomUp("apex", testApexMutator).Parallel()
56 ctx.BottomUp("mixed_builds_prep", mixedBuildsPrepareMutator).Parallel()
57 })
58}
59
60func mixedBuildsPrepareMutator(ctx android.BottomUpMutatorContext) {
61 if m := ctx.Module(); m.Enabled() {
62 if mixedBuildMod, ok := m.(android.MixedBuildBuildable); ok {
MarkDacekf47e1422023-04-19 16:47:36 +000063 if mixedBuildMod.IsMixedBuildSupported(ctx) && android.MixedBuildsEnabled(ctx) == android.MixedBuildEnabled {
Yu Liue4312402023-01-18 09:15:31 -080064 mixedBuildMod.QueueBazelCall(ctx)
65 }
66 }
67 }
68}
69
70func testApexMutator(mctx android.BottomUpMutatorContext) {
71 modules := mctx.CreateVariations(apexVariationName)
72 apexInfo := android.ApexInfo{
73 ApexVariationName: apexVariationName,
74 MinSdkVersion: android.ApiLevelForTest(apexVersion),
75 }
76 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
77}
78
Paul Duffin8567f222021-03-23 00:02:06 +000079// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000080//
81// See testCc for an explanation as to how to stop using this deprecated method.
82//
83// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080084func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070085 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000086 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000087 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090088}
89
Paul Duffin8567f222021-03-23 00:02:06 +000090// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000091//
Paul Duffin8567f222021-03-23 00:02:06 +000092// 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 +000093// easier to customize the test behavior.
94//
95// 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 +000096// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000097// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
98// that it did not change the test behavior unexpectedly.
99//
100// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800101func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800102 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +0000103 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +0000104 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +0800105}
106
Paul Duffin8567f222021-03-23 00:02:06 +0000107// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000108//
109// See testCc for an explanation as to how to stop using this deprecated method.
110//
111// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900112func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800113 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800114
Paul Duffin8567f222021-03-23 00:02:06 +0000115 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000116 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
117 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800118}
119
Paul Duffin8567f222021-03-23 00:02:06 +0000120// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900125func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900126 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000127 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900128 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900129 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900130 testCcErrorWithConfig(t, pattern, config)
131 return
132}
133
Paul Duffin8567f222021-03-23 00:02:06 +0000134// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000135//
136// See testCc for an explanation as to how to stop using this deprecated method.
137//
138// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900139func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900140 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000141 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900142 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900143 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900144 testCcErrorWithConfig(t, pattern, config)
145 return
146}
147
Logan Chienf3511742017-10-31 18:04:35 +0800148const (
Colin Cross7113d202019-11-20 16:39:12 -0800149 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900150 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
151 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800152 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800153)
154
Paul Duffindb462dd2021-03-21 22:01:55 +0000155// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
156// running it in a fixture that requires all source files to exist.
157func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
158 android.GroupFixturePreparers(
159 PrepareForTestWithCcDefaultModules,
160 android.PrepareForTestDisallowNonExistentPaths,
161 ).RunTest(t)
162}
163
Jiyong Park6a43f042017-10-12 23:05:00 +0900164func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400165 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900166 ctx := testCc(t, `
167 cc_library {
168 name: "libTest",
169 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700170 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800171 nocrt: true,
172 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900173 vendor_available: true,
174 target: {
175 vendor: {
176 srcs: ["bar.c"],
177 },
178 },
179 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900180 `)
181
Logan Chienf3511742017-10-31 18:04:35 +0800182 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900183 var objs []string
184 for _, o := range ld.Inputs {
185 objs = append(objs, o.Base())
186 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800187 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900188 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
189 }
190}
191
Justin Yun7f99ec72021-04-12 13:19:28 +0900192func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
193 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
194 partitionDefined := false
195 checkPartition := func(specific bool, partition string) {
196 if specific {
197 if expected != partition && !partitionDefined {
198 // The variant is installed to the 'partition'
199 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
200 }
201 partitionDefined = true
202 } else {
203 // The variant is not installed to the 'partition'
204 if expected == partition {
205 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
206 }
207 }
208 }
209 socSpecific := func(m *Module) bool {
210 return m.SocSpecific() || m.socSpecificModuleContext()
211 }
212 deviceSpecific := func(m *Module) bool {
213 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
214 }
215 productSpecific := func(m *Module) bool {
216 return m.ProductSpecific() || m.productSpecificModuleContext()
217 }
218 systemExtSpecific := func(m *Module) bool {
219 return m.SystemExtSpecific()
220 }
221 checkPartition(socSpecific(mod), "vendor")
222 checkPartition(deviceSpecific(mod), "odm")
223 checkPartition(productSpecific(mod), "product")
224 checkPartition(systemExtSpecific(mod), "system_ext")
225 if !partitionDefined && expected != "system" {
226 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
227 " but installed to system partition", variant, name, expected)
228 }
229}
230
231func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400232 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900233 t.Helper()
234 ctx := prepareForCcTest.RunTestWithBp(t, `
235 cc_library {
236 name: "libsystem",
237 }
238 cc_library {
239 name: "libsystem_ext",
240 system_ext_specific: true,
241 }
242 cc_library {
243 name: "libproduct",
244 product_specific: true,
245 }
246 cc_library {
247 name: "libvendor",
248 vendor: true,
249 }
250 cc_library {
251 name: "libodm",
252 device_specific: true,
253 }
254 cc_library {
255 name: "liball_available",
256 vendor_available: true,
257 product_available: true,
258 }
259 cc_library {
260 name: "libsystem_ext_all_available",
261 system_ext_specific: true,
262 vendor_available: true,
263 product_available: true,
264 }
265 cc_library {
266 name: "liball_available_odm",
267 odm_available: true,
268 product_available: true,
269 }
270 cc_library {
271 name: "libproduct_vendoravailable",
272 product_specific: true,
273 vendor_available: true,
274 }
275 cc_library {
276 name: "libproduct_odmavailable",
277 product_specific: true,
278 odm_available: true,
279 }
280 `).TestContext
281
282 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
283 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
284 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
285 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
286 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
287
288 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
289 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
290 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
291
292 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
293 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
294 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
295
296 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
297 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
298 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
299
300 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
301 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
302
303 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
304 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
305}
306
Logan Chienf3511742017-10-31 18:04:35 +0800307func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900308 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800309
Logan Chiend3c59a22018-03-29 14:08:15 +0800310 t.Helper()
311
Justin Yun0ecf0b22020-02-28 15:07:59 +0900312 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800313
314 // Check library properties.
315 lib, ok := mod.compiler.(*libraryDecorator)
316 if !ok {
317 t.Errorf("%q must have libraryDecorator", name)
318 } else if lib.baseInstaller.subDir != subDir {
319 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
320 lib.baseInstaller.subDir)
321 }
322
323 // Check VNDK properties.
324 if mod.vndkdep == nil {
325 t.Fatalf("%q must have `vndkdep`", name)
326 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700327 if !mod.IsVndk() {
328 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800329 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400330 if mod.IsVndkSp() != isVndkSp {
331 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800332 }
333
334 // Check VNDK extension properties.
335 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500336 if mod.IsVndkExt() != isVndkExt {
337 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800338 }
339
340 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
341 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
342 }
343}
344
Jooyung Han2216fb12019-11-06 16:46:15 +0900345func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
346 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800347 content := android.ContentFromFileRuleForTests(t, params)
348 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900349 assertArrayString(t, actual, expected)
350}
351
Jooyung Han097087b2019-10-22 19:32:18 +0900352func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
353 t.Helper()
354 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900355 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
356}
357
358func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
359 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800360 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800361 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900362}
363
Logan Chienf3511742017-10-31 18:04:35 +0800364func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400365 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800366 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800367 cc_library {
368 name: "libvndk",
369 vendor_available: true,
370 vndk: {
371 enabled: true,
372 },
373 nocrt: true,
374 }
375
376 cc_library {
377 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900378 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800379 vndk: {
380 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900381 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800382 },
383 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900384 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800385 }
386
387 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900388 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800389 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900390 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800391 vndk: {
392 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900393 },
394 nocrt: true,
395 target: {
396 vendor: {
397 cflags: ["-DTEST"],
398 },
399 product: {
400 cflags: ["-DTEST"],
401 },
402 },
403 }
404
405 cc_library {
406 name: "libvndk_sp",
407 vendor_available: true,
408 vndk: {
409 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 support_system_process: true,
411 },
412 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900413 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800414 }
415
416 cc_library {
417 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900418 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800419 vndk: {
420 enabled: true,
421 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900422 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800423 },
424 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900425 target: {
426 vendor: {
427 suffix: "-x",
428 },
429 },
Logan Chienf3511742017-10-31 18:04:35 +0800430 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900431
432 cc_library {
433 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900434 vendor_available: true,
435 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900436 vndk: {
437 enabled: true,
438 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900439 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900440 },
441 nocrt: true,
442 target: {
443 vendor: {
444 suffix: "-x",
445 },
446 product: {
447 suffix: "-x",
448 },
449 },
450 }
451
Justin Yun450ae722021-04-16 19:58:18 +0900452 cc_library {
453 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700454 llndk: {
455 symbol_file: "libllndk.map.txt",
456 export_llndk_headers: ["libllndk_headers"],
457 }
Justin Yun450ae722021-04-16 19:58:18 +0900458 }
459
Justin Yun611e8862021-05-24 18:17:33 +0900460 cc_library {
461 name: "libclang_rt.hwasan-llndk",
462 llndk: {
463 symbol_file: "libclang_rt.hwasan.map.txt",
464 }
465 }
466
Colin Cross627280f2021-04-26 16:53:58 -0700467 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900468 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700469 llndk: {
470 llndk_headers: true,
471 },
Justin Yun450ae722021-04-16 19:58:18 +0900472 export_include_dirs: ["include"],
473 }
474
Colin Crosse4e44bc2020-12-28 13:50:21 -0800475 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900476 name: "llndk.libraries.txt",
477 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800478 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900479 name: "vndkcore.libraries.txt",
480 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800481 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900482 name: "vndksp.libraries.txt",
483 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800484 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900485 name: "vndkprivate.libraries.txt",
486 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800487 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900488 name: "vndkproduct.libraries.txt",
489 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800490 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900491 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800492 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900493 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800494 `
495
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000496 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800497 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900498 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800499
500 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800501
Jooyung Han261e1582020-10-20 18:54:21 +0900502 // subdir == "" because VNDK libs are not supposed to be installed separately.
503 // They are installed as part of VNDK APEX instead.
504 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
505 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900506 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900507 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
508 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900509 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900510
Justin Yun6977e8a2020-10-29 18:24:11 +0900511 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
512 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900513
Inseob Kim1f086e22019-05-09 13:29:15 +0900514 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900515 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000516 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900517
518 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
519 "arm64", "armv8-a"))
520 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
521 "arm", "armv7-a-neon"))
522
523 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
524 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900525 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
526
Inseob Kim1f086e22019-05-09 13:29:15 +0900527 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
528 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900529 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900530
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900531 variant := "android_vendor.29_arm64_armv8-a_shared"
532 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900533
Inseob Kim7f283f42020-06-01 21:53:49 +0900534 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
535
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400536 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
537 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
538 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
539 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
540 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
541 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
542 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
543 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900544
Jooyung Han39edb6c2019-11-06 16:53:07 +0900545 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800546 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
547 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
548 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
549 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
550 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900551
Jooyung Han097087b2019-10-22 19:32:18 +0900552 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
553 "LLNDK: libc.so",
554 "LLNDK: libdl.so",
555 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900556 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900557 "LLNDK: libm.so",
558 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900559 "VNDK-SP: libvndk_sp-x.so",
560 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900561 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900562 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900563 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900564 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900565 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900566 "VNDK-private: libvndk-private.so",
567 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900568 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900569 "VNDK-product: libc++.so",
570 "VNDK-product: libvndk_product.so",
571 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900572 })
Justin Yun611e8862021-05-24 18:17:33 +0900573 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 +0900574 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
575 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
576 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 +0900577 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 +0900578 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
579}
580
Yo Chiangbba545e2020-06-09 16:15:37 +0800581func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400582 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800583 ctx := testCc(t, `
584 cc_library {
585 name: "libvndk_host_supported",
586 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900587 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800588 vndk: {
589 enabled: true,
590 },
591 host_supported: true,
592 }
593
594 cc_library {
595 name: "libvndk_host_supported_but_disabled_on_device",
596 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900597 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800598 vndk: {
599 enabled: true,
600 },
601 host_supported: true,
602 enabled: false,
603 target: {
604 host: {
605 enabled: true,
606 }
607 }
608 }
609
Colin Crosse4e44bc2020-12-28 13:50:21 -0800610 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800611 name: "vndkcore.libraries.txt",
612 }
613 `)
614
615 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
616}
617
Jooyung Han2216fb12019-11-06 16:46:15 +0900618func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400619 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800620 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800621 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900622 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800623 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800624 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000625 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800626 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900627 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900628 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800629 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900630
Colin Cross45bce852021-11-11 22:47:54 -0800631 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700632 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900633 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900634}
635
636func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400637 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800638 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900639 cc_library {
640 name: "libvndk",
641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900642 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900643 vndk: {
644 enabled: true,
645 },
646 nocrt: true,
647 }
648
649 cc_library {
650 name: "libvndk_sp",
651 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900652 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900653 vndk: {
654 enabled: true,
655 support_system_process: true,
656 },
657 nocrt: true,
658 }
659
660 cc_library {
661 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900662 vendor_available: true,
663 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900664 vndk: {
665 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900666 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900667 },
668 nocrt: true,
669 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900670
Colin Crosse4e44bc2020-12-28 13:50:21 -0800671 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900672 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800673 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900674 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800675 `
676
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000677 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800678 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900679 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800680 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900681 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800682
683 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
684
685 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900686
Jooyung Han2216fb12019-11-06 16:46:15 +0900687 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900688}
689
Chris Parsons79d66a52020-06-05 17:26:16 -0400690func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400691 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400692 bp := `
693 cc_test_library {
694 name: "test_lib",
695 srcs: ["test_lib.cpp"],
696 gtest: false,
697 }
698
699 cc_test {
700 name: "main_test",
701 data_libs: ["test_lib"],
702 gtest: false,
703 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400704 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400705
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000706 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400707 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900708 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400709 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
710
711 ctx := testCcWithConfig(t, config)
712 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
713 testBinary := module.(*Module).linker.(*testBinary)
714 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
715 if err != nil {
716 t.Errorf("Expected cc_test to produce output files, error: %s", err)
717 return
718 }
719 if len(outputFiles) != 1 {
720 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
721 return
722 }
723 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800724 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons79d66a52020-06-05 17:26:16 -0400725 return
726 }
727
728 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400729 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400730
731 if !strings.HasSuffix(outputPath, "/main_test") {
732 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
733 return
734 }
735 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
736 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
737 return
738 }
739}
740
Chris Parsons216e10a2020-07-09 17:12:52 -0400741func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400742 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400743 bp := `
744 cc_test_library {
745 name: "test_lib",
746 srcs: ["test_lib.cpp"],
747 relative_install_path: "foo/bar/baz",
748 gtest: false,
749 }
750
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400751 cc_binary {
752 name: "test_bin",
753 relative_install_path: "foo/bar/baz",
754 compile_multilib: "both",
755 }
756
Chris Parsons216e10a2020-07-09 17:12:52 -0400757 cc_test {
758 name: "main_test",
759 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400760 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400761 gtest: false,
762 }
763 `
764
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000765 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400766 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900767 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400768 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
769
770 ctx := testCcWithConfig(t, config)
771 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
772 testBinary := module.(*Module).linker.(*testBinary)
773 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
774 if err != nil {
775 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
776 }
777 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400778 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400779 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400780 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800781 t.Fatalf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400782 }
783
784 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400785
786 if !strings.HasSuffix(outputPath, "/main_test") {
787 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
788 }
Colin Crossaa255532020-07-03 13:18:24 -0700789 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400790 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
791 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400792 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400793 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400794 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
795 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
796 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
797 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400798}
799
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000800func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400801 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000802 bp := `
803 cc_test {
804 name: "main_test",
805 srcs: ["main_test.cpp"],
806 test_suites: [
807 "suite_1",
808 "suite_2",
809 ],
810 gtest: false,
811 }
812 `
813
814 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
815 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
816
817 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
818 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
819 if len(compatEntries) != 2 {
820 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
821 }
822 if compatEntries[0] != "suite_1" {
823 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
824 " but was '%s'", compatEntries[0])
825 }
826 if compatEntries[1] != "suite_2" {
827 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
828 " but was '%s'", compatEntries[1])
829 }
830}
831
832func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400833 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000834 bp := `
835 cc_test_library {
836 name: "main_test_lib",
837 srcs: ["main_test_lib.cpp"],
838 test_suites: [
839 "suite_1",
840 "suite_2",
841 ],
842 gtest: false,
843 }
844 `
845
846 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
847 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
848
849 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
850 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
851 if len(compatEntries) != 2 {
852 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
853 }
854 if compatEntries[0] != "suite_1" {
855 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
856 " but was '%s'", compatEntries[0])
857 }
858 if compatEntries[1] != "suite_2" {
859 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
860 " but was '%s'", compatEntries[1])
861 }
862}
863
Justin Yun63e9ec72020-10-29 16:49:43 +0900864func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400865 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900866 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900867 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900868 cc_library {
869 name: "libvndk",
870 vndk: {
871 enabled: true,
872 },
873 nocrt: true,
874 }
875 `)
876
Justin Yunc0d8c492021-01-07 17:45:31 +0900877 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900878 cc_library {
879 name: "libvndk",
880 product_available: true,
881 vndk: {
882 enabled: true,
883 },
884 nocrt: true,
885 }
886 `)
887
Justin Yun6977e8a2020-10-29 18:24:11 +0900888 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
889 cc_library {
890 name: "libvndkprop",
891 vendor_available: true,
892 product_available: true,
893 vndk: {
894 enabled: true,
895 },
896 nocrt: true,
897 target: {
898 vendor: {
899 cflags: ["-DTEST",],
900 },
901 },
902 }
903 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900904}
905
Logan Chiend3c59a22018-03-29 14:08:15 +0800906func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400907 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800908 // Check whether an error is emitted when a VNDK lib depends on a system lib.
909 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
910 cc_library {
911 name: "libvndk",
912 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900913 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800914 vndk: {
915 enabled: true,
916 },
917 shared_libs: ["libfwk"], // Cause error
918 nocrt: true,
919 }
920
921 cc_library {
922 name: "libfwk",
923 nocrt: true,
924 }
925 `)
926
927 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
928 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
929 cc_library {
930 name: "libvndk",
931 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900932 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800933 vndk: {
934 enabled: true,
935 },
936 shared_libs: ["libvendor"], // Cause error
937 nocrt: true,
938 }
939
940 cc_library {
941 name: "libvendor",
942 vendor: true,
943 nocrt: true,
944 }
945 `)
946
947 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
948 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
949 cc_library {
950 name: "libvndk_sp",
951 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900952 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800953 vndk: {
954 enabled: true,
955 support_system_process: true,
956 },
957 shared_libs: ["libfwk"], // Cause error
958 nocrt: true,
959 }
960
961 cc_library {
962 name: "libfwk",
963 nocrt: true,
964 }
965 `)
966
967 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
968 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
969 cc_library {
970 name: "libvndk_sp",
971 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900972 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800973 vndk: {
974 enabled: true,
975 support_system_process: true,
976 },
977 shared_libs: ["libvendor"], // Cause error
978 nocrt: true,
979 }
980
981 cc_library {
982 name: "libvendor",
983 vendor: true,
984 nocrt: true,
985 }
986 `)
987
988 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
989 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
990 cc_library {
991 name: "libvndk_sp",
992 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900993 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800994 vndk: {
995 enabled: true,
996 support_system_process: true,
997 },
998 shared_libs: ["libvndk"], // Cause error
999 nocrt: true,
1000 }
1001
1002 cc_library {
1003 name: "libvndk",
1004 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001005 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001006 vndk: {
1007 enabled: true,
1008 },
1009 nocrt: true,
1010 }
1011 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001012
1013 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1014 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1015 cc_library {
1016 name: "libvndk",
1017 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001018 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001019 vndk: {
1020 enabled: true,
1021 },
1022 shared_libs: ["libnonvndk"],
1023 nocrt: true,
1024 }
1025
1026 cc_library {
1027 name: "libnonvndk",
1028 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001029 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001030 nocrt: true,
1031 }
1032 `)
1033
1034 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1035 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1036 cc_library {
1037 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001038 vendor_available: true,
1039 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001040 vndk: {
1041 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001042 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001043 },
1044 shared_libs: ["libnonvndk"],
1045 nocrt: true,
1046 }
1047
1048 cc_library {
1049 name: "libnonvndk",
1050 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001051 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001052 nocrt: true,
1053 }
1054 `)
1055
1056 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1057 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1058 cc_library {
1059 name: "libvndksp",
1060 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001061 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001062 vndk: {
1063 enabled: true,
1064 support_system_process: true,
1065 },
1066 shared_libs: ["libnonvndk"],
1067 nocrt: true,
1068 }
1069
1070 cc_library {
1071 name: "libnonvndk",
1072 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001073 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001074 nocrt: true,
1075 }
1076 `)
1077
1078 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1079 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1080 cc_library {
1081 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001082 vendor_available: true,
1083 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001084 vndk: {
1085 enabled: true,
1086 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001087 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001088 },
1089 shared_libs: ["libnonvndk"],
1090 nocrt: true,
1091 }
1092
1093 cc_library {
1094 name: "libnonvndk",
1095 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001096 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001097 nocrt: true,
1098 }
1099 `)
1100}
1101
1102func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001103 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001104 // okay to link : LLNDK -> double_loadable VNDK
1105 testCc(t, `
1106 cc_library {
1107 name: "libllndk",
1108 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001109 llndk: {
1110 symbol_file: "libllndk.map.txt",
1111 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001112 }
1113
1114 cc_library {
1115 name: "libdoubleloadable",
1116 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001117 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001118 vndk: {
1119 enabled: true,
1120 },
1121 double_loadable: true,
1122 }
1123 `)
1124 // okay to link : LLNDK -> VNDK-SP
1125 testCc(t, `
1126 cc_library {
1127 name: "libllndk",
1128 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001129 llndk: {
1130 symbol_file: "libllndk.map.txt",
1131 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001132 }
1133
1134 cc_library {
1135 name: "libvndksp",
1136 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001137 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001138 vndk: {
1139 enabled: true,
1140 support_system_process: true,
1141 },
1142 }
1143 `)
1144 // okay to link : double_loadable -> double_loadable
1145 testCc(t, `
1146 cc_library {
1147 name: "libdoubleloadable1",
1148 shared_libs: ["libdoubleloadable2"],
1149 vendor_available: true,
1150 double_loadable: true,
1151 }
1152
1153 cc_library {
1154 name: "libdoubleloadable2",
1155 vendor_available: true,
1156 double_loadable: true,
1157 }
1158 `)
1159 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1160 testCc(t, `
1161 cc_library {
1162 name: "libdoubleloadable",
1163 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001164 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001165 vndk: {
1166 enabled: true,
1167 },
1168 double_loadable: true,
1169 shared_libs: ["libnondoubleloadable"],
1170 }
1171
1172 cc_library {
1173 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001174 vendor_available: true,
1175 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001176 vndk: {
1177 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001178 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001179 },
1180 double_loadable: true,
1181 }
1182 `)
1183 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1184 testCc(t, `
1185 cc_library {
1186 name: "libllndk",
1187 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001188 llndk: {
1189 symbol_file: "libllndk.map.txt",
1190 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 }
1192
1193 cc_library {
1194 name: "libcoreonly",
1195 shared_libs: ["libvendoravailable"],
1196 }
1197
1198 // indirect dependency of LLNDK
1199 cc_library {
1200 name: "libvendoravailable",
1201 vendor_available: true,
1202 double_loadable: true,
1203 }
1204 `)
1205}
1206
1207func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001208 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1210 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1211 cc_library {
1212 name: "libllndk",
1213 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001214 llndk: {
1215 symbol_file: "libllndk.map.txt",
1216 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001217 }
1218
1219 cc_library {
1220 name: "libnondoubleloadable",
1221 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001222 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001223 vndk: {
1224 enabled: true,
1225 },
1226 }
1227 `)
1228
1229 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1230 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1231 cc_library {
1232 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001233 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001234 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001235 llndk: {
1236 symbol_file: "libllndk.map.txt",
1237 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001238 }
1239
1240 cc_library {
1241 name: "libnondoubleloadable",
1242 vendor_available: true,
1243 }
1244 `)
1245
Jooyung Hana70f0672019-01-18 15:20:43 +09001246 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1247 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1248 cc_library {
1249 name: "libllndk",
1250 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001251 llndk: {
1252 symbol_file: "libllndk.map.txt",
1253 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001254 }
1255
1256 cc_library {
1257 name: "libcoreonly",
1258 shared_libs: ["libvendoravailable"],
1259 }
1260
1261 // indirect dependency of LLNDK
1262 cc_library {
1263 name: "libvendoravailable",
1264 vendor_available: true,
1265 }
1266 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001267
1268 // The error is not from 'client' but from 'libllndk'
1269 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1270 cc_library {
1271 name: "client",
1272 vendor_available: true,
1273 double_loadable: true,
1274 shared_libs: ["libllndk"],
1275 }
1276 cc_library {
1277 name: "libllndk",
1278 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001279 llndk: {
1280 symbol_file: "libllndk.map.txt",
1281 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001282 }
1283 cc_library {
1284 name: "libnondoubleloadable",
1285 vendor_available: true,
1286 }
1287 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001288}
1289
Jooyung Han479ca172020-10-19 18:51:07 +09001290func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001291 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001292 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1293 cc_library {
1294 name: "libvndksp",
1295 shared_libs: ["libanothervndksp"],
1296 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001297 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001298 vndk: {
1299 enabled: true,
1300 support_system_process: true,
1301 }
1302 }
1303
1304 cc_library {
1305 name: "libllndk",
1306 shared_libs: ["libanothervndksp"],
1307 }
1308
Jooyung Han479ca172020-10-19 18:51:07 +09001309 cc_library {
1310 name: "libanothervndksp",
1311 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001312 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001313 }
1314 `)
1315}
1316
Logan Chienf3511742017-10-31 18:04:35 +08001317func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001318 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001319 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001320 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001321 cc_library {
1322 name: "libvndk",
1323 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001324 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001325 vndk: {
1326 enabled: true,
1327 },
1328 nocrt: true,
1329 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001330 cc_library {
1331 name: "libvndk2",
1332 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001333 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001334 vndk: {
1335 enabled: true,
1336 },
1337 target: {
1338 vendor: {
1339 suffix: "-suffix",
1340 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001341 product: {
1342 suffix: "-suffix",
1343 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001344 },
1345 nocrt: true,
1346 }
Logan Chienf3511742017-10-31 18:04:35 +08001347
1348 cc_library {
1349 name: "libvndk_ext",
1350 vendor: true,
1351 vndk: {
1352 enabled: true,
1353 extends: "libvndk",
1354 },
1355 nocrt: true,
1356 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001357
1358 cc_library {
1359 name: "libvndk2_ext",
1360 vendor: true,
1361 vndk: {
1362 enabled: true,
1363 extends: "libvndk2",
1364 },
1365 nocrt: true,
1366 }
Logan Chienf3511742017-10-31 18:04:35 +08001367
Justin Yun0ecf0b22020-02-28 15:07:59 +09001368 cc_library {
1369 name: "libvndk_ext_product",
1370 product_specific: true,
1371 vndk: {
1372 enabled: true,
1373 extends: "libvndk",
1374 },
1375 nocrt: true,
1376 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001377
Justin Yun0ecf0b22020-02-28 15:07:59 +09001378 cc_library {
1379 name: "libvndk2_ext_product",
1380 product_specific: true,
1381 vndk: {
1382 enabled: true,
1383 extends: "libvndk2",
1384 },
1385 nocrt: true,
1386 }
1387 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001388 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001389 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001390 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001391
1392 ctx := testCcWithConfig(t, config)
1393
1394 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1395 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1396
1397 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1398 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1399
1400 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1401 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001402}
1403
Logan Chienf3511742017-10-31 18:04:35 +08001404func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001405 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001406 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001407 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001408 cc_library {
1409 name: "libvndk",
1410 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001411 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001412 vndk: {
1413 enabled: true,
1414 },
1415 nocrt: true,
1416 }
1417
1418 cc_library {
1419 name: "libvndk_ext",
1420 vndk: {
1421 enabled: true,
1422 extends: "libvndk",
1423 },
1424 nocrt: true,
1425 }
1426 `)
1427
1428 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1429 cc_library {
1430 name: "libvndk",
1431 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001432 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001433 vndk: {
1434 enabled: true,
1435 },
1436 nocrt: true,
1437 }
1438
1439 cc_library {
1440 name: "libvndk_ext",
1441 vendor: true,
1442 vndk: {
1443 enabled: true,
1444 },
1445 nocrt: true,
1446 }
1447 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001448
1449 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1450 cc_library {
1451 name: "libvndk",
1452 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001453 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001454 vndk: {
1455 enabled: true,
1456 },
1457 nocrt: true,
1458 }
1459
1460 cc_library {
1461 name: "libvndk_ext_product",
1462 product_specific: true,
1463 vndk: {
1464 enabled: true,
1465 },
1466 nocrt: true,
1467 }
1468 `)
1469
1470 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1471 cc_library {
1472 name: "libvndk",
1473 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001474 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001475 vndk: {
1476 enabled: true,
1477 },
1478 nocrt: true,
1479 }
1480
1481 cc_library {
1482 name: "libvndk_ext_product",
1483 product_specific: true,
1484 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001485 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001486 vndk: {
1487 enabled: true,
1488 extends: "libvndk",
1489 },
1490 nocrt: true,
1491 }
1492 `)
Logan Chienf3511742017-10-31 18:04:35 +08001493}
1494
1495func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001496 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001497 // This test ensures an error is emitted for inconsistent support_system_process.
1498 testCcError(t, "module \".*\" with mismatched support_system_process", `
1499 cc_library {
1500 name: "libvndk",
1501 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001502 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001503 vndk: {
1504 enabled: true,
1505 },
1506 nocrt: true,
1507 }
1508
1509 cc_library {
1510 name: "libvndk_sp_ext",
1511 vendor: true,
1512 vndk: {
1513 enabled: true,
1514 extends: "libvndk",
1515 support_system_process: true,
1516 },
1517 nocrt: true,
1518 }
1519 `)
1520
1521 testCcError(t, "module \".*\" with mismatched support_system_process", `
1522 cc_library {
1523 name: "libvndk_sp",
1524 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001525 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001526 vndk: {
1527 enabled: true,
1528 support_system_process: true,
1529 },
1530 nocrt: true,
1531 }
1532
1533 cc_library {
1534 name: "libvndk_ext",
1535 vendor: true,
1536 vndk: {
1537 enabled: true,
1538 extends: "libvndk_sp",
1539 },
1540 nocrt: true,
1541 }
1542 `)
1543}
1544
1545func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001546 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001547 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001548 // with `private: true`.
1549 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001550 cc_library {
1551 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001552 vendor_available: true,
1553 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001554 vndk: {
1555 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001556 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001557 },
1558 nocrt: true,
1559 }
1560
1561 cc_library {
1562 name: "libvndk_ext",
1563 vendor: true,
1564 vndk: {
1565 enabled: true,
1566 extends: "libvndk",
1567 },
1568 nocrt: true,
1569 }
1570 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001571
Justin Yunfd9e8042020-12-23 18:23:14 +09001572 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001573 cc_library {
1574 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001575 vendor_available: true,
1576 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001577 vndk: {
1578 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001579 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001580 },
1581 nocrt: true,
1582 }
1583
1584 cc_library {
1585 name: "libvndk_ext_product",
1586 product_specific: true,
1587 vndk: {
1588 enabled: true,
1589 extends: "libvndk",
1590 },
1591 nocrt: true,
1592 }
1593 `)
Logan Chienf3511742017-10-31 18:04:35 +08001594}
1595
Logan Chiend3c59a22018-03-29 14:08:15 +08001596func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001597 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001598 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001599 testCc(t, `
1600 cc_library {
1601 name: "libvndk",
1602 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001603 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001604 vndk: {
1605 enabled: true,
1606 },
1607 nocrt: true,
1608 }
1609
1610 cc_library {
1611 name: "libvndk_ext",
1612 vendor: true,
1613 vndk: {
1614 enabled: true,
1615 extends: "libvndk",
1616 },
1617 nocrt: true,
1618 }
1619
1620 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001621 name: "libvndk_sp",
1622 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001623 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001624 vndk: {
1625 enabled: true,
1626 support_system_process: true,
1627 },
1628 nocrt: true,
1629 }
1630
1631 cc_library {
1632 name: "libvndk_sp_ext",
1633 vendor: true,
1634 vndk: {
1635 enabled: true,
1636 extends: "libvndk_sp",
1637 support_system_process: true,
1638 },
1639 nocrt: true,
1640 }
1641
1642 cc_library {
1643 name: "libvendor",
1644 vendor: true,
1645 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1646 nocrt: true,
1647 }
1648 `)
1649}
1650
Logan Chiend3c59a22018-03-29 14:08:15 +08001651func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001652 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001653 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001654 testCc(t, `
1655 cc_library {
1656 name: "libvndk",
1657 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001658 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001659 vndk: {
1660 enabled: true,
1661 },
1662 nocrt: true,
1663 }
1664
1665 cc_library {
1666 name: "libvndk_ext",
1667 vendor: true,
1668 vndk: {
1669 enabled: true,
1670 extends: "libvndk",
1671 },
1672 shared_libs: ["libvendor"],
1673 nocrt: true,
1674 }
1675
1676 cc_library {
1677 name: "libvendor",
1678 vendor: true,
1679 nocrt: true,
1680 }
1681 `)
Logan Chienf3511742017-10-31 18:04:35 +08001682
Logan Chiend3c59a22018-03-29 14:08:15 +08001683 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1684 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001685 cc_library {
1686 name: "libvndk_sp",
1687 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001688 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001689 vndk: {
1690 enabled: true,
1691 support_system_process: true,
1692 },
1693 nocrt: true,
1694 }
1695
1696 cc_library {
1697 name: "libvndk_sp_ext",
1698 vendor: true,
1699 vndk: {
1700 enabled: true,
1701 extends: "libvndk_sp",
1702 support_system_process: true,
1703 },
1704 shared_libs: ["libvendor"], // Cause an error
1705 nocrt: true,
1706 }
1707
1708 cc_library {
1709 name: "libvendor",
1710 vendor: true,
1711 nocrt: true,
1712 }
1713 `)
1714}
1715
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001717 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001718 bp := `
1719 cc_library {
1720 name: "libvndk",
1721 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001722 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001723 vndk: {
1724 enabled: true,
1725 },
1726 nocrt: true,
1727 }
1728
1729 cc_library {
1730 name: "libvndk_ext_product",
1731 product_specific: true,
1732 vndk: {
1733 enabled: true,
1734 extends: "libvndk",
1735 },
1736 shared_libs: ["libproduct_for_vndklibs"],
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_sp",
1742 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001743 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001744 vndk: {
1745 enabled: true,
1746 support_system_process: true,
1747 },
1748 nocrt: true,
1749 }
1750
1751 cc_library {
1752 name: "libvndk_sp_ext_product",
1753 product_specific: true,
1754 vndk: {
1755 enabled: true,
1756 extends: "libvndk_sp",
1757 support_system_process: true,
1758 },
1759 shared_libs: ["libproduct_for_vndklibs"],
1760 nocrt: true,
1761 }
1762
1763 cc_library {
1764 name: "libproduct",
1765 product_specific: true,
1766 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1767 nocrt: true,
1768 }
1769
1770 cc_library {
1771 name: "libproduct_for_vndklibs",
1772 product_specific: true,
1773 nocrt: true,
1774 }
1775 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001776 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001777 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001778 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001779
1780 testCcWithConfig(t, config)
1781}
1782
Logan Chiend3c59a22018-03-29 14:08:15 +08001783func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001784 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001785 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1786 // library.
1787 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1788 cc_library {
1789 name: "libvndk",
1790 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001791 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001792 vndk: {
1793 enabled: true,
1794 },
1795 nocrt: true,
1796 }
1797
1798 cc_library {
1799 name: "libvndk_sp",
1800 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001801 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001802 vndk: {
1803 enabled: true,
1804 support_system_process: true,
1805 },
1806 nocrt: true,
1807 }
1808
1809 cc_library {
1810 name: "libvndk_sp_ext",
1811 vendor: true,
1812 vndk: {
1813 enabled: true,
1814 extends: "libvndk_sp",
1815 support_system_process: true,
1816 },
1817 shared_libs: ["libvndk"], // Cause an error
1818 nocrt: true,
1819 }
1820 `)
1821
1822 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1823 // library.
1824 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1825 cc_library {
1826 name: "libvndk",
1827 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001828 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001829 vndk: {
1830 enabled: true,
1831 },
1832 nocrt: true,
1833 }
1834
1835 cc_library {
1836 name: "libvndk_ext",
1837 vendor: true,
1838 vndk: {
1839 enabled: true,
1840 extends: "libvndk",
1841 },
1842 nocrt: true,
1843 }
1844
1845 cc_library {
1846 name: "libvndk_sp",
1847 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001848 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001849 vndk: {
1850 enabled: true,
1851 support_system_process: true,
1852 },
1853 nocrt: true,
1854 }
1855
1856 cc_library {
1857 name: "libvndk_sp_ext",
1858 vendor: true,
1859 vndk: {
1860 enabled: true,
1861 extends: "libvndk_sp",
1862 support_system_process: true,
1863 },
1864 shared_libs: ["libvndk_ext"], // Cause an error
1865 nocrt: true,
1866 }
1867 `)
1868}
1869
1870func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001871 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001872 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1873 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001874 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1875 cc_library {
1876 name: "libvndk",
1877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001878 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001879 vndk: {
1880 enabled: true,
1881 },
1882 nocrt: true,
1883 }
1884
1885 cc_library {
1886 name: "libvndk_ext",
1887 vendor: true,
1888 vndk: {
1889 enabled: true,
1890 extends: "libvndk",
1891 },
1892 nocrt: true,
1893 }
1894
1895 cc_library {
1896 name: "libvndk2",
1897 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001898 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001899 vndk: {
1900 enabled: true,
1901 },
1902 shared_libs: ["libvndk_ext"],
1903 nocrt: true,
1904 }
1905 `)
1906
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001907 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001908 cc_library {
1909 name: "libvndk",
1910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001911 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001912 vndk: {
1913 enabled: true,
1914 },
1915 nocrt: true,
1916 }
1917
1918 cc_library {
1919 name: "libvndk_ext",
1920 vendor: true,
1921 vndk: {
1922 enabled: true,
1923 extends: "libvndk",
1924 },
1925 nocrt: true,
1926 }
1927
1928 cc_library {
1929 name: "libvndk2",
1930 vendor_available: true,
1931 vndk: {
1932 enabled: true,
1933 },
1934 target: {
1935 vendor: {
1936 shared_libs: ["libvndk_ext"],
1937 },
1938 },
1939 nocrt: true,
1940 }
1941 `)
1942
1943 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1944 cc_library {
1945 name: "libvndk_sp",
1946 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001947 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001948 vndk: {
1949 enabled: true,
1950 support_system_process: true,
1951 },
1952 nocrt: true,
1953 }
1954
1955 cc_library {
1956 name: "libvndk_sp_ext",
1957 vendor: true,
1958 vndk: {
1959 enabled: true,
1960 extends: "libvndk_sp",
1961 support_system_process: true,
1962 },
1963 nocrt: true,
1964 }
1965
1966 cc_library {
1967 name: "libvndk_sp_2",
1968 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001969 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001970 vndk: {
1971 enabled: true,
1972 support_system_process: true,
1973 },
1974 shared_libs: ["libvndk_sp_ext"],
1975 nocrt: true,
1976 }
1977 `)
1978
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001979 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001980 cc_library {
1981 name: "libvndk_sp",
1982 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001983 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001984 vndk: {
1985 enabled: true,
1986 },
1987 nocrt: true,
1988 }
1989
1990 cc_library {
1991 name: "libvndk_sp_ext",
1992 vendor: true,
1993 vndk: {
1994 enabled: true,
1995 extends: "libvndk_sp",
1996 },
1997 nocrt: true,
1998 }
1999
2000 cc_library {
2001 name: "libvndk_sp2",
2002 vendor_available: true,
2003 vndk: {
2004 enabled: true,
2005 },
2006 target: {
2007 vendor: {
2008 shared_libs: ["libvndk_sp_ext"],
2009 },
2010 },
2011 nocrt: true,
2012 }
2013 `)
2014}
2015
Justin Yun5f7f7e82019-11-18 19:52:14 +09002016func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002017 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002018 bp := `
2019 cc_library {
2020 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002021 llndk: {
2022 symbol_file: "libllndk.map.txt",
2023 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002024 }
2025 cc_library {
2026 name: "libvndk",
2027 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002028 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002029 vndk: {
2030 enabled: true,
2031 },
2032 nocrt: true,
2033 }
2034 cc_library {
2035 name: "libvndk_sp",
2036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002037 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002038 vndk: {
2039 enabled: true,
2040 support_system_process: true,
2041 },
2042 nocrt: true,
2043 }
2044 cc_library {
2045 name: "libva",
2046 vendor_available: true,
2047 nocrt: true,
2048 }
2049 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002050 name: "libpa",
2051 product_available: true,
2052 nocrt: true,
2053 }
2054 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002055 name: "libboth_available",
2056 vendor_available: true,
2057 product_available: true,
2058 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002059 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002060 target: {
2061 vendor: {
2062 suffix: "-vendor",
2063 },
2064 product: {
2065 suffix: "-product",
2066 },
2067 }
2068 }
2069 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002070 name: "libproduct_va",
2071 product_specific: true,
2072 vendor_available: true,
2073 nocrt: true,
2074 }
2075 cc_library {
2076 name: "libprod",
2077 product_specific: true,
2078 shared_libs: [
2079 "libllndk",
2080 "libvndk",
2081 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002083 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002084 "libproduct_va",
2085 ],
2086 nocrt: true,
2087 }
2088 cc_library {
2089 name: "libvendor",
2090 vendor: true,
2091 shared_libs: [
2092 "libllndk",
2093 "libvndk",
2094 "libvndk_sp",
2095 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002096 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002097 "libproduct_va",
2098 ],
2099 nocrt: true,
2100 }
2101 `
2102
Paul Duffin8567f222021-03-23 00:02:06 +00002103 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002104
Jooyung Han261e1582020-10-20 18:54:21 +09002105 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2106 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002107
2108 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2109 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2110
2111 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2112 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002113
2114 ensureStringContains := func(t *testing.T, str string, substr string) {
2115 t.Helper()
2116 if !strings.Contains(str, substr) {
2117 t.Errorf("%q is not found in %v", substr, str)
2118 }
2119 }
2120 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2121 t.Helper()
2122 if strings.Contains(str, substr) {
2123 t.Errorf("%q is found in %v", substr, str)
2124 }
2125 }
2126
2127 // _static variant is used since _shared reuses *.o from the static variant
2128 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2129 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2130
2131 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2132 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2133 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2134 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002135 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun13decfb2021-03-08 19:25:55 +09002136
2137 product_cflags := product_static.Rule("cc").Args["cFlags"]
2138 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2139 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2140 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002141 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002142}
2143
2144func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002145 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002146 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002147 cc_library {
2148 name: "libprod",
2149 product_specific: true,
2150 shared_libs: [
2151 "libvendor",
2152 ],
2153 nocrt: true,
2154 }
2155 cc_library {
2156 name: "libvendor",
2157 vendor: true,
2158 nocrt: true,
2159 }
2160 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002161 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002162 cc_library {
2163 name: "libprod",
2164 product_specific: true,
2165 shared_libs: [
2166 "libsystem",
2167 ],
2168 nocrt: true,
2169 }
2170 cc_library {
2171 name: "libsystem",
2172 nocrt: true,
2173 }
2174 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002175 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002176 cc_library {
2177 name: "libprod",
2178 product_specific: true,
2179 shared_libs: [
2180 "libva",
2181 ],
2182 nocrt: true,
2183 }
2184 cc_library {
2185 name: "libva",
2186 vendor_available: true,
2187 nocrt: true,
2188 }
2189 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002190 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002191 cc_library {
2192 name: "libprod",
2193 product_specific: true,
2194 shared_libs: [
2195 "libvndk_private",
2196 ],
2197 nocrt: true,
2198 }
2199 cc_library {
2200 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002201 vendor_available: true,
2202 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002203 vndk: {
2204 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002205 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 },
2207 nocrt: true,
2208 }
2209 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002210 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002211 cc_library {
2212 name: "libprod",
2213 product_specific: true,
2214 shared_libs: [
2215 "libsystem_ext",
2216 ],
2217 nocrt: true,
2218 }
2219 cc_library {
2220 name: "libsystem_ext",
2221 system_ext_specific: true,
2222 nocrt: true,
2223 }
2224 `)
2225 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2226 cc_library {
2227 name: "libsystem",
2228 shared_libs: [
2229 "libproduct_va",
2230 ],
2231 nocrt: true,
2232 }
2233 cc_library {
2234 name: "libproduct_va",
2235 product_specific: true,
2236 vendor_available: true,
2237 nocrt: true,
2238 }
2239 `)
2240}
2241
Jooyung Han38002912019-05-16 04:01:54 +09002242func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002243 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002244 bp := `
2245 cc_library {
2246 name: "libvndk",
2247 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002248 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002249 vndk: {
2250 enabled: true,
2251 },
2252 }
2253 cc_library {
2254 name: "libvndksp",
2255 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002256 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002257 vndk: {
2258 enabled: true,
2259 support_system_process: true,
2260 },
2261 }
2262 cc_library {
2263 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002264 vendor_available: true,
2265 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002266 vndk: {
2267 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002268 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002269 },
2270 }
2271 cc_library {
2272 name: "libvendor",
2273 vendor: true,
2274 }
2275 cc_library {
2276 name: "libvndkext",
2277 vendor: true,
2278 vndk: {
2279 enabled: true,
2280 extends: "libvndk",
2281 },
2282 }
2283 vndk_prebuilt_shared {
2284 name: "prevndk",
2285 version: "27",
2286 target_arch: "arm",
2287 binder32bit: true,
2288 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002289 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002290 vndk: {
2291 enabled: true,
2292 },
2293 arch: {
2294 arm: {
2295 srcs: ["liba.so"],
2296 },
2297 },
2298 }
2299 cc_library {
2300 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002301 llndk: {
2302 symbol_file: "libllndk.map.txt",
2303 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002304 }
2305 cc_library {
2306 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002307 llndk: {
2308 symbol_file: "libllndkprivate.map.txt",
2309 private: true,
2310 }
Colin Cross78212242021-01-06 14:51:30 -08002311 }
2312
2313 llndk_libraries_txt {
2314 name: "llndk.libraries.txt",
2315 }
2316 vndkcore_libraries_txt {
2317 name: "vndkcore.libraries.txt",
2318 }
2319 vndksp_libraries_txt {
2320 name: "vndksp.libraries.txt",
2321 }
2322 vndkprivate_libraries_txt {
2323 name: "vndkprivate.libraries.txt",
2324 }
2325 vndkcorevariant_libraries_txt {
2326 name: "vndkcorevariant.libraries.txt",
2327 insert_vndk_version: false,
2328 }
2329 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002330
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002331 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002332 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002333 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002334 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002335 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002336
Colin Cross78212242021-01-06 14:51:30 -08002337 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2338 []string{"libvndk.so", "libvndkprivate.so"})
2339 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2340 []string{"libc++.so", "libvndksp.so"})
2341 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2342 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2343 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2344 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002345
Colin Crossfb0c16e2019-11-20 17:12:35 -08002346 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002347
Jooyung Han38002912019-05-16 04:01:54 +09002348 tests := []struct {
2349 variant string
2350 name string
2351 expected string
2352 }{
2353 {vendorVariant, "libvndk", "native:vndk"},
2354 {vendorVariant, "libvndksp", "native:vndk"},
2355 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2356 {vendorVariant, "libvendor", "native:vendor"},
2357 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002358 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002359 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002360 {coreVariant, "libvndk", "native:platform"},
2361 {coreVariant, "libvndkprivate", "native:platform"},
2362 {coreVariant, "libllndk", "native:platform"},
2363 }
2364 for _, test := range tests {
2365 t.Run(test.name, func(t *testing.T) {
2366 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2367 assertString(t, module.makeLinkType, test.expected)
2368 })
2369 }
2370}
2371
Jeff Gaston294356f2017-09-27 17:05:30 -07002372var staticLinkDepOrderTestCases = []struct {
2373 // This is a string representation of a map[moduleName][]moduleDependency .
2374 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002375 inStatic string
2376
2377 // This is a string representation of a map[moduleName][]moduleDependency .
2378 // It models the dependencies declared in an Android.bp file.
2379 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002380
2381 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2382 // The keys of allOrdered specify which modules we would like to check.
2383 // The values of allOrdered specify the expected result (of the transitive closure of all
2384 // dependencies) for each module to test
2385 allOrdered string
2386
2387 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2388 // The keys of outOrdered specify which modules we would like to check.
2389 // The values of outOrdered specify the expected result (of the ordered linker command line)
2390 // for each module to test.
2391 outOrdered string
2392}{
2393 // Simple tests
2394 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002395 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002396 outOrdered: "",
2397 },
2398 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002399 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002400 outOrdered: "a:",
2401 },
2402 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002403 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002404 outOrdered: "a:b; b:",
2405 },
2406 // Tests of reordering
2407 {
2408 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002409 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002410 outOrdered: "a:b,c,d; b:d; c:d; d:",
2411 },
2412 {
2413 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002414 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002415 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2416 },
2417 {
2418 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002419 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002420 outOrdered: "a:d,b,e,c; d:b; e:c",
2421 },
2422 {
2423 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002424 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002425 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2426 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2427 },
2428 {
2429 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002430 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 -07002431 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2432 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2433 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002434 // shared dependencies
2435 {
2436 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2437 // So, we don't actually have to check that a shared dependency of c will change the order
2438 // of a library that depends statically on b and on c. We only need to check that if c has
2439 // a shared dependency on b, that that shows up in allOrdered.
2440 inShared: "c:b",
2441 allOrdered: "c:b",
2442 outOrdered: "c:",
2443 },
2444 {
2445 // This test doesn't actually include any shared dependencies but it's a reminder of what
2446 // the second phase of the above test would look like
2447 inStatic: "a:b,c; c:b",
2448 allOrdered: "a:c,b; c:b",
2449 outOrdered: "a:c,b; c:b",
2450 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002451 // tiebreakers for when two modules specifying different orderings and there is no dependency
2452 // to dictate an order
2453 {
2454 // 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 -08002455 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002456 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2457 },
2458 {
2459 // 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 -08002460 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 -07002461 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2462 },
2463 // Tests involving duplicate dependencies
2464 {
2465 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002466 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002467 outOrdered: "a:c,b",
2468 },
2469 {
2470 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002471 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002472 outOrdered: "a:d,c,b",
2473 },
2474 // Tests to confirm the nonexistence of infinite loops.
2475 // These cases should never happen, so as long as the test terminates and the
2476 // result is deterministic then that should be fine.
2477 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002478 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002479 outOrdered: "a:a",
2480 },
2481 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002482 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002483 allOrdered: "a:b,c; b:c,a; c:a,b",
2484 outOrdered: "a:b; b:c; c:a",
2485 },
2486 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002487 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002488 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2489 outOrdered: "a:c,b; b:a,c; c:b,a",
2490 },
2491}
2492
2493// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2494func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2495 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2496 strippedText := strings.Replace(text, " ", "", -1)
2497 if len(strippedText) < 1 {
2498 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2499 }
2500 allDeps = make(map[android.Path][]android.Path, 0)
2501
2502 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2503 moduleTexts := strings.Split(strippedText, ";")
2504
2505 outputForModuleName := func(moduleName string) android.Path {
2506 return android.PathForTesting(moduleName)
2507 }
2508
2509 for _, moduleText := range moduleTexts {
2510 // convert from "a:b,c" to ["a", "b,c"]
2511 components := strings.Split(moduleText, ":")
2512 if len(components) != 2 {
2513 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2514 }
2515 moduleName := components[0]
2516 moduleOutput := outputForModuleName(moduleName)
2517 modulesInOrder = append(modulesInOrder, moduleOutput)
2518
2519 depString := components[1]
2520 // convert from "b,c" to ["b", "c"]
2521 depNames := strings.Split(depString, ",")
2522 if len(depString) < 1 {
2523 depNames = []string{}
2524 }
2525 var deps []android.Path
2526 for _, depName := range depNames {
2527 deps = append(deps, outputForModuleName(depName))
2528 }
2529 allDeps[moduleOutput] = deps
2530 }
2531 return modulesInOrder, allDeps
2532}
2533
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002534func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002535 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002536 ctx := testCc(t, `
2537 cc_library {
2538 name: "a",
2539 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002540 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002541 }
2542 cc_library {
2543 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002544 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002545 }
2546 cc_library {
2547 name: "c",
2548 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002549 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002550 }
2551 cc_library {
2552 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002553 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002554 }
2555
2556 `)
2557
Colin Cross7113d202019-11-20 16:39:12 -08002558 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002559 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002560 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2561 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002562 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002563
2564 if !reflect.DeepEqual(actual, expected) {
2565 t.Errorf("staticDeps orderings were not propagated correctly"+
2566 "\nactual: %v"+
2567 "\nexpected: %v",
2568 actual,
2569 expected,
2570 )
2571 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002572}
Jeff Gaston294356f2017-09-27 17:05:30 -07002573
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002574func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002575 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002576 ctx := testCc(t, `
2577 cc_library {
2578 name: "a",
2579 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002580 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002581 }
2582 cc_library {
2583 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002584 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002585 }
2586 cc_library {
2587 name: "c",
2588 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002589 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002590 }
2591
2592 `)
2593
Colin Cross7113d202019-11-20 16:39:12 -08002594 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002595 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Crossc85750b2022-04-21 12:50:51 -07002596 actual := android.Paths(ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2597 TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002598 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002599
2600 if !reflect.DeepEqual(actual, expected) {
2601 t.Errorf("staticDeps orderings did not account for shared libs"+
2602 "\nactual: %v"+
2603 "\nexpected: %v",
2604 actual,
2605 expected,
2606 )
2607 }
2608}
2609
Jooyung Hanb04a4992020-03-13 18:57:35 +09002610func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002611 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002612 if !reflect.DeepEqual(actual, expected) {
2613 t.Errorf(message+
2614 "\nactual: %v"+
2615 "\nexpected: %v",
2616 actual,
2617 expected,
2618 )
2619 }
2620}
2621
Jooyung Han61b66e92020-03-21 14:21:46 +00002622func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002623 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002624 result := prepareForCcTest.RunTestWithBp(t, `
2625 cc_library {
2626 name: "libllndk",
2627 stubs: { versions: ["1", "2"] },
2628 llndk: {
2629 symbol_file: "libllndk.map.txt",
2630 },
2631 export_include_dirs: ["include"],
2632 }
2633
2634 cc_prebuilt_library_shared {
2635 name: "libllndkprebuilt",
2636 stubs: { versions: ["1", "2"] },
2637 llndk: {
2638 symbol_file: "libllndkprebuilt.map.txt",
2639 },
2640 }
2641
2642 cc_library {
2643 name: "libllndk_with_external_headers",
2644 stubs: { versions: ["1", "2"] },
2645 llndk: {
2646 symbol_file: "libllndk.map.txt",
2647 export_llndk_headers: ["libexternal_llndk_headers"],
2648 },
2649 header_libs: ["libexternal_headers"],
2650 export_header_lib_headers: ["libexternal_headers"],
2651 }
2652 cc_library_headers {
2653 name: "libexternal_headers",
2654 export_include_dirs: ["include"],
2655 vendor_available: true,
2656 }
2657 cc_library_headers {
2658 name: "libexternal_llndk_headers",
2659 export_include_dirs: ["include_llndk"],
2660 llndk: {
2661 symbol_file: "libllndk.map.txt",
2662 },
2663 vendor_available: true,
2664 }
2665
2666 cc_library {
2667 name: "libllndk_with_override_headers",
2668 stubs: { versions: ["1", "2"] },
2669 llndk: {
2670 symbol_file: "libllndk.map.txt",
2671 override_export_include_dirs: ["include_llndk"],
2672 },
2673 export_include_dirs: ["include"],
2674 }
2675 `)
2676 actual := result.ModuleVariantsForTests("libllndk")
2677 for i := 0; i < len(actual); i++ {
2678 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2679 actual = append(actual[:i], actual[i+1:]...)
2680 i--
2681 }
2682 }
2683 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002684 "android_vendor.29_arm64_armv8-a_shared_current",
2685 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002686 "android_vendor.29_arm_armv7-a-neon_shared_current",
2687 "android_vendor.29_arm_armv7-a-neon_shared",
2688 }
2689 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2690
2691 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2692 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2693
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002694 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2695 t.Helper()
2696 m := result.ModuleForTests(module, variant).Module()
2697 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2698 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2699 expectedDirs, f.IncludeDirs)
2700 }
2701
2702 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2703 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2704 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2705 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2706 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2707 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2708}
2709
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002711 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002712 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002713 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002714 name: "libllndk_headers",
2715 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002716 llndk: {
2717 llndk_headers: true,
2718 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002719 }
2720 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002721 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002722 llndk: {
2723 symbol_file: "libllndk.map.txt",
2724 export_llndk_headers: ["libllndk_headers"],
2725 }
Colin Cross0477b422020-10-13 18:43:54 -07002726 }
2727
2728 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002729 name: "libvendor",
2730 shared_libs: ["libllndk"],
2731 vendor: true,
2732 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002733 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002734 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002735 }
2736 `)
2737
2738 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002739 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002740 cflags := cc.Args["cFlags"]
2741 if !strings.Contains(cflags, "-Imy_include") {
2742 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2743 }
2744}
2745
Logan Chien43d34c32017-12-20 01:17:32 +08002746func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2747 actual := module.Properties.AndroidMkRuntimeLibs
2748 if !reflect.DeepEqual(actual, expected) {
2749 t.Errorf("incorrect runtime_libs for shared libs"+
2750 "\nactual: %v"+
2751 "\nexpected: %v",
2752 actual,
2753 expected,
2754 )
2755 }
2756}
2757
2758const runtimeLibAndroidBp = `
2759 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002760 name: "liball_available",
2761 vendor_available: true,
2762 product_available: true,
2763 no_libcrt : true,
2764 nocrt : true,
2765 system_shared_libs : [],
2766 }
2767 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002768 name: "libvendor_available1",
2769 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002770 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002771 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002772 nocrt : true,
2773 system_shared_libs : [],
2774 }
2775 cc_library {
2776 name: "libvendor_available2",
2777 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002778 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002779 target: {
2780 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002781 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002782 }
2783 },
Yi Konge7fe9912019-06-02 00:53:50 -07002784 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002785 nocrt : true,
2786 system_shared_libs : [],
2787 }
2788 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002789 name: "libproduct_vendor",
2790 product_specific: true,
2791 vendor_available: true,
2792 no_libcrt : true,
2793 nocrt : true,
2794 system_shared_libs : [],
2795 }
2796 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002797 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002798 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002799 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002800 nocrt : true,
2801 system_shared_libs : [],
2802 }
2803 cc_library {
2804 name: "libvendor1",
2805 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002806 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002807 nocrt : true,
2808 system_shared_libs : [],
2809 }
2810 cc_library {
2811 name: "libvendor2",
2812 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002813 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002814 no_libcrt : true,
2815 nocrt : true,
2816 system_shared_libs : [],
2817 }
2818 cc_library {
2819 name: "libproduct_available1",
2820 product_available: true,
2821 runtime_libs: ["liball_available"],
2822 no_libcrt : true,
2823 nocrt : true,
2824 system_shared_libs : [],
2825 }
2826 cc_library {
2827 name: "libproduct1",
2828 product_specific: true,
2829 no_libcrt : true,
2830 nocrt : true,
2831 system_shared_libs : [],
2832 }
2833 cc_library {
2834 name: "libproduct2",
2835 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002836 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002837 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002838 nocrt : true,
2839 system_shared_libs : [],
2840 }
2841`
2842
2843func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002844 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002845 ctx := testCc(t, runtimeLibAndroidBp)
2846
2847 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002848 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002849
Justin Yun8a2600c2020-12-07 12:44:03 +09002850 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2851 checkRuntimeLibs(t, []string{"liball_available"}, module)
2852
2853 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2854 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002855
2856 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002857 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002858
2859 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2860 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002861 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002862
Justin Yun8a2600c2020-12-07 12:44:03 +09002863 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2864 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002865
2866 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002867 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002868
2869 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2870 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002871 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002872
2873 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2874 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2875
2876 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002877 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002878}
2879
2880func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002881 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002882 ctx := testCc(t, runtimeLibAndroidBp)
2883
Colin Cross7113d202019-11-20 16:39:12 -08002884 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002885 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2886 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002887
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002888 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002889 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002890 checkRuntimeLibs(t, nil, module)
2891}
2892
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002893func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002894 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002895 actual := module.Properties.AndroidMkStaticLibs
2896 if !reflect.DeepEqual(actual, expected) {
2897 t.Errorf("incorrect static_libs"+
2898 "\nactual: %v"+
2899 "\nexpected: %v",
2900 actual,
2901 expected,
2902 )
2903 }
2904}
2905
2906const staticLibAndroidBp = `
2907 cc_library {
2908 name: "lib1",
2909 }
2910 cc_library {
2911 name: "lib2",
2912 static_libs: ["lib1"],
2913 }
2914`
2915
2916func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002917 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002918 ctx := testCc(t, staticLibAndroidBp)
2919
2920 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002921 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002922 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002923 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002924
2925 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002926 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002927 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2928 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002929 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002930}
2931
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002932func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
2933 bp := `
2934 cc_library {
2935 name: "static_dep",
2936 }
2937 cc_library {
2938 name: "whole_static_dep",
2939 }
2940 cc_library {
2941 name: "shared_dep",
2942 }
2943 cc_library {
2944 name: "lib",
2945 bazel_module: { label: "//:lib" },
2946 static_libs: ["static_dep"],
2947 whole_static_libs: ["whole_static_dep"],
2948 shared_libs: ["shared_dep"],
2949 }
2950 cc_test {
2951 name: "test",
2952 bazel_module: { label: "//:test" },
2953 static_libs: ["static_dep"],
2954 whole_static_libs: ["whole_static_dep"],
2955 shared_libs: ["shared_dep"],
2956 gtest: false,
Sam Delmericoef69d472023-04-18 17:32:43 -04002957 sanitize: {
2958 // cc_test modules default to memtag_heap: true,
2959 // but this adds extra dependencies that we don't care about
2960 never: true,
2961 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002962 }
2963 cc_binary {
2964 name: "binary",
2965 bazel_module: { label: "//:binary" },
2966 static_libs: ["static_dep"],
2967 whole_static_libs: ["whole_static_dep"],
2968 shared_libs: ["shared_dep"],
2969 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05002970 cc_library_headers {
2971 name: "lib_headers",
2972 bazel_module: { label: "//:lib_headers" },
2973 static_libs: ["static_dep"],
2974 whole_static_libs: ["whole_static_dep"],
2975 shared_libs: ["shared_dep"],
2976 }
2977 cc_prebuilt_library {
2978 name: "lib_prebuilt",
2979 bazel_module: { label: "//:lib_prebuilt" },
2980 static_libs: ["static_dep"],
2981 whole_static_libs: ["whole_static_dep"],
2982 shared_libs: ["shared_dep"],
2983 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05002984 `
2985
2986 testCases := []struct {
2987 name string
2988 moduleName string
2989 variant string
2990 androidMkInfo cquery.CcAndroidMkInfo
2991 }{
2992 {
2993 name: "shared lib",
2994 moduleName: "lib",
2995 variant: "android_arm64_armv8-a_shared",
2996 androidMkInfo: cquery.CcAndroidMkInfo{
2997 LocalStaticLibs: []string{"static_dep"},
2998 LocalWholeStaticLibs: []string{"whole_static_dep"},
2999 LocalSharedLibs: []string{"shared_dep"},
3000 },
3001 },
3002 {
3003 name: "static lib",
3004 moduleName: "lib",
3005 variant: "android_arm64_armv8-a_static",
3006 androidMkInfo: cquery.CcAndroidMkInfo{
3007 LocalStaticLibs: []string{"static_dep"},
3008 LocalWholeStaticLibs: []string{"whole_static_dep"},
3009 LocalSharedLibs: []string{"shared_dep"},
3010 },
3011 },
3012 {
3013 name: "cc_test arm64",
3014 moduleName: "test",
3015 variant: "android_arm64_armv8-a",
3016 androidMkInfo: cquery.CcAndroidMkInfo{
3017 LocalStaticLibs: []string{"static_dep"},
3018 LocalWholeStaticLibs: []string{"whole_static_dep"},
3019 LocalSharedLibs: []string{"shared_dep"},
3020 },
3021 },
3022 {
3023 name: "cc_test arm",
3024 moduleName: "test",
3025 variant: "android_arm_armv7-a-neon",
3026 androidMkInfo: cquery.CcAndroidMkInfo{
3027 LocalStaticLibs: []string{"static_dep"},
3028 LocalWholeStaticLibs: []string{"whole_static_dep"},
3029 LocalSharedLibs: []string{"shared_dep"},
3030 },
3031 },
3032 {
3033 name: "cc_binary",
3034 moduleName: "binary",
3035 variant: "android_arm64_armv8-a",
3036 androidMkInfo: cquery.CcAndroidMkInfo{
3037 LocalStaticLibs: []string{"static_dep"},
3038 LocalWholeStaticLibs: []string{"whole_static_dep"},
3039 LocalSharedLibs: []string{"shared_dep"},
3040 },
3041 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003042 {
3043 name: "cc_library_headers",
3044 moduleName: "lib_headers",
3045 variant: "android_arm64_armv8-a",
3046 androidMkInfo: cquery.CcAndroidMkInfo{
3047 LocalStaticLibs: []string{"static_dep"},
3048 LocalWholeStaticLibs: []string{"whole_static_dep"},
3049 LocalSharedLibs: []string{"shared_dep"},
3050 },
3051 },
3052 {
3053 name: "prebuilt lib static",
3054 moduleName: "lib_prebuilt",
3055 variant: "android_arm64_armv8-a_static",
3056 androidMkInfo: cquery.CcAndroidMkInfo{
3057 LocalStaticLibs: []string{"static_dep"},
3058 LocalWholeStaticLibs: []string{"whole_static_dep"},
3059 LocalSharedLibs: []string{"shared_dep"},
3060 },
3061 },
3062 {
3063 name: "prebuilt lib shared",
3064 moduleName: "lib_prebuilt",
3065 variant: "android_arm64_armv8-a_shared",
3066 androidMkInfo: cquery.CcAndroidMkInfo{
3067 LocalStaticLibs: []string{"static_dep"},
3068 LocalWholeStaticLibs: []string{"whole_static_dep"},
3069 LocalSharedLibs: []string{"shared_dep"},
3070 },
3071 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003072 }
3073
3074 outputBaseDir := "out/bazel"
3075 for _, tc := range testCases {
3076 t.Run(tc.name, func(t *testing.T) {
3077 result := android.GroupFixturePreparers(
3078 prepareForCcTest,
3079 android.FixtureModifyConfig(func(config android.Config) {
3080 config.BazelContext = android.MockBazelContext{
3081 OutputBaseDir: outputBaseDir,
3082 LabelToCcInfo: map[string]cquery.CcInfo{
3083 "//:lib": cquery.CcInfo{
3084 CcAndroidMkInfo: tc.androidMkInfo,
3085 RootDynamicLibraries: []string{""},
3086 },
3087 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3088 CcAndroidMkInfo: tc.androidMkInfo,
3089 RootStaticArchives: []string{""},
3090 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003091 "//:lib_headers": cquery.CcInfo{
3092 CcAndroidMkInfo: tc.androidMkInfo,
3093 OutputFiles: []string{""},
3094 },
3095 "//:lib_prebuilt": cquery.CcInfo{
3096 CcAndroidMkInfo: tc.androidMkInfo,
3097 },
3098 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3099 CcAndroidMkInfo: tc.androidMkInfo,
3100 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003101 },
3102 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
Jingwen Chen6ee23ad2023-07-24 14:56:28 +00003103 "//:test__tf_internal": cquery.CcUnstrippedInfo{
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003104 CcAndroidMkInfo: tc.androidMkInfo,
3105 },
3106 "//:binary": cquery.CcUnstrippedInfo{
3107 CcAndroidMkInfo: tc.androidMkInfo,
3108 },
3109 },
3110 }
3111 }),
3112 ).RunTestWithBp(t, bp)
3113 ctx := result.TestContext
3114
3115 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3116 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003117 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3118 t.Errorf("incorrect static_libs"+
3119 "\nactual: %v"+
3120 "\nexpected: %v",
3121 module.Properties.AndroidMkStaticLibs,
3122 tc.androidMkInfo.LocalStaticLibs,
3123 )
3124 }
3125 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3126 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3127 tc.androidMkInfo.LocalStaticLibs,
3128 )
3129 if staticDepsDiffer {
3130 t.Errorf(
3131 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3132 tc.androidMkInfo.LocalStaticLibs,
3133 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3134 missingStaticDeps,
3135 additionalStaticDeps,
3136 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003137 }
3138
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003139 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3140 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3141 tc.androidMkInfo.LocalWholeStaticLibs,
3142 module.Properties.AndroidMkWholeStaticLibs,
3143 )
3144 }
3145 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3146 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3147 tc.androidMkInfo.LocalWholeStaticLibs,
3148 )
3149 if wholeStaticDepsDiffer {
3150 t.Errorf(
3151 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3152 tc.androidMkInfo.LocalWholeStaticLibs,
3153 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3154 missingWholeStaticDeps,
3155 additionalWholeStaticDeps,
3156 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003157 }
3158
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003159 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3160 t.Errorf("incorrect shared_libs"+
3161 "\nactual: %v"+
3162 "\nexpected: %v",
3163 module.Properties.AndroidMkSharedLibs,
3164 tc.androidMkInfo.LocalSharedLibs,
3165 )
3166 }
3167 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3168 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3169 tc.androidMkInfo.LocalSharedLibs,
3170 )
3171 if sharedDepsDiffer {
3172 t.Errorf(
3173 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3174 tc.androidMkInfo.LocalSharedLibs,
3175 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3176 missingSharedDeps,
3177 additionalSharedDeps,
3178 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003179 }
3180 })
3181 }
3182}
3183
Jiyong Parkd08b6972017-09-26 10:50:54 +09003184var compilerFlagsTestCases = []struct {
3185 in string
3186 out bool
3187}{
3188 {
3189 in: "a",
3190 out: false,
3191 },
3192 {
3193 in: "-a",
3194 out: true,
3195 },
3196 {
3197 in: "-Ipath/to/something",
3198 out: false,
3199 },
3200 {
3201 in: "-isystempath/to/something",
3202 out: false,
3203 },
3204 {
3205 in: "--coverage",
3206 out: false,
3207 },
3208 {
3209 in: "-include a/b",
3210 out: true,
3211 },
3212 {
3213 in: "-include a/b c/d",
3214 out: false,
3215 },
3216 {
3217 in: "-DMACRO",
3218 out: true,
3219 },
3220 {
3221 in: "-DMAC RO",
3222 out: false,
3223 },
3224 {
3225 in: "-a -b",
3226 out: false,
3227 },
3228 {
3229 in: "-DMACRO=definition",
3230 out: true,
3231 },
3232 {
3233 in: "-DMACRO=defi nition",
3234 out: true, // TODO(jiyong): this should be false
3235 },
3236 {
3237 in: "-DMACRO(x)=x + 1",
3238 out: true,
3239 },
3240 {
3241 in: "-DMACRO=\"defi nition\"",
3242 out: true,
3243 },
3244}
3245
3246type mockContext struct {
3247 BaseModuleContext
3248 result bool
3249}
3250
3251func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3252 // CheckBadCompilerFlags calls this function when the flag should be rejected
3253 ctx.result = false
3254}
3255
3256func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003257 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003258 for _, testCase := range compilerFlagsTestCases {
3259 ctx := &mockContext{result: true}
3260 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3261 if ctx.result != testCase.out {
3262 t.Errorf("incorrect output:")
3263 t.Errorf(" input: %#v", testCase.in)
3264 t.Errorf(" expected: %#v", testCase.out)
3265 t.Errorf(" got: %#v", ctx.result)
3266 }
3267 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003268}
Jiyong Park374510b2018-03-19 18:23:01 +09003269
Jiyong Park37b25202018-07-11 10:49:27 +09003270func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003271 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003272 ctx := testCc(t, `
3273 cc_library_shared {
3274 name: "librecovery",
3275 recovery: true,
3276 }
3277 cc_library_shared {
3278 name: "librecovery32",
3279 recovery: true,
3280 compile_multilib:"32",
3281 }
Jiyong Park5baac542018-08-28 09:55:37 +09003282 cc_library_shared {
3283 name: "libHalInRecovery",
3284 recovery_available: true,
3285 vendor: true,
3286 }
Jiyong Park37b25202018-07-11 10:49:27 +09003287 `)
3288
3289 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003290 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003291 if len(variants) != 1 || !android.InList(arm64, variants) {
3292 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3293 }
3294
3295 variants = ctx.ModuleVariantsForTests("librecovery32")
3296 if android.InList(arm64, variants) {
3297 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3298 }
Jiyong Park5baac542018-08-28 09:55:37 +09003299
3300 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3301 if !recoveryModule.Platform() {
3302 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3303 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003304}
Jiyong Park5baac542018-08-28 09:55:37 +09003305
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003306func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003307 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003308 bp := `
3309 cc_prebuilt_test_library_shared {
3310 name: "test_lib",
3311 relative_install_path: "foo/bar/baz",
3312 srcs: ["srcpath/dontusethispath/baz.so"],
3313 }
3314
3315 cc_test {
3316 name: "main_test",
3317 data_libs: ["test_lib"],
3318 gtest: false,
3319 }
3320 `
3321
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003322 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003323 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003324 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003325 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3326
3327 ctx := testCcWithConfig(t, config)
3328 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3329 testBinary := module.(*Module).linker.(*testBinary)
3330 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3331 if err != nil {
3332 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3333 }
3334 if len(outputFiles) != 1 {
3335 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3336 }
3337 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003338 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003339 }
3340
3341 outputPath := outputFiles[0].String()
3342
3343 if !strings.HasSuffix(outputPath, "/main_test") {
3344 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3345 }
Colin Crossaa255532020-07-03 13:18:24 -07003346 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003347 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3348 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3349 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3350 }
3351}
3352
Jiyong Park7ed9de32018-10-15 22:25:07 +09003353func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003354 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003355 ctx := testCc(t, `
3356 cc_library_shared {
3357 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003358 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003359 stubs: {
3360 symbol_file: "foo.map.txt",
3361 versions: ["1", "2", "3"],
3362 },
3363 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003364
Jiyong Park7ed9de32018-10-15 22:25:07 +09003365 cc_library_shared {
3366 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003367 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003368 shared_libs: ["libFoo#1"],
3369 }`)
3370
3371 variants := ctx.ModuleVariantsForTests("libFoo")
3372 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003373 "android_arm64_armv8-a_shared",
3374 "android_arm64_armv8-a_shared_1",
3375 "android_arm64_armv8-a_shared_2",
3376 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003377 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003378 "android_arm_armv7-a-neon_shared",
3379 "android_arm_armv7-a-neon_shared_1",
3380 "android_arm_armv7-a-neon_shared_2",
3381 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003382 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003383 }
3384 variantsMismatch := false
3385 if len(variants) != len(expectedVariants) {
3386 variantsMismatch = true
3387 } else {
3388 for _, v := range expectedVariants {
3389 if !inList(v, variants) {
3390 variantsMismatch = false
3391 }
3392 }
3393 }
3394 if variantsMismatch {
3395 t.Errorf("variants of libFoo expected:\n")
3396 for _, v := range expectedVariants {
3397 t.Errorf("%q\n", v)
3398 }
3399 t.Errorf(", but got:\n")
3400 for _, v := range variants {
3401 t.Errorf("%q\n", v)
3402 }
3403 }
3404
Colin Cross7113d202019-11-20 16:39:12 -08003405 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003406 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003407 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003408 if !strings.Contains(libFlags, libFoo1StubPath) {
3409 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3410 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003411
Colin Cross7113d202019-11-20 16:39:12 -08003412 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003413 cFlags := libBarCompileRule.Args["cFlags"]
3414 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3415 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3416 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3417 }
Jiyong Park37b25202018-07-11 10:49:27 +09003418}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003419
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003420func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3421 t.Parallel()
3422 ctx := testCc(t, `
3423 cc_library_shared {
3424 name: "libFoo",
3425 srcs: ["foo.c"],
3426 stubs: {
3427 symbol_file: "foo.map.txt",
3428 versions: ["current"],
3429 },
3430 apex_available: ["bar", "a1"],
3431 }
3432
3433 cc_library_shared {
3434 name: "libBar",
3435 srcs: ["bar.c"],
3436 shared_libs: ["libFoo"],
3437 apex_available: ["a1"],
3438 }
3439
3440 cc_library_shared {
3441 name: "libA1",
3442 srcs: ["a1.c"],
3443 shared_libs: ["libFoo"],
3444 apex_available: ["a1"],
3445 }
3446
3447 cc_library_shared {
3448 name: "libBarA1",
3449 srcs: ["bara1.c"],
3450 shared_libs: ["libFoo"],
3451 apex_available: ["bar", "a1"],
3452 }
3453
3454 cc_library_shared {
3455 name: "libAnyApex",
3456 srcs: ["anyApex.c"],
3457 shared_libs: ["libFoo"],
3458 apex_available: ["//apex_available:anyapex"],
3459 }
3460
3461 cc_library_shared {
3462 name: "libBaz",
3463 srcs: ["baz.c"],
3464 shared_libs: ["libFoo"],
3465 apex_available: ["baz"],
3466 }
3467
3468 cc_library_shared {
3469 name: "libQux",
3470 srcs: ["qux.c"],
3471 shared_libs: ["libFoo"],
3472 apex_available: ["qux", "bar"],
3473 }`)
3474
3475 variants := ctx.ModuleVariantsForTests("libFoo")
3476 expectedVariants := []string{
3477 "android_arm64_armv8-a_shared",
3478 "android_arm64_armv8-a_shared_current",
3479 "android_arm_armv7-a-neon_shared",
3480 "android_arm_armv7-a-neon_shared_current",
3481 }
3482 variantsMismatch := false
3483 if len(variants) != len(expectedVariants) {
3484 variantsMismatch = true
3485 } else {
3486 for _, v := range expectedVariants {
3487 if !inList(v, variants) {
3488 variantsMismatch = false
3489 }
3490 }
3491 }
3492 if variantsMismatch {
3493 t.Errorf("variants of libFoo expected:\n")
3494 for _, v := range expectedVariants {
3495 t.Errorf("%q\n", v)
3496 }
3497 t.Errorf(", but got:\n")
3498 for _, v := range variants {
3499 t.Errorf("%q\n", v)
3500 }
3501 }
3502
3503 linkAgainstFoo := []string{"libBarA1"}
3504 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3505
3506 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3507 for _, lib := range linkAgainstFoo {
3508 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3509 libFlags := libLinkRule.Args["libFlags"]
3510 if !strings.Contains(libFlags, libFooPath) {
3511 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3512 }
3513 }
3514
3515 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3516 for _, lib := range linkAgainstFooStubs {
3517 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3518 libFlags := libLinkRule.Args["libFlags"]
3519 if !strings.Contains(libFlags, libFooStubPath) {
3520 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3521 }
3522 }
3523}
3524
Sam Delmerico75dbca22023-04-20 13:13:25 +00003525func TestMixedBuildUsesStubs(t *testing.T) {
Sam Delmerico75dbca22023-04-20 13:13:25 +00003526 t.Parallel()
3527 bp := `
3528 cc_library_shared {
3529 name: "libFoo",
3530 bazel_module: { label: "//:libFoo" },
3531 srcs: ["foo.c"],
3532 stubs: {
3533 symbol_file: "foo.map.txt",
3534 versions: ["current"],
3535 },
3536 apex_available: ["bar", "a1"],
3537 }
3538
3539 cc_library_shared {
3540 name: "libBar",
3541 srcs: ["bar.c"],
3542 shared_libs: ["libFoo"],
3543 apex_available: ["a1"],
3544 }
3545
3546 cc_library_shared {
3547 name: "libA1",
3548 srcs: ["a1.c"],
3549 shared_libs: ["libFoo"],
3550 apex_available: ["a1"],
3551 }
3552
3553 cc_library_shared {
3554 name: "libBarA1",
3555 srcs: ["bara1.c"],
3556 shared_libs: ["libFoo"],
3557 apex_available: ["bar", "a1"],
3558 }
3559
3560 cc_library_shared {
3561 name: "libAnyApex",
3562 srcs: ["anyApex.c"],
3563 shared_libs: ["libFoo"],
3564 apex_available: ["//apex_available:anyapex"],
3565 }
3566
3567 cc_library_shared {
3568 name: "libBaz",
3569 srcs: ["baz.c"],
3570 shared_libs: ["libFoo"],
3571 apex_available: ["baz"],
3572 }
3573
3574 cc_library_shared {
3575 name: "libQux",
3576 srcs: ["qux.c"],
3577 shared_libs: ["libFoo"],
3578 apex_available: ["qux", "bar"],
3579 }`
3580
3581 result := android.GroupFixturePreparers(
3582 prepareForCcTest,
3583 android.FixtureModifyConfig(func(config android.Config) {
3584 config.BazelContext = android.MockBazelContext{
3585 OutputBaseDir: "out/bazel",
3586 LabelToCcInfo: map[string]cquery.CcInfo{
3587 "//:libFoo": {
3588 RootDynamicLibraries: []string{"libFoo.so"},
3589 },
3590 "//:libFoo_stub_libs-current": {
3591 RootDynamicLibraries: []string{"libFoo_stub_libs-current.so"},
3592 },
3593 },
3594 }
3595 }),
3596 ).RunTestWithBp(t, bp)
3597 ctx := result.TestContext
3598
3599 variants := ctx.ModuleVariantsForTests("libFoo")
3600 expectedVariants := []string{
3601 "android_arm64_armv8-a_shared",
3602 "android_arm64_armv8-a_shared_current",
3603 "android_arm_armv7-a-neon_shared",
3604 "android_arm_armv7-a-neon_shared_current",
3605 }
3606 variantsMismatch := false
3607 if len(variants) != len(expectedVariants) {
3608 variantsMismatch = true
3609 } else {
3610 for _, v := range expectedVariants {
3611 if !inList(v, variants) {
3612 variantsMismatch = false
3613 }
3614 }
3615 }
3616 if variantsMismatch {
3617 t.Errorf("variants of libFoo expected:\n")
3618 for _, v := range expectedVariants {
3619 t.Errorf("%q\n", v)
3620 }
3621 t.Errorf(", but got:\n")
3622 for _, v := range variants {
3623 t.Errorf("%q\n", v)
3624 }
3625 }
3626
3627 linkAgainstFoo := []string{"libBarA1"}
3628 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3629
3630 libFooPath := "out/bazel/execroot/__main__/libFoo.so"
3631 for _, lib := range linkAgainstFoo {
3632 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3633 libFlags := libLinkRule.Args["libFlags"]
3634 if !strings.Contains(libFlags, libFooPath) {
3635 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3636 }
3637 }
3638
3639 libFooStubPath := "out/bazel/execroot/__main__/libFoo_stub_libs-current.so"
3640 for _, lib := range linkAgainstFooStubs {
3641 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3642 libFlags := libLinkRule.Args["libFlags"]
3643 if !strings.Contains(libFlags, libFooStubPath) {
3644 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3645 }
3646 }
3647}
3648
Jooyung Hanb04a4992020-03-13 18:57:35 +09003649func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003650 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003651 for _, tc := range []struct{ moduleName, expected string }{
3652 {"libc", "__LIBC_API__"},
3653 {"libfoo", "__LIBFOO_API__"},
3654 {"libfoo@1", "__LIBFOO_1_API__"},
3655 {"libfoo-v1", "__LIBFOO_V1_API__"},
3656 {"libfoo.v1", "__LIBFOO_V1_API__"},
3657 } {
3658 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3659 }
3660}
3661
Liz Kammer83cf81b2022-09-22 08:24:20 -04003662func pathsToBase(paths android.Paths) []string {
3663 var ret []string
3664 for _, p := range paths {
3665 ret = append(ret, p.Base())
3666 }
3667 return ret
3668}
3669
3670func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003671 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003672 ctx := testCc(t, `
3673 cc_library_static {
3674 name: "foo",
3675 srcs: ["foo.c"],
3676 }
3677
3678 cc_library_static {
3679 name: "bar",
3680 srcs: ["bar.c"],
3681 }
3682
3683 cc_library_shared {
3684 name: "qux",
3685 srcs: ["qux.c"],
3686 }
3687
3688 cc_library_static {
3689 name: "baz",
3690 srcs: ["baz.c"],
3691 static_libs: ["foo"],
3692 shared_libs: ["qux"],
3693 whole_static_libs: ["bar"],
3694 }`)
3695
3696 variant := "android_arm64_armv8-a_static"
3697 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3698
3699 // For static libraries, the object files of a whole static dep are included in the archive
3700 // directly
3701 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3702 t.Errorf("Expected input objects %q, got %q", w, g)
3703 }
3704
3705 // non whole static dependencies are not linked into the archive
3706 if len(arRule.Implicits) > 0 {
3707 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3708 }
3709}
3710
3711func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003712 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003713 ctx := testCc(t, `
3714 cc_library_static {
3715 name: "foo",
3716 srcs: ["foo.c"],
3717 }
3718
3719 cc_library_static {
3720 name: "bar",
3721 srcs: ["bar.c"],
3722 }
3723
3724 cc_library_shared {
3725 name: "qux",
3726 srcs: ["qux.c"],
3727 }
3728
3729 cc_library_shared {
3730 name: "baz",
3731 srcs: ["baz.c"],
3732 static_libs: ["foo"],
3733 shared_libs: ["qux"],
3734 whole_static_libs: ["bar"],
3735 }`)
3736
3737 variant := "android_arm64_armv8-a_shared"
3738 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3739 libFlags := linkRule.Args["libFlags"]
3740 // When dynamically linking, we expect static dependencies to be found on the command line
3741 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3742 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3743 }
3744 // When dynamically linking, we expect whole static dependencies to be found on the command line
3745 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3746 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3747 }
3748
3749 // When dynamically linking, we expect shared dependencies to be found on the command line
3750 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3751 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3752 }
3753
3754 // We should only have the objects from the shared library srcs, not the whole static dependencies
3755 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3756 t.Errorf("Expected input objects %q, got %q", w, g)
3757 }
3758}
3759
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003760func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003761 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003762 ctx := testCc(t, `
3763 cc_binary {
3764 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003765 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003766 static_executable: true,
3767 }`)
3768
Colin Cross7113d202019-11-20 16:39:12 -08003769 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003770 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3771 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003772 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003773 for _, lib := range systemStaticLibs {
3774 if !strings.Contains(libFlags, lib) {
3775 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3776 }
3777 }
3778 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3779 for _, lib := range systemSharedLibs {
3780 if strings.Contains(libFlags, lib) {
3781 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3782 }
3783 }
3784}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003785
3786func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003787 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003788 ctx := testCc(t, `
3789 cc_binary {
3790 name: "mybin",
3791 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003792 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003793 static_executable: true,
3794 stl: "none",
3795 }
3796
3797 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003798 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003799 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003800 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003801 stl: "none",
3802 }
3803
3804 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003805 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003806 srcs: ["foo.c"],
3807 stl: "none",
3808 stubs: {
3809 versions: ["1"],
3810 },
3811 }`)
3812
Colin Cross0de8a1e2020-09-18 14:15:30 -07003813 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3814 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003815 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003816
3817 if !reflect.DeepEqual(actual, expected) {
3818 t.Errorf("staticDeps orderings were not propagated correctly"+
3819 "\nactual: %v"+
3820 "\nexpected: %v",
3821 actual,
3822 expected,
3823 )
3824 }
3825}
Jooyung Han38002912019-05-16 04:01:54 +09003826
Jooyung Hand48f3c32019-08-23 11:18:57 +09003827func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003828 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003829 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3830 cc_library {
3831 name: "libA",
3832 srcs: ["foo.c"],
3833 shared_libs: ["libB"],
3834 stl: "none",
3835 }
3836
3837 cc_library {
3838 name: "libB",
3839 srcs: ["foo.c"],
3840 enabled: false,
3841 stl: "none",
3842 }
3843 `)
3844}
3845
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003846func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3847 bp := `
3848 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003849 name: "test_afl_fuzz_target",
3850 srcs: ["foo.c"],
3851 host_supported: true,
3852 static_libs: [
3853 "afl_fuzz_static_lib",
3854 ],
3855 shared_libs: [
3856 "afl_fuzz_shared_lib",
3857 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003858 fuzzing_frameworks: {
3859 afl: true,
3860 libfuzzer: false,
3861 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003862 }
3863 cc_library {
3864 name: "afl_fuzz_static_lib",
3865 host_supported: true,
3866 srcs: ["static_file.c"],
3867 }
3868 cc_library {
3869 name: "libfuzzer_only_static_lib",
3870 host_supported: true,
3871 srcs: ["static_file.c"],
3872 }
3873 cc_library {
3874 name: "afl_fuzz_shared_lib",
3875 host_supported: true,
3876 srcs: ["shared_file.c"],
3877 static_libs: [
3878 "second_static_lib",
3879 ],
3880 }
3881 cc_library_headers {
3882 name: "libafl_headers",
3883 vendor_available: true,
3884 host_supported: true,
3885 export_include_dirs: [
3886 "include",
3887 "instrumentation",
3888 ],
3889 }
3890 cc_object {
3891 name: "afl-compiler-rt",
3892 vendor_available: true,
3893 host_supported: true,
3894 cflags: [
3895 "-fPIC",
3896 ],
3897 srcs: [
3898 "instrumentation/afl-compiler-rt.o.c",
3899 ],
3900 }
3901 cc_library {
3902 name: "second_static_lib",
3903 host_supported: true,
3904 srcs: ["second_file.c"],
3905 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003906 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003907 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003908 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003909 srcs: [
3910 "aflpp_driver.c",
3911 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003912 }`
3913
3914 testEnv := map[string]string{
3915 "FUZZ_FRAMEWORK": "AFL",
3916 }
3917
3918 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003919
3920 checkPcGuardFlag := func(
3921 modName string, variantName string, shouldHave bool) {
3922 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3923
3924 cFlags, ok := cc.Args["cFlags"]
3925 if !ok {
3926 t.Errorf("Could not find cFlags for module %s and variant %s",
3927 modName, variantName)
3928 }
3929
3930 if strings.Contains(
3931 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3932 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3933 "Test failed for module %s and variant %s",
3934 !shouldHave, shouldHave, modName, variantName)
3935 }
3936 }
3937
Cory Barkera1da26f2022-06-07 20:12:06 +00003938 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003939 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003940
3941 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003942 checkPcGuardFlag(moduleName, variant+"_static", false)
3943 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003944
3945 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003946 checkPcGuardFlag(moduleName, variant+"_static", false)
3947 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003948
3949 ctx.ModuleForTests("afl_fuzz_shared_lib",
3950 "android_arm64_armv8-a_shared").Rule("cc")
3951 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003952 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3953}
3954
3955func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003956 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003957 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3958}
3959
3960func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003961 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003962 if runtime.GOOS != "linux" {
3963 t.Skip("requires linux")
3964 }
3965
3966 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003967}
3968
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003969// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3970// correctly.
3971func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003972 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003973 ctx := testCc(t, `
3974 cc_fuzz {
3975 name: "fuzz_smoke_test",
3976 srcs: ["foo.c"],
3977 }`)
3978
Paul Duffin075c4172019-12-19 19:06:13 +00003979 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003980 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3981}
3982
Jooyung Han38002912019-05-16 04:01:54 +09003983func assertString(t *testing.T, got, expected string) {
3984 t.Helper()
3985 if got != expected {
3986 t.Errorf("expected %q got %q", expected, got)
3987 }
3988}
3989
3990func assertArrayString(t *testing.T, got, expected []string) {
3991 t.Helper()
3992 if len(got) != len(expected) {
3993 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3994 return
3995 }
3996 for i := range got {
3997 if got[i] != expected[i] {
3998 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3999 i, expected[i], expected, got[i], got)
4000 return
4001 }
4002 }
4003}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004004
Jooyung Han0302a842019-10-30 18:43:49 +09004005func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4006 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08004007 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09004008}
4009
Colin Crosse1bb5d02019-09-24 14:55:04 -07004010func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004011 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07004012 ctx := testCc(t, `
4013 cc_defaults {
4014 name: "defaults",
4015 srcs: ["foo.c"],
4016 static: {
4017 srcs: ["bar.c"],
4018 },
4019 shared: {
4020 srcs: ["baz.c"],
4021 },
Liz Kammer3cf52112021-03-31 15:42:03 -04004022 bazel_module: {
4023 bp2build_available: true,
4024 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07004025 }
4026
4027 cc_library_static {
4028 name: "libstatic",
4029 defaults: ["defaults"],
4030 }
4031
4032 cc_library_shared {
4033 name: "libshared",
4034 defaults: ["defaults"],
4035 }
4036
4037 cc_library {
4038 name: "libboth",
4039 defaults: ["defaults"],
4040 }
4041
4042 cc_binary {
4043 name: "binary",
4044 defaults: ["defaults"],
4045 }`)
4046
Colin Cross7113d202019-11-20 16:39:12 -08004047 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004048 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4049 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4050 }
Colin Cross7113d202019-11-20 16:39:12 -08004051 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004052 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4053 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4054 }
Colin Cross7113d202019-11-20 16:39:12 -08004055 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004056 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4057 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4058 }
4059
Colin Cross7113d202019-11-20 16:39:12 -08004060 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004061 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4062 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4063 }
Colin Cross7113d202019-11-20 16:39:12 -08004064 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004065 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4066 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4067 }
4068}
Colin Crosseabaedd2020-02-06 17:01:55 -08004069
4070func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004071 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08004072 bp := `
4073 cc_defaults {
4074 name: "libfoo_defaults",
4075 srcs: ["foo.c"],
4076 cppflags: ["-DFOO"],
4077 product_variables: {
4078 debuggable: {
4079 cppflags: ["-DBAR"],
4080 },
4081 },
4082 }
4083
4084 cc_library {
4085 name: "libfoo",
4086 defaults: ["libfoo_defaults"],
4087 }
4088 `
4089
Paul Duffin8567f222021-03-23 00:02:06 +00004090 result := android.GroupFixturePreparers(
4091 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004092 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08004093
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004094 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4095 variables.Debuggable = BoolPtr(true)
4096 }),
4097 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08004098
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004099 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00004100 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08004101}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004102
4103func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4104 t.Parallel()
4105 bp := `
4106 cc_library_static {
4107 name: "libfoo",
4108 srcs: ["foo.c"],
4109 whole_static_libs: ["libbar"],
4110 }
4111
4112 cc_library_static {
4113 name: "libbar",
4114 whole_static_libs: ["libmissing"],
4115 }
4116 `
4117
Paul Duffin8567f222021-03-23 00:02:06 +00004118 result := android.GroupFixturePreparers(
4119 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004120 android.PrepareForTestWithAllowMissingDependencies,
4121 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004122
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004123 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004124 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004125
Paul Duffine84b1332021-03-12 11:59:43 +00004126 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004127
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004128 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004129 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004130}
Colin Crosse9fe2942020-11-10 18:12:15 -08004131
4132func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004133 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004134 bp := `
4135 cc_binary {
4136 name: "bin",
4137 host_supported: true,
4138 shared_libs: ["libshared"],
4139 runtime_libs: ["libruntime"],
4140 srcs: [":gen"],
4141 }
4142
4143 cc_library_shared {
4144 name: "libshared",
4145 host_supported: true,
4146 shared_libs: ["libtransitive"],
4147 }
4148
4149 cc_library_shared {
4150 name: "libtransitive",
4151 host_supported: true,
4152 }
4153
4154 cc_library_shared {
4155 name: "libruntime",
4156 host_supported: true,
4157 }
4158
4159 cc_binary_host {
4160 name: "tool",
4161 srcs: ["foo.cpp"],
4162 }
4163
4164 genrule {
4165 name: "gen",
4166 tools: ["tool"],
4167 out: ["gen.cpp"],
4168 cmd: "$(location tool) $(out)",
4169 }
4170 `
4171
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004172 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004173 ctx := testCcWithConfig(t, config)
4174
4175 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4176 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4177 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4178 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4179 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4180
4181 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4182 t.Errorf("expected host bin dependency %q, got %q", w, g)
4183 }
4184
4185 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4186 t.Errorf("expected host bin dependency %q, got %q", w, g)
4187 }
4188
4189 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4190 t.Errorf("expected host bin dependency %q, got %q", w, g)
4191 }
4192
4193 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4194 t.Errorf("expected host bin dependency %q, got %q", w, g)
4195 }
4196
4197 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4198 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4199 }
4200
4201 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4202 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4203 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4204 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4205
4206 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4207 t.Errorf("expected device bin dependency %q, got %q", w, g)
4208 }
4209
4210 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4211 t.Errorf("expected device bin dependency %q, got %q", w, g)
4212 }
4213
4214 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4215 t.Errorf("expected device bin dependency %q, got %q", w, g)
4216 }
4217
4218 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4219 t.Errorf("expected device bin dependency %q, got %q", w, g)
4220 }
4221
4222 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4223 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4224 }
4225
4226}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004227
4228func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004229 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004230 ctx := testCc(t, `
4231 cc_library_shared {
4232 name: "libclient",
4233 srcs: ["foo.c"],
4234 shared_libs: ["libfoo#1"],
4235 }
4236
4237 cc_library_shared {
4238 name: "libfoo",
4239 srcs: ["foo.c"],
4240 shared_libs: ["libbar"],
4241 export_shared_lib_headers: ["libbar"],
4242 stubs: {
4243 symbol_file: "foo.map.txt",
4244 versions: ["1", "2", "3"],
4245 },
4246 }
4247
4248 cc_library_shared {
4249 name: "libbar",
4250 export_include_dirs: ["include/libbar"],
4251 srcs: ["foo.c"],
4252 }`)
4253
4254 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4255
4256 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4257 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4258 }
4259}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004260
Vinh Tran09581952023-05-16 16:03:20 -04004261func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004262 t.Parallel()
4263 ctx := android.GroupFixturePreparers(
4264 prepareForCcTest,
4265 aidl_library.PrepareForTestWithAidlLibrary,
4266 android.MockFS{
4267 "package_bar/Android.bp": []byte(`
4268 aidl_library {
4269 name: "bar",
4270 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004271 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004272 strip_import_prefix: "x",
4273 }
4274 `)}.AddToFixture(),
4275 android.MockFS{
4276 "package_foo/Android.bp": []byte(`
4277 aidl_library {
4278 name: "foo",
4279 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04004280 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04004281 strip_import_prefix: "a",
4282 deps: ["bar"],
4283 }
4284 cc_library {
4285 name: "libfoo",
4286 aidl: {
4287 libs: ["foo"],
4288 }
4289 }
4290 `),
4291 }.AddToFixture(),
4292 ).RunTest(t).TestContext
4293
4294 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04004295
4296 android.AssertPathsRelativeToTopEquals(
4297 t,
4298 "aidl headers",
4299 []string{
4300 "package_bar/x/HeaderBar.aidl",
4301 "package_foo/a/HeaderFoo.aidl",
4302 "package_foo/a/b/Foo.aidl",
4303 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
4304 },
4305 libfoo.Rule("aidl_library").Implicits,
4306 )
4307
4308 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04004309 aidlCommand := manifest.Commands[0].GetCommand()
4310
4311 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
4312 if !strings.Contains(aidlCommand, expectedAidlFlags) {
4313 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
4314 }
4315
4316 outputs := strings.Join(libfoo.AllOutputs(), " ")
4317
Vinh Tran09581952023-05-16 16:03:20 -04004318 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
4319 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
4320 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004321 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
4322 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04004323 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
4324 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
4325 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04004326 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
4327}
4328
Jooyung Hane197d8b2021-01-05 10:33:16 +09004329func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004330 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04004331 ctx := android.GroupFixturePreparers(
4332 prepareForCcTest,
4333 aidl_library.PrepareForTestWithAidlLibrary,
4334 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09004335 cc_library {
4336 name: "libfoo",
4337 srcs: ["a/Foo.aidl"],
4338 aidl: { flags: ["-Werror"], },
4339 }
4340 `)
4341
4342 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4343 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4344 aidlCommand := manifest.Commands[0].GetCommand()
4345 expectedAidlFlag := "-Werror"
4346 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4347 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4348 }
4349}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004350
Jooyung Han07f70c02021-11-06 07:08:45 +09004351func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004352 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004353 for _, tc := range []struct {
4354 name string
4355 sdkVersion string
4356 variant string
4357 expected string
4358 }{
4359 {
4360 name: "default is current",
4361 sdkVersion: "",
4362 variant: "android_arm64_armv8-a_static",
4363 expected: "platform_apis",
4364 },
4365 {
4366 name: "use sdk_version",
4367 sdkVersion: `sdk_version: "29"`,
4368 variant: "android_arm64_armv8-a_static",
4369 expected: "platform_apis",
4370 },
4371 {
4372 name: "use sdk_version(sdk variant)",
4373 sdkVersion: `sdk_version: "29"`,
4374 variant: "android_arm64_armv8-a_sdk_static",
4375 expected: "29",
4376 },
4377 {
4378 name: "use min_sdk_version",
4379 sdkVersion: `min_sdk_version: "29"`,
4380 variant: "android_arm64_armv8-a_static",
4381 expected: "29",
4382 },
4383 } {
4384 t.Run(tc.name, func(t *testing.T) {
4385 ctx := testCc(t, `
4386 cc_library {
4387 name: "libfoo",
4388 stl: "none",
4389 srcs: ["a/Foo.aidl"],
4390 `+tc.sdkVersion+`
4391 }
4392 `)
4393 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4394 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4395 aidlCommand := manifest.Commands[0].GetCommand()
4396 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4397 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4398 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4399 }
4400 })
4401 }
4402}
4403
Vinh Tran09581952023-05-16 16:03:20 -04004404func TestInvalidAidlProp(t *testing.T) {
4405 t.Parallel()
4406
4407 testCases := []struct {
4408 description string
4409 bp string
4410 }{
4411 {
4412 description: "Invalid use of aidl.libs and aidl.include_dirs",
4413 bp: `
4414 cc_library {
4415 name: "foo",
4416 aidl: {
4417 libs: ["foo_aidl"],
4418 include_dirs: ["bar/include"],
4419 }
4420 }
4421 `,
4422 },
4423 {
4424 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4425 bp: `
4426 cc_library {
4427 name: "foo",
4428 aidl: {
4429 libs: ["foo_aidl"],
4430 local_include_dirs: ["include"],
4431 }
4432 }
4433 `,
4434 },
4435 }
4436
4437 for _, testCase := range testCases {
4438 t.Run(testCase.description, func(t *testing.T) {
4439 bp := `
4440 aidl_library {
4441 name: "foo_aidl",
4442 srcs: ["Foo.aidl"],
4443 } ` + testCase.bp
4444 android.GroupFixturePreparers(
4445 prepareForCcTest,
4446 aidl_library.PrepareForTestWithAidlLibrary.
4447 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4448 ).RunTestWithBp(t, bp)
4449 })
4450 }
4451}
4452
Jiyong Parka008fb02021-03-16 17:15:53 +09004453func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004454 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004455 ctx := testCc(t, `
4456 cc_library_shared {
4457 name: "libfoo",
4458 srcs: ["foo.c"],
4459 min_sdk_version: "29",
4460 }`)
4461
4462 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4463 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4464}
4465
Vinh Tranf1924742022-06-24 16:40:11 -04004466func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004467 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004468 bp := `
4469 cc_library_shared {
4470 name: "libfoo",
4471 srcs: ["foo.c"],
4472 min_sdk_version: "S",
4473 }
4474 `
4475 result := android.GroupFixturePreparers(
4476 prepareForCcTest,
4477 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4478 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4479 }),
4480 ).RunTestWithBp(t, bp)
4481 ctx := result.TestContext
4482 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4483 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4484}
4485
Paul Duffin3cb603e2021-02-19 13:57:10 +00004486func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004487 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004488
4489 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4490 // embedded newline characters alone.
4491 trimIndentingSpaces := func(s string) string {
4492 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4493 }
4494
4495 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4496 t.Helper()
4497 expected = trimIndentingSpaces(expected)
4498 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4499 if expected != actual {
4500 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4501 }
4502 }
4503
4504 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4505
4506 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4507 t.Helper()
4508 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4509 name := module.Name()
4510
4511 for _, checker := range checkers {
4512 checker(t, name, exported)
4513 }
4514 }
4515
4516 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4517 return func(t *testing.T, name string, exported FlagExporterInfo) {
4518 t.Helper()
4519 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4520 }
4521 }
4522
4523 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4524 return func(t *testing.T, name string, exported FlagExporterInfo) {
4525 t.Helper()
4526 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4527 }
4528 }
4529
4530 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4531 return func(t *testing.T, name string, exported FlagExporterInfo) {
4532 t.Helper()
4533 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4534 }
4535 }
4536
4537 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4538 return func(t *testing.T, name string, exported FlagExporterInfo) {
4539 t.Helper()
4540 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4541 }
4542 }
4543
4544 genRuleModules := `
4545 genrule {
4546 name: "genrule_foo",
4547 cmd: "generate-foo",
4548 out: [
4549 "generated_headers/foo/generated_header.h",
4550 ],
4551 export_include_dirs: [
4552 "generated_headers",
4553 ],
4554 }
4555
4556 genrule {
4557 name: "genrule_bar",
4558 cmd: "generate-bar",
4559 out: [
4560 "generated_headers/bar/generated_header.h",
4561 ],
4562 export_include_dirs: [
4563 "generated_headers",
4564 ],
4565 }
4566 `
4567
4568 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4569 ctx := testCc(t, genRuleModules+`
4570 cc_library {
4571 name: "libfoo",
4572 srcs: ["foo.c"],
4573 export_include_dirs: ["foo/standard"],
4574 export_system_include_dirs: ["foo/system"],
4575 generated_headers: ["genrule_foo"],
4576 export_generated_headers: ["genrule_foo"],
4577 }
4578
4579 cc_library {
4580 name: "libbar",
4581 srcs: ["bar.c"],
4582 shared_libs: ["libfoo"],
4583 export_include_dirs: ["bar/standard"],
4584 export_system_include_dirs: ["bar/system"],
4585 generated_headers: ["genrule_bar"],
4586 export_generated_headers: ["genrule_bar"],
4587 }
4588 `)
4589 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4590 checkIncludeDirs(t, ctx, foo,
4591 expectedIncludeDirs(`
4592 foo/standard
4593 .intermediates/genrule_foo/gen/generated_headers
4594 `),
4595 expectedSystemIncludeDirs(`foo/system`),
4596 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4597 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4598 )
4599
4600 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4601 checkIncludeDirs(t, ctx, bar,
4602 expectedIncludeDirs(`
4603 bar/standard
4604 .intermediates/genrule_bar/gen/generated_headers
4605 `),
4606 expectedSystemIncludeDirs(`bar/system`),
4607 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4608 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4609 )
4610 })
4611
4612 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4613 ctx := testCc(t, genRuleModules+`
4614 cc_library {
4615 name: "libfoo",
4616 srcs: ["foo.c"],
4617 export_include_dirs: ["foo/standard"],
4618 export_system_include_dirs: ["foo/system"],
4619 generated_headers: ["genrule_foo"],
4620 export_generated_headers: ["genrule_foo"],
4621 }
4622
4623 cc_library {
4624 name: "libbar",
4625 srcs: ["bar.c"],
4626 whole_static_libs: ["libfoo"],
4627 export_include_dirs: ["bar/standard"],
4628 export_system_include_dirs: ["bar/system"],
4629 generated_headers: ["genrule_bar"],
4630 export_generated_headers: ["genrule_bar"],
4631 }
4632 `)
4633 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4634 checkIncludeDirs(t, ctx, foo,
4635 expectedIncludeDirs(`
4636 foo/standard
4637 .intermediates/genrule_foo/gen/generated_headers
4638 `),
4639 expectedSystemIncludeDirs(`foo/system`),
4640 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4641 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4642 )
4643
4644 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4645 checkIncludeDirs(t, ctx, bar,
4646 expectedIncludeDirs(`
4647 bar/standard
4648 foo/standard
4649 .intermediates/genrule_foo/gen/generated_headers
4650 .intermediates/genrule_bar/gen/generated_headers
4651 `),
4652 expectedSystemIncludeDirs(`
4653 bar/system
4654 foo/system
4655 `),
4656 expectedGeneratedHeaders(`
4657 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4658 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4659 `),
4660 expectedOrderOnlyDeps(`
4661 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4662 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4663 `),
4664 )
4665 })
4666
Paul Duffin3cb603e2021-02-19 13:57:10 +00004667 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004668 ctx := android.GroupFixturePreparers(
4669 prepareForCcTest,
4670 aidl_library.PrepareForTestWithAidlLibrary,
4671 ).RunTestWithBp(t, `
4672 aidl_library {
4673 name: "libfoo_aidl",
4674 srcs: ["x/y/Bar.aidl"],
4675 strip_import_prefix: "x",
4676 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004677 cc_library_shared {
4678 name: "libfoo",
4679 srcs: [
4680 "foo.c",
4681 "b.aidl",
4682 "a.proto",
4683 ],
4684 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004685 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004686 export_aidl_headers: true,
4687 }
4688 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004689 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004690 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4691 checkIncludeDirs(t, ctx, foo,
4692 expectedIncludeDirs(`
4693 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004694 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004695 `),
4696 expectedSystemIncludeDirs(``),
4697 expectedGeneratedHeaders(`
4698 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4699 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4700 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004701 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4702 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4703 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004704 `),
4705 expectedOrderOnlyDeps(`
4706 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4707 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4708 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004709 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4710 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4711 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004712 `),
4713 )
4714 })
4715
Paul Duffin3cb603e2021-02-19 13:57:10 +00004716 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4717 ctx := testCc(t, genRuleModules+`
4718 cc_library_shared {
4719 name: "libfoo",
4720 srcs: [
4721 "foo.c",
4722 "b.aidl",
4723 "a.proto",
4724 ],
4725 proto: {
4726 export_proto_headers: true,
4727 }
4728 }
4729 `)
4730 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4731 checkIncludeDirs(t, ctx, foo,
4732 expectedIncludeDirs(`
4733 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4734 `),
4735 expectedSystemIncludeDirs(``),
4736 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004737 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4738 `),
4739 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004740 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4741 `),
4742 )
4743 })
4744
Paul Duffin33056e82021-02-19 13:49:08 +00004745 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004746 ctx := testCc(t, genRuleModules+`
4747 cc_library_shared {
4748 name: "libfoo",
4749 srcs: [
4750 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004751 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004752 "b.aidl",
4753 "a.proto",
4754 ],
4755 }
4756 `)
4757 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4758 checkIncludeDirs(t, ctx, foo,
4759 expectedIncludeDirs(`
4760 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4761 `),
4762 expectedSystemIncludeDirs(``),
4763 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004764 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004765 `),
4766 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004767 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4768 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004769 `),
4770 )
4771 })
4772}
Colin Crossae628182021-06-14 16:52:28 -07004773
4774func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004775 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004776 baseExpectedFlags := []string{
4777 "${config.ArmThumbCflags}",
4778 "${config.ArmCflags}",
4779 "${config.CommonGlobalCflags}",
4780 "${config.DeviceGlobalCflags}",
4781 "${config.ExternalCflags}",
4782 "${config.ArmToolchainCflags}",
4783 "${config.ArmArmv7ANeonCflags}",
4784 "${config.ArmGenericCflags}",
4785 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004786 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004787 }
4788
4789 expectedIncludes := []string{
4790 "external/foo/android_arm_export_include_dirs",
4791 "external/foo/lib32_export_include_dirs",
4792 "external/foo/arm_export_include_dirs",
4793 "external/foo/android_export_include_dirs",
4794 "external/foo/linux_export_include_dirs",
4795 "external/foo/export_include_dirs",
4796 "external/foo/android_arm_local_include_dirs",
4797 "external/foo/lib32_local_include_dirs",
4798 "external/foo/arm_local_include_dirs",
4799 "external/foo/android_local_include_dirs",
4800 "external/foo/linux_local_include_dirs",
4801 "external/foo/local_include_dirs",
4802 "external/foo",
4803 "external/foo/libheader1",
4804 "external/foo/libheader2",
4805 "external/foo/libwhole1",
4806 "external/foo/libwhole2",
4807 "external/foo/libstatic1",
4808 "external/foo/libstatic2",
4809 "external/foo/libshared1",
4810 "external/foo/libshared2",
4811 "external/foo/liblinux",
4812 "external/foo/libandroid",
4813 "external/foo/libarm",
4814 "external/foo/lib32",
4815 "external/foo/libandroid_arm",
4816 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004817 }
4818
4819 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4820 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4821
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004822 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004823 cstd := []string{"-std=gnu17", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004824 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004825
4826 lastIncludes := []string{
4827 "out/soong/ndk/sysroot/usr/include",
4828 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4829 }
4830
4831 combineSlices := func(slices ...[]string) []string {
4832 var ret []string
4833 for _, s := range slices {
4834 ret = append(ret, s...)
4835 }
4836 return ret
4837 }
4838
4839 testCases := []struct {
4840 name string
4841 src string
4842 expected []string
4843 }{
4844 {
4845 name: "c",
4846 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004847 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004848 },
4849 {
4850 name: "cc",
4851 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004852 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004853 },
4854 {
4855 name: "assemble",
4856 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004857 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004858 },
4859 }
4860
4861 for _, tc := range testCases {
4862 t.Run(tc.name, func(t *testing.T) {
4863 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004864 cc_library {
4865 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004866 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004867 cflags: ["-std=candcpp"],
4868 conlyflags: ["-std=conly"],
4869 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004870 local_include_dirs: ["local_include_dirs"],
4871 export_include_dirs: ["export_include_dirs"],
4872 export_system_include_dirs: ["export_system_include_dirs"],
4873 static_libs: ["libstatic1", "libstatic2"],
4874 whole_static_libs: ["libwhole1", "libwhole2"],
4875 shared_libs: ["libshared1", "libshared2"],
4876 header_libs: ["libheader1", "libheader2"],
4877 target: {
4878 android: {
4879 shared_libs: ["libandroid"],
4880 local_include_dirs: ["android_local_include_dirs"],
4881 export_include_dirs: ["android_export_include_dirs"],
4882 },
4883 android_arm: {
4884 shared_libs: ["libandroid_arm"],
4885 local_include_dirs: ["android_arm_local_include_dirs"],
4886 export_include_dirs: ["android_arm_export_include_dirs"],
4887 },
4888 linux: {
4889 shared_libs: ["liblinux"],
4890 local_include_dirs: ["linux_local_include_dirs"],
4891 export_include_dirs: ["linux_export_include_dirs"],
4892 },
4893 },
4894 multilib: {
4895 lib32: {
4896 shared_libs: ["lib32"],
4897 local_include_dirs: ["lib32_local_include_dirs"],
4898 export_include_dirs: ["lib32_export_include_dirs"],
4899 },
4900 },
4901 arch: {
4902 arm: {
4903 shared_libs: ["libarm"],
4904 local_include_dirs: ["arm_local_include_dirs"],
4905 export_include_dirs: ["arm_export_include_dirs"],
4906 },
4907 },
4908 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004909 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004910 }
4911
4912 cc_library_headers {
4913 name: "libheader1",
4914 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004915 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004916 stl: "none",
4917 }
4918
4919 cc_library_headers {
4920 name: "libheader2",
4921 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004922 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004923 stl: "none",
4924 }
Liz Kammer08572c62021-09-30 10:11:04 -04004925 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004926
Liz Kammer08572c62021-09-30 10:11:04 -04004927 libs := []string{
4928 "libstatic1",
4929 "libstatic2",
4930 "libwhole1",
4931 "libwhole2",
4932 "libshared1",
4933 "libshared2",
4934 "libandroid",
4935 "libandroid_arm",
4936 "liblinux",
4937 "lib32",
4938 "libarm",
4939 }
Colin Crossae628182021-06-14 16:52:28 -07004940
Liz Kammer08572c62021-09-30 10:11:04 -04004941 for _, lib := range libs {
4942 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004943 cc_library {
4944 name: "%s",
4945 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004946 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004947 stl: "none",
4948 }
4949 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004950 }
4951
4952 ctx := android.GroupFixturePreparers(
4953 PrepareForIntegrationTestWithCc,
4954 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4955 ).RunTest(t)
4956 // Use the arm variant instead of the arm64 variant so that it gets headers from
4957 // ndk_libandroid_support to test LateStaticLibs.
4958 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4959
4960 var includes []string
4961 flags := strings.Split(cflags, " ")
4962 for _, flag := range flags {
4963 if strings.HasPrefix(flag, "-I") {
4964 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4965 } else if flag == "-isystem" {
4966 // skip isystem, include next
4967 } else if len(flag) > 0 {
4968 includes = append(includes, flag)
4969 }
4970 }
4971
4972 android.AssertArrayString(t, "includes", tc.expected, includes)
4973 })
Colin Crossae628182021-06-14 16:52:28 -07004974 }
4975
Colin Crossae628182021-06-14 16:52:28 -07004976}
Alixb5f6d9e2022-04-20 23:00:58 +00004977
zijunzhao933e3802023-01-12 07:26:20 +00004978func TestAddnoOverride64GlobalCflags(t *testing.T) {
4979 t.Parallel()
4980 ctx := testCc(t, `
4981 cc_library_shared {
4982 name: "libclient",
4983 srcs: ["foo.c"],
4984 shared_libs: ["libfoo#1"],
4985 }
4986
4987 cc_library_shared {
4988 name: "libfoo",
4989 srcs: ["foo.c"],
4990 shared_libs: ["libbar"],
4991 export_shared_lib_headers: ["libbar"],
4992 stubs: {
4993 symbol_file: "foo.map.txt",
4994 versions: ["1", "2", "3"],
4995 },
4996 }
4997
4998 cc_library_shared {
4999 name: "libbar",
5000 export_include_dirs: ["include/libbar"],
5001 srcs: ["foo.c"],
5002 }`)
5003
5004 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
5005
5006 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
5007 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
5008 }
5009}
5010
Alixb5f6d9e2022-04-20 23:00:58 +00005011func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005012 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00005013 tests := []struct {
5014 name string
5015 clang bool
5016 BuildBrokenClangProperty bool
5017 err string
5018 }{
5019 {
5020 name: "error when clang is set to false",
5021 clang: false,
5022 err: "is no longer supported",
5023 },
5024 {
5025 name: "error when clang is set to true",
5026 clang: true,
5027 err: "property is deprecated, see Changes.md",
5028 },
5029 {
5030 name: "no error when BuildBrokenClangProperty is explicitly set to true",
5031 clang: true,
5032 BuildBrokenClangProperty: true,
5033 },
5034 }
5035
5036 for _, test := range tests {
5037 t.Run(test.name, func(t *testing.T) {
5038 bp := fmt.Sprintf(`
5039 cc_library {
5040 name: "foo",
5041 clang: %t,
5042 }`, test.clang)
5043
5044 if test.err == "" {
5045 android.GroupFixturePreparers(
5046 prepareForCcTest,
5047 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5048 if test.BuildBrokenClangProperty {
5049 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
5050 }
5051 }),
5052 ).RunTestWithBp(t, bp)
5053 } else {
5054 prepareForCcTest.
5055 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5056 RunTestWithBp(t, bp)
5057 }
5058 })
5059 }
5060}
Alix Espinoef47e542022-09-14 19:10:51 +00005061
5062func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005063 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005064 tests := []struct {
5065 name string
5066 clangAsFlags []string
5067 BuildBrokenClangAsFlags bool
5068 err string
5069 }{
5070 {
5071 name: "error when clang_asflags is set",
5072 clangAsFlags: []string{"-a", "-b"},
5073 err: "clang_asflags: property is deprecated",
5074 },
5075 {
5076 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
5077 clangAsFlags: []string{"-a", "-b"},
5078 BuildBrokenClangAsFlags: true,
5079 },
5080 }
5081
5082 for _, test := range tests {
5083 t.Run(test.name, func(t *testing.T) {
5084 bp := fmt.Sprintf(`
5085 cc_library {
5086 name: "foo",
5087 clang_asflags: %s,
5088 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
5089
5090 if test.err == "" {
5091 android.GroupFixturePreparers(
5092 prepareForCcTest,
5093 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5094 if test.BuildBrokenClangAsFlags {
5095 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
5096 }
5097 }),
5098 ).RunTestWithBp(t, bp)
5099 } else {
5100 prepareForCcTest.
5101 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5102 RunTestWithBp(t, bp)
5103 }
5104 })
5105 }
5106}
5107
5108func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04005109 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00005110 tests := []struct {
5111 name string
5112 clangCFlags []string
5113 BuildBrokenClangCFlags bool
5114 err string
5115 }{
5116 {
5117 name: "error when clang_cflags is set",
5118 clangCFlags: []string{"-a", "-b"},
5119 err: "clang_cflags: property is deprecated",
5120 },
5121 {
5122 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
5123 clangCFlags: []string{"-a", "-b"},
5124 BuildBrokenClangCFlags: true,
5125 },
5126 }
5127
5128 for _, test := range tests {
5129 t.Run(test.name, func(t *testing.T) {
5130 bp := fmt.Sprintf(`
5131 cc_library {
5132 name: "foo",
5133 clang_cflags: %s,
5134 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
5135
5136 if test.err == "" {
5137 android.GroupFixturePreparers(
5138 prepareForCcTest,
5139 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
5140 if test.BuildBrokenClangCFlags {
5141 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
5142 }
5143 }),
5144 ).RunTestWithBp(t, bp)
5145 } else {
5146 prepareForCcTest.
5147 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
5148 RunTestWithBp(t, bp)
5149 }
5150 })
5151 }
5152}
Yu Liue4312402023-01-18 09:15:31 -08005153
5154func TestDclaLibraryInApex(t *testing.T) {
5155 t.Parallel()
5156 bp := `
5157 cc_library_shared {
5158 name: "cc_lib_in_apex",
5159 srcs: ["foo.cc"],
5160 apex_available: ["myapex"],
5161 bazel_module: { label: "//foo/bar:bar" },
5162 }`
5163 label := "//foo/bar:bar"
5164 arch64 := "arm64_armv8-a"
5165 arch32 := "arm_armv7-a-neon"
5166 apexCfgKey := android.ApexConfigKey{
5167 WithinApex: true,
5168 ApexSdkVersion: "28",
5169 }
5170
5171 result := android.GroupFixturePreparers(
5172 prepareForCcTest,
5173 android.FixtureRegisterWithContext(registerTestMutators),
5174 android.FixtureModifyConfig(func(config android.Config) {
5175 config.BazelContext = android.MockBazelContext{
5176 OutputBaseDir: "outputbase",
5177 LabelToCcInfo: map[string]cquery.CcInfo{
5178 android.BuildMockBazelContextResultKey(label, arch32, android.Android, apexCfgKey): cquery.CcInfo{
5179 RootDynamicLibraries: []string{"foo.so"},
5180 },
5181 android.BuildMockBazelContextResultKey(label, arch64, android.Android, apexCfgKey): cquery.CcInfo{
5182 RootDynamicLibraries: []string{"foo.so"},
5183 },
5184 },
5185 BazelRequests: make(map[string]bool),
5186 }
5187 }),
5188 ).RunTestWithBp(t, bp)
5189 ctx := result.TestContext
5190
5191 // Test if the bazel request is queued correctly
5192 key := android.BuildMockBazelContextRequestKey(label, cquery.GetCcInfo, arch32, android.Android, apexCfgKey)
5193 if !ctx.Config().BazelContext.(android.MockBazelContext).BazelRequests[key] {
5194 t.Errorf("Bazel request was not queued: %s", key)
5195 }
5196
5197 sharedFoo := ctx.ModuleForTests(ccLibInApex, "android_arm_armv7-a-neon_shared_"+apexVariationName).Module()
5198 producer := sharedFoo.(android.OutputFileProducer)
5199 outputFiles, err := producer.OutputFiles("")
5200 if err != nil {
5201 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5202 }
5203 expectedOutputFiles := []string{"outputbase/execroot/__main__/foo.so"}
5204 android.AssertDeepEquals(t, "output files", expectedOutputFiles, outputFiles.Strings())
5205}
Sam Delmericoef69d472023-04-18 17:32:43 -04005206
5207func TestDisableSanitizerVariantsInMixedBuilds(t *testing.T) {
5208 t.Parallel()
5209 bp := `
5210 cc_library_static {
5211 name: "foo_ubsan_minimal",
5212 srcs: ["foo.cc"],
5213 bazel_module: { label: "//foo_ubsan_minimal" },
5214 sanitize: {
5215 all_undefined: true,
5216 integer_overflow: true,
5217 },
5218 }
5219 cc_library_static {
5220 name: "foo",
5221 srcs: ["foo.cc"],
5222 bazel_module: { label: "//foo" },
5223 sanitize: {
5224 address: true,
5225 hwaddress: true,
5226 fuzzer: true,
5227 integer_overflow: true,
5228 scs: true,
5229 },
5230 }
5231 cc_library_static {
5232 name: "foo_tsan",
5233 srcs: ["foo.cc"],
5234 bazel_module: { label: "//foo_tsan" },
5235 sanitize: {
5236 thread: true,
5237 },
5238 }
5239 cc_library_static {
5240 name: "foo_cfi",
5241 srcs: ["foo.cc"],
5242 bazel_module: { label: "//foo_cfi" },
5243 sanitize: {
5244 cfi: true,
5245 },
5246 }
5247 cc_library_static {
5248 name: "foo_memtag_stack",
5249 srcs: ["foo.cc"],
5250 bazel_module: { label: "//foo_memtag_stack" },
5251 sanitize: {
5252 memtag_stack: true,
5253 },
5254 }
5255 cc_library_static {
5256 name: "foo_memtag_heap",
5257 srcs: ["foo.cc"],
5258 bazel_module: { label: "//foo_memtag_heap" },
5259 sanitize: {
5260 memtag_heap: true,
5261 },
5262 }
5263 cc_library_static {
5264 name: "foo_safestack",
5265 srcs: ["foo.cc"],
5266 bazel_module: { label: "//foo_safestack" },
5267 sanitize: {
5268 safestack: true,
5269 },
5270 }
5271 cc_library_static {
5272 name: "foo_scudo",
5273 srcs: ["foo.cc"],
5274 bazel_module: { label: "//foo_scudo" },
5275 sanitize: {
5276 scudo: true,
5277 },
5278 }
5279 `
5280 testcases := []struct {
5281 name string
5282 variant string
5283 expectedOutputPaths []string
5284 }{
5285 {
5286 name: "foo_ubsan_minimal",
5287 variant: "android_arm64_armv8-a_static_apex28",
5288 expectedOutputPaths: []string{
5289 "outputbase/execroot/__main__/foo_ubsan_minimal.a",
5290 },
5291 },
5292 {
5293 name: "foo",
5294 variant: "android_arm64_armv8-a_static_apex28",
5295 expectedOutputPaths: []string{
5296 "outputbase/execroot/__main__/foo.a",
5297 },
5298 },
5299 {
5300 name: "foo",
5301 variant: "android_arm_armv7-a-neon_static_asan_apex28",
5302 expectedOutputPaths: []string{
5303 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_apex28/foo.a",
5304 },
5305 },
5306 {
5307 name: "foo",
5308 variant: "android_arm64_armv8-a_static_hwasan_apex28",
5309 expectedOutputPaths: []string{
5310 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_apex28/foo.a",
5311 },
5312 },
5313 {
5314 name: "foo",
5315 variant: "android_arm64_armv8-a_static_fuzzer_apex28",
5316 expectedOutputPaths: []string{
5317 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_fuzzer_apex28/foo.a",
5318 },
5319 },
5320 {
5321 name: "foo",
5322 variant: "android_arm_armv7-a-neon_static_asan_fuzzer_apex28",
5323 expectedOutputPaths: []string{
5324 "out/soong/.intermediates/foo/android_arm_armv7-a-neon_static_asan_fuzzer_apex28/foo.a",
5325 },
5326 },
5327 {
5328 name: "foo",
5329 variant: "android_arm64_armv8-a_static_hwasan_fuzzer_apex28",
5330 expectedOutputPaths: []string{
5331 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_fuzzer_apex28/foo.a",
5332 },
5333 },
5334 {
5335 name: "foo",
5336 variant: "android_arm64_armv8-a_static_scs_apex28",
5337 expectedOutputPaths: []string{
5338 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_scs_apex28/foo.a",
5339 },
5340 },
5341 {
5342 name: "foo",
5343 variant: "android_arm64_armv8-a_static_hwasan_scs_apex28",
5344 expectedOutputPaths: []string{
5345 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_apex28/foo.a",
5346 },
5347 },
5348 {
5349 name: "foo",
5350 variant: "android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28",
5351 expectedOutputPaths: []string{
5352 "out/soong/.intermediates/foo/android_arm64_armv8-a_static_hwasan_scs_fuzzer_apex28/foo.a",
5353 },
5354 },
5355 {
5356 name: "foo_tsan",
5357 variant: "android_arm64_armv8-a_static_apex28",
5358 expectedOutputPaths: []string{
5359 "outputbase/execroot/__main__/foo_tsan.a",
5360 },
5361 },
5362 {
5363 name: "foo_tsan",
5364 variant: "android_arm64_armv8-a_static_tsan_apex28",
5365 expectedOutputPaths: []string{
5366 "out/soong/.intermediates/foo_tsan/android_arm64_armv8-a_static_tsan_apex28/foo_tsan.a",
5367 },
5368 },
5369 {
5370 name: "foo_cfi",
5371 variant: "android_arm64_armv8-a_static_apex28",
5372 expectedOutputPaths: []string{
5373 "outputbase/execroot/__main__/foo_cfi.a",
5374 },
5375 },
5376 {
5377 name: "foo_cfi",
5378 variant: "android_arm64_armv8-a_static_cfi_apex28",
5379 expectedOutputPaths: []string{
Yu Liu95497dc2023-05-25 11:15:07 -07005380 "outputbase/execroot/__main__/foo_cfi.a",
Sam Delmericoef69d472023-04-18 17:32:43 -04005381 },
5382 },
5383 {
5384 name: "foo_memtag_stack",
5385 variant: "android_arm64_armv8-a_static_apex28",
5386 expectedOutputPaths: []string{
5387 "out/soong/.intermediates/foo_memtag_stack/android_arm64_armv8-a_static_apex28/foo_memtag_stack.a",
5388 },
5389 },
5390 {
5391 name: "foo_memtag_heap",
5392 variant: "android_arm64_armv8-a_static_apex28",
5393 expectedOutputPaths: []string{
5394 "out/soong/.intermediates/foo_memtag_heap/android_arm64_armv8-a_static_apex28/foo_memtag_heap.a",
5395 },
5396 },
5397 {
5398 name: "foo_safestack",
5399 variant: "android_arm64_armv8-a_static_apex28",
5400 expectedOutputPaths: []string{
5401 "out/soong/.intermediates/foo_safestack/android_arm64_armv8-a_static_apex28/foo_safestack.a",
5402 },
5403 },
5404 {
5405 name: "foo_scudo",
5406 variant: "android_arm64_armv8-a_static_apex28",
5407 expectedOutputPaths: []string{
5408 "out/soong/.intermediates/foo_scudo/android_arm64_armv8-a_static_apex28/foo_scudo.a",
5409 },
5410 },
5411 }
5412
5413 ctx := android.GroupFixturePreparers(
5414 prepareForCcTest,
5415 prepareForAsanTest,
5416 android.FixtureRegisterWithContext(registerTestMutators),
5417 android.FixtureModifyConfig(func(config android.Config) {
5418 config.BazelContext = android.MockBazelContext{
5419 OutputBaseDir: "outputbase",
5420 LabelToCcInfo: map[string]cquery.CcInfo{
5421 "//foo_ubsan_minimal": {
5422 RootStaticArchives: []string{"foo_ubsan_minimal.a"},
5423 },
5424 "//foo": {
5425 RootStaticArchives: []string{"foo.a"},
5426 },
5427 "//foo_tsan": {
5428 RootStaticArchives: []string{"foo_tsan.a"},
5429 },
5430 "//foo_cfi": {
5431 RootStaticArchives: []string{"foo_cfi.a"},
5432 },
5433 "//foo_memtag_stack": {
5434 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5435 },
5436 "//foo_memtag_heap": {
5437 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5438 },
5439 "//foo_safestack": {
5440 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5441 },
5442 "//foo_scudo": {
5443 RootStaticArchives: []string{"INVALID_ARCHIVE.a"},
5444 },
5445 },
5446 }
5447 }),
5448 ).RunTestWithBp(t, bp).TestContext
5449
5450 for _, tc := range testcases {
5451 fooMod := ctx.ModuleForTests(tc.name, tc.variant).Module()
5452 outputFiles, err := fooMod.(android.OutputFileProducer).OutputFiles("")
5453 if err != nil {
5454 t.Errorf("Unexpected error getting cc_object outputfiles %s", err)
5455 }
5456 android.AssertPathsRelativeToTopEquals(t, "output files", tc.expectedOutputPaths, outputFiles)
5457 }
5458}