blob: 5c5275ede5ff676589ec3a3a50994014f0156790 [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"
Colin Cross74d1ec02015-04-28 13:30:13 -070029)
30
Yu Liue4312402023-01-18 09:15:31 -080031func init() {
32 registerTestMutators(android.InitRegistrationContext)
33}
34
Jiyong Park6a43f042017-10-12 23:05:00 +090035func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000036 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090037}
38
Paul Duffin2e6f90e2021-03-22 23:20:25 +000039var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000040 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000041 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
Justin Yun41cbb5e2023-11-29 17:58:16 +090042 variables.VendorApiLevel = StringPtr("202404")
Paul Duffin02a3d652021-02-24 18:51:54 +000043 variables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090044 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000045 }),
46)
47
Yu Liue4312402023-01-18 09:15:31 -080048var apexVariationName = "apex28"
49var apexVersion = "28"
50
51func registerTestMutators(ctx android.RegistrationContext) {
52 ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
53 ctx.BottomUp("apex", testApexMutator).Parallel()
Yu Liue4312402023-01-18 09:15:31 -080054 })
55}
56
Yu Liue4312402023-01-18 09:15:31 -080057func testApexMutator(mctx android.BottomUpMutatorContext) {
58 modules := mctx.CreateVariations(apexVariationName)
59 apexInfo := android.ApexInfo{
60 ApexVariationName: apexVariationName,
61 MinSdkVersion: android.ApiLevelForTest(apexVersion),
62 }
63 mctx.SetVariationProvider(modules[0], android.ApexInfoProvider, apexInfo)
64}
65
Paul Duffin8567f222021-03-23 00:02:06 +000066// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000067//
68// See testCc for an explanation as to how to stop using this deprecated method.
69//
70// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080071func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070072 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000073 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000074 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090075}
76
Paul Duffin8567f222021-03-23 00:02:06 +000077// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000078//
Paul Duffin8567f222021-03-23 00:02:06 +000079// 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 +000080// easier to customize the test behavior.
81//
82// 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 +000083// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000084// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
85// that it did not change the test behavior unexpectedly.
86//
87// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080088func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080089 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000090 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000091 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080092}
93
Paul Duffin8567f222021-03-23 00:02:06 +000094// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000095//
96// See testCc for an explanation as to how to stop using this deprecated method.
97//
98// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +090099func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800100 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800101
Paul Duffin8567f222021-03-23 00:02:06 +0000102 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000103 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
104 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800105}
106
Paul Duffin8567f222021-03-23 00:02:06 +0000107// testCcError 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 testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900113 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000114 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900116 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900117 testCcErrorWithConfig(t, pattern, config)
118 return
119}
120
Paul Duffin8567f222021-03-23 00:02:06 +0000121// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000122//
123// See testCc for an explanation as to how to stop using this deprecated method.
124//
125// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900127 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000128 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900130 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900131 testCcErrorWithConfig(t, pattern, config)
132 return
133}
134
Logan Chienf3511742017-10-31 18:04:35 +0800135const (
Colin Cross7113d202019-11-20 16:39:12 -0800136 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900137 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
138 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800139 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800140)
141
Paul Duffindb462dd2021-03-21 22:01:55 +0000142// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
143// running it in a fixture that requires all source files to exist.
144func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
145 android.GroupFixturePreparers(
146 PrepareForTestWithCcDefaultModules,
147 android.PrepareForTestDisallowNonExistentPaths,
148 ).RunTest(t)
149}
150
Jiyong Park6a43f042017-10-12 23:05:00 +0900151func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400152 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900153 ctx := testCc(t, `
154 cc_library {
155 name: "libTest",
156 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700157 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800158 nocrt: true,
159 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900160 vendor_available: true,
161 target: {
162 vendor: {
163 srcs: ["bar.c"],
164 },
165 },
166 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900167 `)
168
Logan Chienf3511742017-10-31 18:04:35 +0800169 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900170 var objs []string
171 for _, o := range ld.Inputs {
172 objs = append(objs, o.Base())
173 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800174 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900175 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
176 }
177}
178
Justin Yun7f99ec72021-04-12 13:19:28 +0900179func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
180 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
181 partitionDefined := false
182 checkPartition := func(specific bool, partition string) {
183 if specific {
184 if expected != partition && !partitionDefined {
185 // The variant is installed to the 'partition'
186 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
187 }
188 partitionDefined = true
189 } else {
190 // The variant is not installed to the 'partition'
191 if expected == partition {
192 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
193 }
194 }
195 }
196 socSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800197 return m.SocSpecific() || m.InstallInVendor()
Justin Yun7f99ec72021-04-12 13:19:28 +0900198 }
199 deviceSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800200 return m.DeviceSpecific() || m.InstallInOdm()
Justin Yun7f99ec72021-04-12 13:19:28 +0900201 }
202 productSpecific := func(m *Module) bool {
Colin Crossea30d852023-11-29 16:00:16 -0800203 return m.ProductSpecific() || m.InstallInProduct()
Justin Yun7f99ec72021-04-12 13:19:28 +0900204 }
205 systemExtSpecific := func(m *Module) bool {
206 return m.SystemExtSpecific()
207 }
208 checkPartition(socSpecific(mod), "vendor")
209 checkPartition(deviceSpecific(mod), "odm")
210 checkPartition(productSpecific(mod), "product")
211 checkPartition(systemExtSpecific(mod), "system_ext")
212 if !partitionDefined && expected != "system" {
213 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
214 " but installed to system partition", variant, name, expected)
215 }
216}
217
218func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400219 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900220 t.Helper()
221 ctx := prepareForCcTest.RunTestWithBp(t, `
222 cc_library {
223 name: "libsystem",
224 }
225 cc_library {
226 name: "libsystem_ext",
227 system_ext_specific: true,
228 }
229 cc_library {
230 name: "libproduct",
231 product_specific: true,
232 }
233 cc_library {
234 name: "libvendor",
235 vendor: true,
236 }
237 cc_library {
238 name: "libodm",
239 device_specific: true,
240 }
241 cc_library {
242 name: "liball_available",
243 vendor_available: true,
244 product_available: true,
245 }
246 cc_library {
247 name: "libsystem_ext_all_available",
248 system_ext_specific: true,
249 vendor_available: true,
250 product_available: true,
251 }
252 cc_library {
253 name: "liball_available_odm",
254 odm_available: true,
255 product_available: true,
256 }
257 cc_library {
258 name: "libproduct_vendoravailable",
259 product_specific: true,
260 vendor_available: true,
261 }
262 cc_library {
263 name: "libproduct_odmavailable",
264 product_specific: true,
265 odm_available: true,
266 }
267 `).TestContext
268
269 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
270 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
271 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
272 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
273 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
274
275 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
276 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
277 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
278
279 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
280 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
281 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
282
283 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
284 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
285 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
286
287 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
288 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
289
290 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
291 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
292}
293
Logan Chienf3511742017-10-31 18:04:35 +0800294func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900295 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800296
Logan Chiend3c59a22018-03-29 14:08:15 +0800297 t.Helper()
298
Justin Yun0ecf0b22020-02-28 15:07:59 +0900299 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800300
301 // Check library properties.
302 lib, ok := mod.compiler.(*libraryDecorator)
303 if !ok {
304 t.Errorf("%q must have libraryDecorator", name)
305 } else if lib.baseInstaller.subDir != subDir {
306 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
307 lib.baseInstaller.subDir)
308 }
309
310 // Check VNDK properties.
311 if mod.vndkdep == nil {
312 t.Fatalf("%q must have `vndkdep`", name)
313 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700314 if !mod.IsVndk() {
315 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800316 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400317 if mod.IsVndkSp() != isVndkSp {
318 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800319 }
320
321 // Check VNDK extension properties.
322 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500323 if mod.IsVndkExt() != isVndkExt {
324 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800325 }
326
327 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
328 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
329 }
330}
331
Colin Crossf61d03d2023-11-02 16:56:39 -0700332func checkWriteFileOutput(t *testing.T, ctx *android.TestContext, params android.TestingBuildParams, expected []string) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900333 t.Helper()
Colin Crossf61d03d2023-11-02 16:56:39 -0700334 content := android.ContentFromFileRuleForTests(t, ctx, params)
Colin Crosscf371cc2020-11-13 11:48:42 -0800335 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900336 assertArrayString(t, actual, expected)
337}
338
Jooyung Han097087b2019-10-22 19:32:18 +0900339func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
340 t.Helper()
341 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Colin Crossf61d03d2023-11-02 16:56:39 -0700342 checkWriteFileOutput(t, ctx, vndkSnapshot.Output(output), expected)
Jooyung Han2216fb12019-11-06 16:46:15 +0900343}
344
345func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
346 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800347 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800348 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900349}
350
Logan Chienf3511742017-10-31 18:04:35 +0800351func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400352 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800353 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800354 cc_library {
355 name: "libvndk",
356 vendor_available: true,
357 vndk: {
358 enabled: true,
359 },
360 nocrt: true,
361 }
362
363 cc_library {
364 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900365 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800366 vndk: {
367 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900368 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800369 },
370 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900371 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800372 }
373
374 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900375 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800376 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900377 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800378 vndk: {
379 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900380 },
381 nocrt: true,
382 target: {
383 vendor: {
384 cflags: ["-DTEST"],
385 },
386 product: {
387 cflags: ["-DTEST"],
388 },
389 },
390 }
391
392 cc_library {
393 name: "libvndk_sp",
394 vendor_available: true,
395 vndk: {
396 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800397 support_system_process: true,
398 },
399 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900400 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800401 }
402
403 cc_library {
404 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900405 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800406 vndk: {
407 enabled: true,
408 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900409 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 },
411 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900412 target: {
413 vendor: {
414 suffix: "-x",
415 },
416 },
Logan Chienf3511742017-10-31 18:04:35 +0800417 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900418
419 cc_library {
420 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900421 vendor_available: true,
422 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900423 vndk: {
424 enabled: true,
425 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900426 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900427 },
428 nocrt: true,
429 target: {
430 vendor: {
431 suffix: "-x",
432 },
433 product: {
434 suffix: "-x",
435 },
436 },
437 }
438
Justin Yun450ae722021-04-16 19:58:18 +0900439 cc_library {
440 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700441 llndk: {
442 symbol_file: "libllndk.map.txt",
443 export_llndk_headers: ["libllndk_headers"],
444 }
Justin Yun450ae722021-04-16 19:58:18 +0900445 }
446
Justin Yun611e8862021-05-24 18:17:33 +0900447 cc_library {
448 name: "libclang_rt.hwasan-llndk",
449 llndk: {
450 symbol_file: "libclang_rt.hwasan.map.txt",
451 }
452 }
453
Colin Cross627280f2021-04-26 16:53:58 -0700454 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900455 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700456 llndk: {
457 llndk_headers: true,
458 },
Justin Yun450ae722021-04-16 19:58:18 +0900459 export_include_dirs: ["include"],
460 }
461
Colin Crosse4e44bc2020-12-28 13:50:21 -0800462 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900463 name: "llndk.libraries.txt",
464 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800465 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900466 name: "vndkcore.libraries.txt",
467 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800468 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900469 name: "vndksp.libraries.txt",
470 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800471 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900472 name: "vndkprivate.libraries.txt",
473 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800474 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900475 name: "vndkproduct.libraries.txt",
476 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800477 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900478 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800479 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900480 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800481 `
482
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000483 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800484 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900485 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800486
487 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800488
Jooyung Han261e1582020-10-20 18:54:21 +0900489 // subdir == "" because VNDK libs are not supposed to be installed separately.
490 // They are installed as part of VNDK APEX instead.
491 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
492 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900493 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900494 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
495 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900496 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900497
Justin Yun6977e8a2020-10-29 18:24:11 +0900498 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
499 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900500
Inseob Kim1f086e22019-05-09 13:29:15 +0900501 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900502 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000503 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900504
505 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
506 "arm64", "armv8-a"))
507 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
508 "arm", "armv7-a-neon"))
509
510 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
511 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900512 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
513
Inseob Kim1f086e22019-05-09 13:29:15 +0900514 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
515 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900516 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900517
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900518 variant := "android_vendor.29_arm64_armv8-a_shared"
519 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900520
Inseob Kim7f283f42020-06-01 21:53:49 +0900521 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
522
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400523 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
524 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
525 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
526 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
527 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
528 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
529 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
530 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900531
Jooyung Han39edb6c2019-11-06 16:53:07 +0900532 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800533 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
534 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
535 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
536 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
537 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900538
Jooyung Han097087b2019-10-22 19:32:18 +0900539 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
540 "LLNDK: libc.so",
541 "LLNDK: libdl.so",
542 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900543 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900544 "LLNDK: libm.so",
545 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900546 "VNDK-SP: libvndk_sp-x.so",
547 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900548 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900549 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900550 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900551 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900552 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900553 "VNDK-private: libvndk-private.so",
554 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900555 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900556 "VNDK-product: libc++.so",
557 "VNDK-product: libvndk_product.so",
558 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900559 })
Justin Yun611e8862021-05-24 18:17:33 +0900560 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 +0900561 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
562 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
563 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 +0900564 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 +0900565 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
566}
567
Yo Chiangbba545e2020-06-09 16:15:37 +0800568func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400569 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800570 ctx := testCc(t, `
571 cc_library {
572 name: "libvndk_host_supported",
573 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900574 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800575 vndk: {
576 enabled: true,
577 },
578 host_supported: true,
579 }
580
581 cc_library {
582 name: "libvndk_host_supported_but_disabled_on_device",
583 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900584 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800585 vndk: {
586 enabled: true,
587 },
588 host_supported: true,
589 enabled: false,
590 target: {
591 host: {
592 enabled: true,
593 }
594 }
595 }
596
Colin Crosse4e44bc2020-12-28 13:50:21 -0800597 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800598 name: "vndkcore.libraries.txt",
599 }
600 `)
601
602 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
603}
604
Jooyung Han2216fb12019-11-06 16:46:15 +0900605func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400606 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800607 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800608 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900609 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800610 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800611 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000612 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800613 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900614 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Kiyoung Kima2d6dee2023-08-11 10:14:43 +0900615 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800616 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900617
Colin Cross45bce852021-11-11 22:47:54 -0800618 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700619 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900620 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900621}
622
623func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400624 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800625 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900626 cc_library {
627 name: "libvndk",
628 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900629 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900630 vndk: {
631 enabled: true,
632 },
633 nocrt: true,
634 }
635
636 cc_library {
637 name: "libvndk_sp",
638 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900639 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900640 vndk: {
641 enabled: true,
642 support_system_process: true,
643 },
644 nocrt: true,
645 }
646
647 cc_library {
648 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900649 vendor_available: true,
650 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900651 vndk: {
652 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900653 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900654 },
655 nocrt: true,
656 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900657
Colin Crosse4e44bc2020-12-28 13:50:21 -0800658 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900659 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800660 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900661 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800662 `
663
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000664 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800665 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900666 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800667 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
Kiyoung Kim03b6cba2023-10-06 14:12:43 +0900668 config.TestProductVariables.KeepVndk = BoolPtr(true)
Colin Cross98be1bb2019-12-13 20:41:13 -0800669
670 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
671
672 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900673
Jooyung Han2216fb12019-11-06 16:46:15 +0900674 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900675}
676
Chris Parsons79d66a52020-06-05 17:26:16 -0400677func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400678 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400679 bp := `
680 cc_test_library {
681 name: "test_lib",
682 srcs: ["test_lib.cpp"],
683 gtest: false,
684 }
685
686 cc_test {
687 name: "main_test",
688 data_libs: ["test_lib"],
689 gtest: false,
690 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400691 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400692
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000693 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400694 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900695 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400696 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
697
698 ctx := testCcWithConfig(t, config)
699 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
700 testBinary := module.(*Module).linker.(*testBinary)
701 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
702 if err != nil {
703 t.Errorf("Expected cc_test to produce output files, error: %s", err)
704 return
705 }
706 if len(outputFiles) != 1 {
707 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
708 return
709 }
710 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800711 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons79d66a52020-06-05 17:26:16 -0400712 return
713 }
714
715 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400716 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400717
718 if !strings.HasSuffix(outputPath, "/main_test") {
719 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
720 return
721 }
722 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
723 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
724 return
725 }
726}
727
Chris Parsons216e10a2020-07-09 17:12:52 -0400728func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400729 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400730 bp := `
731 cc_test_library {
732 name: "test_lib",
733 srcs: ["test_lib.cpp"],
734 relative_install_path: "foo/bar/baz",
735 gtest: false,
736 }
737
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400738 cc_binary {
739 name: "test_bin",
740 relative_install_path: "foo/bar/baz",
741 compile_multilib: "both",
742 }
743
Chris Parsons216e10a2020-07-09 17:12:52 -0400744 cc_test {
745 name: "main_test",
746 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400747 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400748 gtest: false,
749 }
750 `
751
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000752 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400753 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900754 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400755 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
756
757 ctx := testCcWithConfig(t, config)
758 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
759 testBinary := module.(*Module).linker.(*testBinary)
760 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
761 if err != nil {
762 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
763 }
764 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400765 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400766 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400767 if len(testBinary.dataPaths()) != 2 {
Colin Cross7e2e7942023-11-16 12:56:02 -0800768 t.Fatalf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400769 }
770
771 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400772
773 if !strings.HasSuffix(outputPath, "/main_test") {
774 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
775 }
Colin Crossaa255532020-07-03 13:18:24 -0700776 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400777 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
778 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400779 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400780 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400781 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
782 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
783 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
784 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400785}
786
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000787func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400788 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000789 bp := `
790 cc_test {
791 name: "main_test",
792 srcs: ["main_test.cpp"],
793 test_suites: [
794 "suite_1",
795 "suite_2",
796 ],
797 gtest: false,
798 }
799 `
800
801 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
802 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
803
804 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
805 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
806 if len(compatEntries) != 2 {
807 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
808 }
809 if compatEntries[0] != "suite_1" {
810 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
811 " but was '%s'", compatEntries[0])
812 }
813 if compatEntries[1] != "suite_2" {
814 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
815 " but was '%s'", compatEntries[1])
816 }
817}
818
819func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400820 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000821 bp := `
822 cc_test_library {
823 name: "main_test_lib",
824 srcs: ["main_test_lib.cpp"],
825 test_suites: [
826 "suite_1",
827 "suite_2",
828 ],
829 gtest: false,
830 }
831 `
832
833 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
834 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
835
836 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
837 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
838 if len(compatEntries) != 2 {
839 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
840 }
841 if compatEntries[0] != "suite_1" {
842 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
843 " but was '%s'", compatEntries[0])
844 }
845 if compatEntries[1] != "suite_2" {
846 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
847 " but was '%s'", compatEntries[1])
848 }
849}
850
Justin Yun63e9ec72020-10-29 16:49:43 +0900851func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400852 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900853 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900854 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900855 cc_library {
856 name: "libvndk",
857 vndk: {
858 enabled: true,
859 },
860 nocrt: true,
861 }
862 `)
863
Justin Yunc0d8c492021-01-07 17:45:31 +0900864 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900865 cc_library {
866 name: "libvndk",
867 product_available: true,
868 vndk: {
869 enabled: true,
870 },
871 nocrt: true,
872 }
873 `)
874
Justin Yun6977e8a2020-10-29 18:24:11 +0900875 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
876 cc_library {
877 name: "libvndkprop",
878 vendor_available: true,
879 product_available: true,
880 vndk: {
881 enabled: true,
882 },
883 nocrt: true,
884 target: {
885 vendor: {
886 cflags: ["-DTEST",],
887 },
888 },
889 }
890 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900891}
892
Logan Chiend3c59a22018-03-29 14:08:15 +0800893func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400894 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800895 // Check whether an error is emitted when a VNDK lib depends on a system lib.
896 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
897 cc_library {
898 name: "libvndk",
899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900900 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800901 vndk: {
902 enabled: true,
903 },
904 shared_libs: ["libfwk"], // Cause error
905 nocrt: true,
906 }
907
908 cc_library {
909 name: "libfwk",
910 nocrt: true,
911 }
912 `)
913
914 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
915 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
916 cc_library {
917 name: "libvndk",
918 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900919 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800920 vndk: {
921 enabled: true,
922 },
923 shared_libs: ["libvendor"], // Cause error
924 nocrt: true,
925 }
926
927 cc_library {
928 name: "libvendor",
929 vendor: true,
930 nocrt: true,
931 }
932 `)
933
934 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
935 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
936 cc_library {
937 name: "libvndk_sp",
938 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900939 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800940 vndk: {
941 enabled: true,
942 support_system_process: true,
943 },
944 shared_libs: ["libfwk"], // Cause error
945 nocrt: true,
946 }
947
948 cc_library {
949 name: "libfwk",
950 nocrt: true,
951 }
952 `)
953
954 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
955 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
956 cc_library {
957 name: "libvndk_sp",
958 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900959 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800960 vndk: {
961 enabled: true,
962 support_system_process: true,
963 },
964 shared_libs: ["libvendor"], // Cause error
965 nocrt: true,
966 }
967
968 cc_library {
969 name: "libvendor",
970 vendor: true,
971 nocrt: true,
972 }
973 `)
974
975 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
976 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
977 cc_library {
978 name: "libvndk_sp",
979 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900980 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800981 vndk: {
982 enabled: true,
983 support_system_process: true,
984 },
985 shared_libs: ["libvndk"], // Cause error
986 nocrt: true,
987 }
988
989 cc_library {
990 name: "libvndk",
991 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900992 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800993 vndk: {
994 enabled: true,
995 },
996 nocrt: true,
997 }
998 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900999
1000 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1001 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1002 cc_library {
1003 name: "libvndk",
1004 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001005 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001006 vndk: {
1007 enabled: true,
1008 },
1009 shared_libs: ["libnonvndk"],
1010 nocrt: true,
1011 }
1012
1013 cc_library {
1014 name: "libnonvndk",
1015 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001016 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001017 nocrt: true,
1018 }
1019 `)
1020
1021 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1022 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1023 cc_library {
1024 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001025 vendor_available: true,
1026 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001027 vndk: {
1028 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001029 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001030 },
1031 shared_libs: ["libnonvndk"],
1032 nocrt: true,
1033 }
1034
1035 cc_library {
1036 name: "libnonvndk",
1037 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001038 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001039 nocrt: true,
1040 }
1041 `)
1042
1043 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1044 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1045 cc_library {
1046 name: "libvndksp",
1047 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001048 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001049 vndk: {
1050 enabled: true,
1051 support_system_process: true,
1052 },
1053 shared_libs: ["libnonvndk"],
1054 nocrt: true,
1055 }
1056
1057 cc_library {
1058 name: "libnonvndk",
1059 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001060 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001061 nocrt: true,
1062 }
1063 `)
1064
1065 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1066 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1067 cc_library {
1068 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001069 vendor_available: true,
1070 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001071 vndk: {
1072 enabled: true,
1073 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001074 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001075 },
1076 shared_libs: ["libnonvndk"],
1077 nocrt: true,
1078 }
1079
1080 cc_library {
1081 name: "libnonvndk",
1082 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001083 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001084 nocrt: true,
1085 }
1086 `)
1087}
1088
1089func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001090 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001091 // okay to link : LLNDK -> double_loadable VNDK
1092 testCc(t, `
1093 cc_library {
1094 name: "libllndk",
1095 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001096 llndk: {
1097 symbol_file: "libllndk.map.txt",
1098 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001099 }
1100
1101 cc_library {
1102 name: "libdoubleloadable",
1103 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001104 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001105 vndk: {
1106 enabled: true,
1107 },
1108 double_loadable: true,
1109 }
1110 `)
1111 // okay to link : LLNDK -> VNDK-SP
1112 testCc(t, `
1113 cc_library {
1114 name: "libllndk",
1115 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001116 llndk: {
1117 symbol_file: "libllndk.map.txt",
1118 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001119 }
1120
1121 cc_library {
1122 name: "libvndksp",
1123 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001124 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001125 vndk: {
1126 enabled: true,
1127 support_system_process: true,
1128 },
1129 }
1130 `)
1131 // okay to link : double_loadable -> double_loadable
1132 testCc(t, `
1133 cc_library {
1134 name: "libdoubleloadable1",
1135 shared_libs: ["libdoubleloadable2"],
1136 vendor_available: true,
1137 double_loadable: true,
1138 }
1139
1140 cc_library {
1141 name: "libdoubleloadable2",
1142 vendor_available: true,
1143 double_loadable: true,
1144 }
1145 `)
1146 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1147 testCc(t, `
1148 cc_library {
1149 name: "libdoubleloadable",
1150 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001151 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001152 vndk: {
1153 enabled: true,
1154 },
1155 double_loadable: true,
1156 shared_libs: ["libnondoubleloadable"],
1157 }
1158
1159 cc_library {
1160 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001161 vendor_available: true,
1162 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001163 vndk: {
1164 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001165 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001166 },
1167 double_loadable: true,
1168 }
1169 `)
1170 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1171 testCc(t, `
1172 cc_library {
1173 name: "libllndk",
1174 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001175 llndk: {
1176 symbol_file: "libllndk.map.txt",
1177 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001178 }
1179
1180 cc_library {
1181 name: "libcoreonly",
1182 shared_libs: ["libvendoravailable"],
1183 }
1184
1185 // indirect dependency of LLNDK
1186 cc_library {
1187 name: "libvendoravailable",
1188 vendor_available: true,
1189 double_loadable: true,
1190 }
1191 `)
1192}
1193
1194func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001195 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001196 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1197 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1198 cc_library {
1199 name: "libllndk",
1200 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001201 llndk: {
1202 symbol_file: "libllndk.map.txt",
1203 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001204 }
1205
1206 cc_library {
1207 name: "libnondoubleloadable",
1208 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001209 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001210 vndk: {
1211 enabled: true,
1212 },
1213 }
1214 `)
1215
1216 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1217 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1218 cc_library {
1219 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001220 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001222 llndk: {
1223 symbol_file: "libllndk.map.txt",
1224 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001225 }
1226
1227 cc_library {
1228 name: "libnondoubleloadable",
1229 vendor_available: true,
1230 }
1231 `)
1232
Jooyung Hana70f0672019-01-18 15:20:43 +09001233 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1234 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1235 cc_library {
1236 name: "libllndk",
1237 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001238 llndk: {
1239 symbol_file: "libllndk.map.txt",
1240 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001241 }
1242
1243 cc_library {
1244 name: "libcoreonly",
1245 shared_libs: ["libvendoravailable"],
1246 }
1247
1248 // indirect dependency of LLNDK
1249 cc_library {
1250 name: "libvendoravailable",
1251 vendor_available: true,
1252 }
1253 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001254
1255 // The error is not from 'client' but from 'libllndk'
1256 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1257 cc_library {
1258 name: "client",
1259 vendor_available: true,
1260 double_loadable: true,
1261 shared_libs: ["libllndk"],
1262 }
1263 cc_library {
1264 name: "libllndk",
1265 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001266 llndk: {
1267 symbol_file: "libllndk.map.txt",
1268 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001269 }
1270 cc_library {
1271 name: "libnondoubleloadable",
1272 vendor_available: true,
1273 }
1274 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001275}
1276
Jooyung Han479ca172020-10-19 18:51:07 +09001277func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001278 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001279 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1280 cc_library {
1281 name: "libvndksp",
1282 shared_libs: ["libanothervndksp"],
1283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001284 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001285 vndk: {
1286 enabled: true,
1287 support_system_process: true,
1288 }
1289 }
1290
1291 cc_library {
1292 name: "libllndk",
1293 shared_libs: ["libanothervndksp"],
1294 }
1295
Jooyung Han479ca172020-10-19 18:51:07 +09001296 cc_library {
1297 name: "libanothervndksp",
1298 vendor_available: true,
Justin Yunaf1fde42023-09-27 16:22:10 +09001299 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001300 }
1301 `)
1302}
1303
Logan Chienf3511742017-10-31 18:04:35 +08001304func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001305 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001306 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001307 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001308 cc_library {
1309 name: "libvndk",
1310 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001311 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001312 vndk: {
1313 enabled: true,
1314 },
1315 nocrt: true,
1316 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001317 cc_library {
1318 name: "libvndk2",
1319 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001320 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001321 vndk: {
1322 enabled: true,
1323 },
1324 target: {
1325 vendor: {
1326 suffix: "-suffix",
1327 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001328 product: {
1329 suffix: "-suffix",
1330 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001331 },
1332 nocrt: true,
1333 }
Logan Chienf3511742017-10-31 18:04:35 +08001334
1335 cc_library {
1336 name: "libvndk_ext",
1337 vendor: true,
1338 vndk: {
1339 enabled: true,
1340 extends: "libvndk",
1341 },
1342 nocrt: true,
1343 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001344
1345 cc_library {
1346 name: "libvndk2_ext",
1347 vendor: true,
1348 vndk: {
1349 enabled: true,
1350 extends: "libvndk2",
1351 },
1352 nocrt: true,
1353 }
Logan Chienf3511742017-10-31 18:04:35 +08001354
Justin Yun0ecf0b22020-02-28 15:07:59 +09001355 cc_library {
1356 name: "libvndk_ext_product",
1357 product_specific: true,
1358 vndk: {
1359 enabled: true,
1360 extends: "libvndk",
1361 },
1362 nocrt: true,
1363 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001364
Justin Yun0ecf0b22020-02-28 15:07:59 +09001365 cc_library {
1366 name: "libvndk2_ext_product",
1367 product_specific: true,
1368 vndk: {
1369 enabled: true,
1370 extends: "libvndk2",
1371 },
1372 nocrt: true,
1373 }
1374 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001375 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001376 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001377 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001378
1379 ctx := testCcWithConfig(t, config)
1380
1381 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1382 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1383
1384 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1385 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1386
1387 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1388 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001389}
1390
Logan Chienf3511742017-10-31 18:04:35 +08001391func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001392 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001393 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001394 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001395 cc_library {
1396 name: "libvndk",
1397 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001398 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001399 vndk: {
1400 enabled: true,
1401 },
1402 nocrt: true,
1403 }
1404
1405 cc_library {
1406 name: "libvndk_ext",
1407 vndk: {
1408 enabled: true,
1409 extends: "libvndk",
1410 },
1411 nocrt: true,
1412 }
1413 `)
1414
1415 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1416 cc_library {
1417 name: "libvndk",
1418 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001419 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001420 vndk: {
1421 enabled: true,
1422 },
1423 nocrt: true,
1424 }
1425
1426 cc_library {
1427 name: "libvndk_ext",
1428 vendor: true,
1429 vndk: {
1430 enabled: true,
1431 },
1432 nocrt: true,
1433 }
1434 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001435
1436 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1437 cc_library {
1438 name: "libvndk",
1439 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001440 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001441 vndk: {
1442 enabled: true,
1443 },
1444 nocrt: true,
1445 }
1446
1447 cc_library {
1448 name: "libvndk_ext_product",
1449 product_specific: true,
1450 vndk: {
1451 enabled: true,
1452 },
1453 nocrt: true,
1454 }
1455 `)
1456
1457 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1458 cc_library {
1459 name: "libvndk",
1460 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001461 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001462 vndk: {
1463 enabled: true,
1464 },
1465 nocrt: true,
1466 }
1467
1468 cc_library {
1469 name: "libvndk_ext_product",
1470 product_specific: true,
1471 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001472 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001473 vndk: {
1474 enabled: true,
1475 extends: "libvndk",
1476 },
1477 nocrt: true,
1478 }
1479 `)
Logan Chienf3511742017-10-31 18:04:35 +08001480}
1481
1482func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001483 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001484 // This test ensures an error is emitted for inconsistent support_system_process.
1485 testCcError(t, "module \".*\" with mismatched support_system_process", `
1486 cc_library {
1487 name: "libvndk",
1488 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001489 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001490 vndk: {
1491 enabled: true,
1492 },
1493 nocrt: true,
1494 }
1495
1496 cc_library {
1497 name: "libvndk_sp_ext",
1498 vendor: true,
1499 vndk: {
1500 enabled: true,
1501 extends: "libvndk",
1502 support_system_process: true,
1503 },
1504 nocrt: true,
1505 }
1506 `)
1507
1508 testCcError(t, "module \".*\" with mismatched support_system_process", `
1509 cc_library {
1510 name: "libvndk_sp",
1511 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001512 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001513 vndk: {
1514 enabled: true,
1515 support_system_process: true,
1516 },
1517 nocrt: true,
1518 }
1519
1520 cc_library {
1521 name: "libvndk_ext",
1522 vendor: true,
1523 vndk: {
1524 enabled: true,
1525 extends: "libvndk_sp",
1526 },
1527 nocrt: true,
1528 }
1529 `)
1530}
1531
1532func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001533 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001534 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001535 // with `private: true`.
1536 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001537 cc_library {
1538 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001539 vendor_available: true,
1540 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001541 vndk: {
1542 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001543 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001544 },
1545 nocrt: true,
1546 }
1547
1548 cc_library {
1549 name: "libvndk_ext",
1550 vendor: true,
1551 vndk: {
1552 enabled: true,
1553 extends: "libvndk",
1554 },
1555 nocrt: true,
1556 }
1557 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001558
Justin Yunfd9e8042020-12-23 18:23:14 +09001559 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001560 cc_library {
1561 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001562 vendor_available: true,
1563 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001564 vndk: {
1565 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001566 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001567 },
1568 nocrt: true,
1569 }
1570
1571 cc_library {
1572 name: "libvndk_ext_product",
1573 product_specific: true,
1574 vndk: {
1575 enabled: true,
1576 extends: "libvndk",
1577 },
1578 nocrt: true,
1579 }
1580 `)
Logan Chienf3511742017-10-31 18:04:35 +08001581}
1582
Logan Chiend3c59a22018-03-29 14:08:15 +08001583func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001584 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001585 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001586 testCc(t, `
1587 cc_library {
1588 name: "libvndk",
1589 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001590 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001591 vndk: {
1592 enabled: true,
1593 },
1594 nocrt: true,
1595 }
1596
1597 cc_library {
1598 name: "libvndk_ext",
1599 vendor: true,
1600 vndk: {
1601 enabled: true,
1602 extends: "libvndk",
1603 },
1604 nocrt: true,
1605 }
1606
1607 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001608 name: "libvndk_sp",
1609 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001610 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001611 vndk: {
1612 enabled: true,
1613 support_system_process: true,
1614 },
1615 nocrt: true,
1616 }
1617
1618 cc_library {
1619 name: "libvndk_sp_ext",
1620 vendor: true,
1621 vndk: {
1622 enabled: true,
1623 extends: "libvndk_sp",
1624 support_system_process: true,
1625 },
1626 nocrt: true,
1627 }
1628
1629 cc_library {
1630 name: "libvendor",
1631 vendor: true,
1632 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1633 nocrt: true,
1634 }
1635 `)
1636}
1637
Logan Chiend3c59a22018-03-29 14:08:15 +08001638func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001639 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001640 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001641 testCc(t, `
1642 cc_library {
1643 name: "libvndk",
1644 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001645 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001646 vndk: {
1647 enabled: true,
1648 },
1649 nocrt: true,
1650 }
1651
1652 cc_library {
1653 name: "libvndk_ext",
1654 vendor: true,
1655 vndk: {
1656 enabled: true,
1657 extends: "libvndk",
1658 },
1659 shared_libs: ["libvendor"],
1660 nocrt: true,
1661 }
1662
1663 cc_library {
1664 name: "libvendor",
1665 vendor: true,
1666 nocrt: true,
1667 }
1668 `)
Logan Chienf3511742017-10-31 18:04:35 +08001669
Logan Chiend3c59a22018-03-29 14:08:15 +08001670 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1671 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001672 cc_library {
1673 name: "libvndk_sp",
1674 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001675 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001676 vndk: {
1677 enabled: true,
1678 support_system_process: true,
1679 },
1680 nocrt: true,
1681 }
1682
1683 cc_library {
1684 name: "libvndk_sp_ext",
1685 vendor: true,
1686 vndk: {
1687 enabled: true,
1688 extends: "libvndk_sp",
1689 support_system_process: true,
1690 },
1691 shared_libs: ["libvendor"], // Cause an error
1692 nocrt: true,
1693 }
1694
1695 cc_library {
1696 name: "libvendor",
1697 vendor: true,
1698 nocrt: true,
1699 }
1700 `)
1701}
1702
Justin Yun0ecf0b22020-02-28 15:07:59 +09001703func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001704 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001705 bp := `
1706 cc_library {
1707 name: "libvndk",
1708 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001709 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001710 vndk: {
1711 enabled: true,
1712 },
1713 nocrt: true,
1714 }
1715
1716 cc_library {
1717 name: "libvndk_ext_product",
1718 product_specific: true,
1719 vndk: {
1720 enabled: true,
1721 extends: "libvndk",
1722 },
1723 shared_libs: ["libproduct_for_vndklibs"],
1724 nocrt: true,
1725 }
1726
1727 cc_library {
1728 name: "libvndk_sp",
1729 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001730 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001731 vndk: {
1732 enabled: true,
1733 support_system_process: true,
1734 },
1735 nocrt: true,
1736 }
1737
1738 cc_library {
1739 name: "libvndk_sp_ext_product",
1740 product_specific: true,
1741 vndk: {
1742 enabled: true,
1743 extends: "libvndk_sp",
1744 support_system_process: true,
1745 },
1746 shared_libs: ["libproduct_for_vndklibs"],
1747 nocrt: true,
1748 }
1749
1750 cc_library {
1751 name: "libproduct",
1752 product_specific: true,
1753 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1754 nocrt: true,
1755 }
1756
1757 cc_library {
1758 name: "libproduct_for_vndklibs",
1759 product_specific: true,
1760 nocrt: true,
1761 }
1762 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001763 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001764 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001765 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001766
1767 testCcWithConfig(t, config)
1768}
1769
Logan Chiend3c59a22018-03-29 14:08:15 +08001770func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001771 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001772 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1773 // library.
1774 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1775 cc_library {
1776 name: "libvndk",
1777 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001778 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001779 vndk: {
1780 enabled: true,
1781 },
1782 nocrt: true,
1783 }
1784
1785 cc_library {
1786 name: "libvndk_sp",
1787 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001788 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001789 vndk: {
1790 enabled: true,
1791 support_system_process: true,
1792 },
1793 nocrt: true,
1794 }
1795
1796 cc_library {
1797 name: "libvndk_sp_ext",
1798 vendor: true,
1799 vndk: {
1800 enabled: true,
1801 extends: "libvndk_sp",
1802 support_system_process: true,
1803 },
1804 shared_libs: ["libvndk"], // Cause an error
1805 nocrt: true,
1806 }
1807 `)
1808
1809 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1810 // library.
1811 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1812 cc_library {
1813 name: "libvndk",
1814 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001815 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001816 vndk: {
1817 enabled: true,
1818 },
1819 nocrt: true,
1820 }
1821
1822 cc_library {
1823 name: "libvndk_ext",
1824 vendor: true,
1825 vndk: {
1826 enabled: true,
1827 extends: "libvndk",
1828 },
1829 nocrt: true,
1830 }
1831
1832 cc_library {
1833 name: "libvndk_sp",
1834 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001835 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001836 vndk: {
1837 enabled: true,
1838 support_system_process: true,
1839 },
1840 nocrt: true,
1841 }
1842
1843 cc_library {
1844 name: "libvndk_sp_ext",
1845 vendor: true,
1846 vndk: {
1847 enabled: true,
1848 extends: "libvndk_sp",
1849 support_system_process: true,
1850 },
1851 shared_libs: ["libvndk_ext"], // Cause an error
1852 nocrt: true,
1853 }
1854 `)
1855}
1856
1857func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001858 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001859 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1860 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001861 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1862 cc_library {
1863 name: "libvndk",
1864 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001865 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001866 vndk: {
1867 enabled: true,
1868 },
1869 nocrt: true,
1870 }
1871
1872 cc_library {
1873 name: "libvndk_ext",
1874 vendor: true,
1875 vndk: {
1876 enabled: true,
1877 extends: "libvndk",
1878 },
1879 nocrt: true,
1880 }
1881
1882 cc_library {
1883 name: "libvndk2",
1884 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001885 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001886 vndk: {
1887 enabled: true,
1888 },
1889 shared_libs: ["libvndk_ext"],
1890 nocrt: true,
1891 }
1892 `)
1893
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001894 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001895 cc_library {
1896 name: "libvndk",
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 nocrt: true,
1903 }
1904
1905 cc_library {
1906 name: "libvndk_ext",
1907 vendor: true,
1908 vndk: {
1909 enabled: true,
1910 extends: "libvndk",
1911 },
1912 nocrt: true,
1913 }
1914
1915 cc_library {
1916 name: "libvndk2",
1917 vendor_available: true,
1918 vndk: {
1919 enabled: true,
1920 },
1921 target: {
1922 vendor: {
1923 shared_libs: ["libvndk_ext"],
1924 },
1925 },
1926 nocrt: true,
1927 }
1928 `)
1929
1930 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1931 cc_library {
1932 name: "libvndk_sp",
1933 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001934 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001935 vndk: {
1936 enabled: true,
1937 support_system_process: true,
1938 },
1939 nocrt: true,
1940 }
1941
1942 cc_library {
1943 name: "libvndk_sp_ext",
1944 vendor: true,
1945 vndk: {
1946 enabled: true,
1947 extends: "libvndk_sp",
1948 support_system_process: true,
1949 },
1950 nocrt: true,
1951 }
1952
1953 cc_library {
1954 name: "libvndk_sp_2",
1955 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001956 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001957 vndk: {
1958 enabled: true,
1959 support_system_process: true,
1960 },
1961 shared_libs: ["libvndk_sp_ext"],
1962 nocrt: true,
1963 }
1964 `)
1965
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001966 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001967 cc_library {
1968 name: "libvndk_sp",
1969 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001970 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001971 vndk: {
1972 enabled: true,
1973 },
1974 nocrt: true,
1975 }
1976
1977 cc_library {
1978 name: "libvndk_sp_ext",
1979 vendor: true,
1980 vndk: {
1981 enabled: true,
1982 extends: "libvndk_sp",
1983 },
1984 nocrt: true,
1985 }
1986
1987 cc_library {
1988 name: "libvndk_sp2",
1989 vendor_available: true,
1990 vndk: {
1991 enabled: true,
1992 },
1993 target: {
1994 vendor: {
1995 shared_libs: ["libvndk_sp_ext"],
1996 },
1997 },
1998 nocrt: true,
1999 }
2000 `)
2001}
2002
Justin Yun5f7f7e82019-11-18 19:52:14 +09002003func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002004 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002005 bp := `
2006 cc_library {
2007 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002008 llndk: {
2009 symbol_file: "libllndk.map.txt",
2010 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002011 }
2012 cc_library {
2013 name: "libvndk",
2014 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002015 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002016 vndk: {
2017 enabled: true,
2018 },
2019 nocrt: true,
2020 }
2021 cc_library {
2022 name: "libvndk_sp",
2023 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002024 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002025 vndk: {
2026 enabled: true,
2027 support_system_process: true,
2028 },
2029 nocrt: true,
2030 }
2031 cc_library {
2032 name: "libva",
2033 vendor_available: true,
2034 nocrt: true,
2035 }
2036 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002037 name: "libpa",
2038 product_available: true,
2039 nocrt: true,
2040 }
2041 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002042 name: "libboth_available",
2043 vendor_available: true,
2044 product_available: true,
2045 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002046 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002047 target: {
2048 vendor: {
2049 suffix: "-vendor",
2050 },
2051 product: {
2052 suffix: "-product",
2053 },
2054 }
2055 }
2056 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002057 name: "libproduct_va",
2058 product_specific: true,
2059 vendor_available: true,
2060 nocrt: true,
2061 }
2062 cc_library {
2063 name: "libprod",
2064 product_specific: true,
2065 shared_libs: [
2066 "libllndk",
2067 "libvndk",
2068 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002069 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002070 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002071 "libproduct_va",
2072 ],
2073 nocrt: true,
2074 }
2075 cc_library {
2076 name: "libvendor",
2077 vendor: true,
2078 shared_libs: [
2079 "libllndk",
2080 "libvndk",
2081 "libvndk_sp",
2082 "libva",
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 `
2089
Paul Duffin8567f222021-03-23 00:02:06 +00002090 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002091
Jooyung Han261e1582020-10-20 18:54:21 +09002092 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2093 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002094
2095 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2096 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2097
2098 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2099 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002100
2101 ensureStringContains := func(t *testing.T, str string, substr string) {
2102 t.Helper()
2103 if !strings.Contains(str, substr) {
2104 t.Errorf("%q is not found in %v", substr, str)
2105 }
2106 }
2107 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2108 t.Helper()
2109 if strings.Contains(str, substr) {
2110 t.Errorf("%q is found in %v", substr, str)
2111 }
2112 }
2113
2114 // _static variant is used since _shared reuses *.o from the static variant
2115 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2116 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2117
2118 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2119 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2120 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2121 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002122 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun13decfb2021-03-08 19:25:55 +09002123
2124 product_cflags := product_static.Rule("cc").Args["cFlags"]
2125 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2126 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2127 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun41cbb5e2023-11-29 17:58:16 +09002128 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR_API__=202404")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002129}
2130
2131func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002132 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002133 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002134 cc_library {
2135 name: "libprod",
2136 product_specific: true,
2137 shared_libs: [
2138 "libvendor",
2139 ],
2140 nocrt: true,
2141 }
2142 cc_library {
2143 name: "libvendor",
2144 vendor: true,
2145 nocrt: true,
2146 }
2147 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002148 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002149 cc_library {
2150 name: "libprod",
2151 product_specific: true,
2152 shared_libs: [
2153 "libsystem",
2154 ],
2155 nocrt: true,
2156 }
2157 cc_library {
2158 name: "libsystem",
2159 nocrt: true,
2160 }
2161 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002162 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002163 cc_library {
2164 name: "libprod",
2165 product_specific: true,
2166 shared_libs: [
2167 "libva",
2168 ],
2169 nocrt: true,
2170 }
2171 cc_library {
2172 name: "libva",
2173 vendor_available: true,
2174 nocrt: true,
2175 }
2176 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002177 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002178 cc_library {
2179 name: "libprod",
2180 product_specific: true,
2181 shared_libs: [
2182 "libvndk_private",
2183 ],
2184 nocrt: true,
2185 }
2186 cc_library {
2187 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002188 vendor_available: true,
2189 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002190 vndk: {
2191 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002192 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002193 },
2194 nocrt: true,
2195 }
2196 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002197 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002198 cc_library {
2199 name: "libprod",
2200 product_specific: true,
2201 shared_libs: [
2202 "libsystem_ext",
2203 ],
2204 nocrt: true,
2205 }
2206 cc_library {
2207 name: "libsystem_ext",
2208 system_ext_specific: true,
2209 nocrt: true,
2210 }
2211 `)
2212 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2213 cc_library {
2214 name: "libsystem",
2215 shared_libs: [
2216 "libproduct_va",
2217 ],
2218 nocrt: true,
2219 }
2220 cc_library {
2221 name: "libproduct_va",
2222 product_specific: true,
2223 vendor_available: true,
2224 nocrt: true,
2225 }
2226 `)
2227}
2228
Jooyung Han38002912019-05-16 04:01:54 +09002229func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002230 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002231 bp := `
2232 cc_library {
2233 name: "libvndk",
2234 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002235 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002236 vndk: {
2237 enabled: true,
2238 },
2239 }
2240 cc_library {
2241 name: "libvndksp",
2242 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002243 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002244 vndk: {
2245 enabled: true,
2246 support_system_process: true,
2247 },
2248 }
2249 cc_library {
2250 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002251 vendor_available: true,
2252 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002253 vndk: {
2254 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002255 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002256 },
2257 }
2258 cc_library {
2259 name: "libvendor",
2260 vendor: true,
2261 }
2262 cc_library {
2263 name: "libvndkext",
2264 vendor: true,
2265 vndk: {
2266 enabled: true,
2267 extends: "libvndk",
2268 },
2269 }
2270 vndk_prebuilt_shared {
2271 name: "prevndk",
2272 version: "27",
2273 target_arch: "arm",
2274 binder32bit: true,
2275 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002276 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002277 vndk: {
2278 enabled: true,
2279 },
2280 arch: {
2281 arm: {
2282 srcs: ["liba.so"],
2283 },
2284 },
2285 }
2286 cc_library {
2287 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002288 llndk: {
2289 symbol_file: "libllndk.map.txt",
2290 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002291 }
2292 cc_library {
2293 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002294 llndk: {
2295 symbol_file: "libllndkprivate.map.txt",
2296 private: true,
2297 }
Colin Cross78212242021-01-06 14:51:30 -08002298 }
2299
2300 llndk_libraries_txt {
2301 name: "llndk.libraries.txt",
2302 }
2303 vndkcore_libraries_txt {
2304 name: "vndkcore.libraries.txt",
2305 }
2306 vndksp_libraries_txt {
2307 name: "vndksp.libraries.txt",
2308 }
2309 vndkprivate_libraries_txt {
2310 name: "vndkprivate.libraries.txt",
2311 }
2312 vndkcorevariant_libraries_txt {
2313 name: "vndkcorevariant.libraries.txt",
2314 insert_vndk_version: false,
2315 }
2316 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002317
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002318 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002319 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002320 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002321 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002322 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002323
Colin Cross78212242021-01-06 14:51:30 -08002324 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2325 []string{"libvndk.so", "libvndkprivate.so"})
2326 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2327 []string{"libc++.so", "libvndksp.so"})
2328 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2329 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2330 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2331 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002332
Colin Crossfb0c16e2019-11-20 17:12:35 -08002333 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002334
Jooyung Han38002912019-05-16 04:01:54 +09002335 tests := []struct {
2336 variant string
2337 name string
2338 expected string
2339 }{
2340 {vendorVariant, "libvndk", "native:vndk"},
2341 {vendorVariant, "libvndksp", "native:vndk"},
2342 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2343 {vendorVariant, "libvendor", "native:vendor"},
2344 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002345 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002346 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002347 {coreVariant, "libvndk", "native:platform"},
2348 {coreVariant, "libvndkprivate", "native:platform"},
2349 {coreVariant, "libllndk", "native:platform"},
2350 }
2351 for _, test := range tests {
2352 t.Run(test.name, func(t *testing.T) {
2353 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2354 assertString(t, module.makeLinkType, test.expected)
2355 })
2356 }
2357}
2358
Jeff Gaston294356f2017-09-27 17:05:30 -07002359var staticLinkDepOrderTestCases = []struct {
2360 // This is a string representation of a map[moduleName][]moduleDependency .
2361 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002362 inStatic string
2363
2364 // This is a string representation of a map[moduleName][]moduleDependency .
2365 // It models the dependencies declared in an Android.bp file.
2366 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002367
2368 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2369 // The keys of allOrdered specify which modules we would like to check.
2370 // The values of allOrdered specify the expected result (of the transitive closure of all
2371 // dependencies) for each module to test
2372 allOrdered string
2373
2374 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2375 // The keys of outOrdered specify which modules we would like to check.
2376 // The values of outOrdered specify the expected result (of the ordered linker command line)
2377 // for each module to test.
2378 outOrdered string
2379}{
2380 // Simple tests
2381 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002382 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002383 outOrdered: "",
2384 },
2385 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002386 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002387 outOrdered: "a:",
2388 },
2389 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002390 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002391 outOrdered: "a:b; b:",
2392 },
2393 // Tests of reordering
2394 {
2395 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002396 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002397 outOrdered: "a:b,c,d; b:d; c:d; d:",
2398 },
2399 {
2400 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002401 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002402 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2403 },
2404 {
2405 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002406 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002407 outOrdered: "a:d,b,e,c; d:b; e:c",
2408 },
2409 {
2410 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002411 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002412 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2413 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2414 },
2415 {
2416 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002417 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 -07002418 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2419 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2420 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002421 // shared dependencies
2422 {
2423 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2424 // So, we don't actually have to check that a shared dependency of c will change the order
2425 // of a library that depends statically on b and on c. We only need to check that if c has
2426 // a shared dependency on b, that that shows up in allOrdered.
2427 inShared: "c:b",
2428 allOrdered: "c:b",
2429 outOrdered: "c:",
2430 },
2431 {
2432 // This test doesn't actually include any shared dependencies but it's a reminder of what
2433 // the second phase of the above test would look like
2434 inStatic: "a:b,c; c:b",
2435 allOrdered: "a:c,b; c:b",
2436 outOrdered: "a:c,b; c:b",
2437 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002438 // tiebreakers for when two modules specifying different orderings and there is no dependency
2439 // to dictate an order
2440 {
2441 // 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 -08002442 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002443 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2444 },
2445 {
2446 // 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 -08002447 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 -07002448 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2449 },
2450 // Tests involving duplicate dependencies
2451 {
2452 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002453 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002454 outOrdered: "a:c,b",
2455 },
2456 {
2457 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002458 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002459 outOrdered: "a:d,c,b",
2460 },
2461 // Tests to confirm the nonexistence of infinite loops.
2462 // These cases should never happen, so as long as the test terminates and the
2463 // result is deterministic then that should be fine.
2464 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002465 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002466 outOrdered: "a:a",
2467 },
2468 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002469 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002470 allOrdered: "a:b,c; b:c,a; c:a,b",
2471 outOrdered: "a:b; b:c; c:a",
2472 },
2473 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002474 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002475 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2476 outOrdered: "a:c,b; b:a,c; c:b,a",
2477 },
2478}
2479
2480// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2481func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2482 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2483 strippedText := strings.Replace(text, " ", "", -1)
2484 if len(strippedText) < 1 {
2485 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2486 }
2487 allDeps = make(map[android.Path][]android.Path, 0)
2488
2489 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2490 moduleTexts := strings.Split(strippedText, ";")
2491
2492 outputForModuleName := func(moduleName string) android.Path {
2493 return android.PathForTesting(moduleName)
2494 }
2495
2496 for _, moduleText := range moduleTexts {
2497 // convert from "a:b,c" to ["a", "b,c"]
2498 components := strings.Split(moduleText, ":")
2499 if len(components) != 2 {
2500 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2501 }
2502 moduleName := components[0]
2503 moduleOutput := outputForModuleName(moduleName)
2504 modulesInOrder = append(modulesInOrder, moduleOutput)
2505
2506 depString := components[1]
2507 // convert from "b,c" to ["b", "c"]
2508 depNames := strings.Split(depString, ",")
2509 if len(depString) < 1 {
2510 depNames = []string{}
2511 }
2512 var deps []android.Path
2513 for _, depName := range depNames {
2514 deps = append(deps, outputForModuleName(depName))
2515 }
2516 allDeps[moduleOutput] = deps
2517 }
2518 return modulesInOrder, allDeps
2519}
2520
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002521func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002522 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002523 ctx := testCc(t, `
2524 cc_library {
2525 name: "a",
2526 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002527 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002528 }
2529 cc_library {
2530 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002531 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 }
2533 cc_library {
2534 name: "c",
2535 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002536 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002537 }
2538 cc_library {
2539 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002540 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002541 }
2542
2543 `)
2544
Colin Cross7113d202019-11-20 16:39:12 -08002545 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002546 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross5a377182023-12-14 14:46:23 -08002547 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2548 actual := android.Paths(staticLibInfo.TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002549 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002550
2551 if !reflect.DeepEqual(actual, expected) {
2552 t.Errorf("staticDeps orderings were not propagated correctly"+
2553 "\nactual: %v"+
2554 "\nexpected: %v",
2555 actual,
2556 expected,
2557 )
2558 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002559}
Jeff Gaston294356f2017-09-27 17:05:30 -07002560
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002561func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002562 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002563 ctx := testCc(t, `
2564 cc_library {
2565 name: "a",
2566 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002567 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002568 }
2569 cc_library {
2570 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002571 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002572 }
2573 cc_library {
2574 name: "c",
2575 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002576 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002577 }
2578
2579 `)
2580
Colin Cross7113d202019-11-20 16:39:12 -08002581 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002582 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross5a377182023-12-14 14:46:23 -08002583 staticLibInfo, _ := android.SingletonModuleProvider(ctx, moduleA, StaticLibraryInfoProvider)
2584 actual := android.Paths(staticLibInfo.TransitiveStaticLibrariesForOrdering.ToList()).RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002585 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002586
2587 if !reflect.DeepEqual(actual, expected) {
2588 t.Errorf("staticDeps orderings did not account for shared libs"+
2589 "\nactual: %v"+
2590 "\nexpected: %v",
2591 actual,
2592 expected,
2593 )
2594 }
2595}
2596
Jooyung Hanb04a4992020-03-13 18:57:35 +09002597func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002598 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002599 if !reflect.DeepEqual(actual, expected) {
2600 t.Errorf(message+
2601 "\nactual: %v"+
2602 "\nexpected: %v",
2603 actual,
2604 expected,
2605 )
2606 }
2607}
2608
Jooyung Han61b66e92020-03-21 14:21:46 +00002609func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002610 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002611 result := prepareForCcTest.RunTestWithBp(t, `
2612 cc_library {
2613 name: "libllndk",
2614 stubs: { versions: ["1", "2"] },
2615 llndk: {
2616 symbol_file: "libllndk.map.txt",
2617 },
2618 export_include_dirs: ["include"],
2619 }
2620
2621 cc_prebuilt_library_shared {
2622 name: "libllndkprebuilt",
2623 stubs: { versions: ["1", "2"] },
2624 llndk: {
2625 symbol_file: "libllndkprebuilt.map.txt",
2626 },
2627 }
2628
2629 cc_library {
2630 name: "libllndk_with_external_headers",
2631 stubs: { versions: ["1", "2"] },
2632 llndk: {
2633 symbol_file: "libllndk.map.txt",
2634 export_llndk_headers: ["libexternal_llndk_headers"],
2635 },
2636 header_libs: ["libexternal_headers"],
2637 export_header_lib_headers: ["libexternal_headers"],
2638 }
2639 cc_library_headers {
2640 name: "libexternal_headers",
2641 export_include_dirs: ["include"],
2642 vendor_available: true,
2643 }
2644 cc_library_headers {
2645 name: "libexternal_llndk_headers",
2646 export_include_dirs: ["include_llndk"],
2647 llndk: {
2648 symbol_file: "libllndk.map.txt",
2649 },
2650 vendor_available: true,
2651 }
2652
2653 cc_library {
2654 name: "libllndk_with_override_headers",
2655 stubs: { versions: ["1", "2"] },
2656 llndk: {
2657 symbol_file: "libllndk.map.txt",
2658 override_export_include_dirs: ["include_llndk"],
2659 },
2660 export_include_dirs: ["include"],
2661 }
2662 `)
2663 actual := result.ModuleVariantsForTests("libllndk")
2664 for i := 0; i < len(actual); i++ {
2665 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2666 actual = append(actual[:i], actual[i+1:]...)
2667 i--
2668 }
2669 }
2670 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002671 "android_vendor.29_arm64_armv8-a_shared_current",
2672 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002673 "android_vendor.29_arm_armv7-a-neon_shared_current",
2674 "android_vendor.29_arm_armv7-a-neon_shared",
2675 }
2676 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2677
2678 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2679 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2680
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002681 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2682 t.Helper()
2683 m := result.ModuleForTests(module, variant).Module()
Colin Cross5a377182023-12-14 14:46:23 -08002684 f, _ := android.SingletonModuleProvider(result, m, FlagExporterInfoProvider)
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002685 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2686 expectedDirs, f.IncludeDirs)
2687 }
2688
2689 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2690 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2691 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2692 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2693 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2694 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2695}
2696
Jiyong Parka46a4d52017-12-14 19:54:34 +09002697func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002698 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002699 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002700 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002701 name: "libllndk_headers",
2702 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002703 llndk: {
2704 llndk_headers: true,
2705 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002706 }
2707 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002708 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002709 llndk: {
2710 symbol_file: "libllndk.map.txt",
2711 export_llndk_headers: ["libllndk_headers"],
2712 }
Colin Cross0477b422020-10-13 18:43:54 -07002713 }
2714
2715 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002716 name: "libvendor",
2717 shared_libs: ["libllndk"],
2718 vendor: true,
2719 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002720 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002721 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002722 }
2723 `)
2724
2725 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002726 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002727 cflags := cc.Args["cFlags"]
2728 if !strings.Contains(cflags, "-Imy_include") {
2729 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2730 }
2731}
2732
Logan Chien43d34c32017-12-20 01:17:32 +08002733func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2734 actual := module.Properties.AndroidMkRuntimeLibs
2735 if !reflect.DeepEqual(actual, expected) {
2736 t.Errorf("incorrect runtime_libs for shared libs"+
2737 "\nactual: %v"+
2738 "\nexpected: %v",
2739 actual,
2740 expected,
2741 )
2742 }
2743}
2744
2745const runtimeLibAndroidBp = `
2746 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002747 name: "liball_available",
2748 vendor_available: true,
2749 product_available: true,
2750 no_libcrt : true,
2751 nocrt : true,
2752 system_shared_libs : [],
2753 }
2754 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002755 name: "libvendor_available1",
2756 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002757 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002758 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002759 nocrt : true,
2760 system_shared_libs : [],
2761 }
2762 cc_library {
2763 name: "libvendor_available2",
2764 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002765 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002766 target: {
2767 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002768 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002769 }
2770 },
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 {
Justin Yuncbca3732021-02-03 19:24:13 +09002776 name: "libproduct_vendor",
2777 product_specific: true,
2778 vendor_available: true,
2779 no_libcrt : true,
2780 nocrt : true,
2781 system_shared_libs : [],
2782 }
2783 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002784 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002785 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002786 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002787 nocrt : true,
2788 system_shared_libs : [],
2789 }
2790 cc_library {
2791 name: "libvendor1",
2792 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002793 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002794 nocrt : true,
2795 system_shared_libs : [],
2796 }
2797 cc_library {
2798 name: "libvendor2",
2799 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002800 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002801 no_libcrt : true,
2802 nocrt : true,
2803 system_shared_libs : [],
2804 }
2805 cc_library {
2806 name: "libproduct_available1",
2807 product_available: true,
2808 runtime_libs: ["liball_available"],
2809 no_libcrt : true,
2810 nocrt : true,
2811 system_shared_libs : [],
2812 }
2813 cc_library {
2814 name: "libproduct1",
2815 product_specific: true,
2816 no_libcrt : true,
2817 nocrt : true,
2818 system_shared_libs : [],
2819 }
2820 cc_library {
2821 name: "libproduct2",
2822 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002823 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002824 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002825 nocrt : true,
2826 system_shared_libs : [],
2827 }
2828`
2829
2830func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002831 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002832 ctx := testCc(t, runtimeLibAndroidBp)
2833
2834 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002835 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002836
Justin Yun8a2600c2020-12-07 12:44:03 +09002837 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2838 checkRuntimeLibs(t, []string{"liball_available"}, module)
2839
2840 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2841 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002842
2843 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002844 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002845
2846 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2847 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002848 variant = "android_vendor.29_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.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002852
2853 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002854 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002855
2856 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2857 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002858 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002859
2860 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2861 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2862
2863 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002864 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002865}
2866
2867func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002868 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002869 ctx := testCc(t, runtimeLibAndroidBp)
2870
Colin Cross7113d202019-11-20 16:39:12 -08002871 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002872 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2873 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002874
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002875 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002876 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002877 checkRuntimeLibs(t, nil, module)
2878}
2879
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002880func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002881 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002882 actual := module.Properties.AndroidMkStaticLibs
2883 if !reflect.DeepEqual(actual, expected) {
2884 t.Errorf("incorrect static_libs"+
2885 "\nactual: %v"+
2886 "\nexpected: %v",
2887 actual,
2888 expected,
2889 )
2890 }
2891}
2892
2893const staticLibAndroidBp = `
2894 cc_library {
2895 name: "lib1",
2896 }
2897 cc_library {
2898 name: "lib2",
2899 static_libs: ["lib1"],
2900 }
2901`
2902
2903func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002904 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002905 ctx := testCc(t, staticLibAndroidBp)
2906
2907 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002908 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002909 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08002910 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002911
2912 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002913 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002914 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2915 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08002916 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002917}
2918
Jiyong Parkd08b6972017-09-26 10:50:54 +09002919var compilerFlagsTestCases = []struct {
2920 in string
2921 out bool
2922}{
2923 {
2924 in: "a",
2925 out: false,
2926 },
2927 {
2928 in: "-a",
2929 out: true,
2930 },
2931 {
2932 in: "-Ipath/to/something",
2933 out: false,
2934 },
2935 {
2936 in: "-isystempath/to/something",
2937 out: false,
2938 },
2939 {
2940 in: "--coverage",
2941 out: false,
2942 },
2943 {
2944 in: "-include a/b",
2945 out: true,
2946 },
2947 {
2948 in: "-include a/b c/d",
2949 out: false,
2950 },
2951 {
2952 in: "-DMACRO",
2953 out: true,
2954 },
2955 {
2956 in: "-DMAC RO",
2957 out: false,
2958 },
2959 {
2960 in: "-a -b",
2961 out: false,
2962 },
2963 {
2964 in: "-DMACRO=definition",
2965 out: true,
2966 },
2967 {
2968 in: "-DMACRO=defi nition",
2969 out: true, // TODO(jiyong): this should be false
2970 },
2971 {
2972 in: "-DMACRO(x)=x + 1",
2973 out: true,
2974 },
2975 {
2976 in: "-DMACRO=\"defi nition\"",
2977 out: true,
2978 },
2979}
2980
2981type mockContext struct {
2982 BaseModuleContext
2983 result bool
2984}
2985
2986func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2987 // CheckBadCompilerFlags calls this function when the flag should be rejected
2988 ctx.result = false
2989}
2990
2991func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002992 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09002993 for _, testCase := range compilerFlagsTestCases {
2994 ctx := &mockContext{result: true}
2995 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2996 if ctx.result != testCase.out {
2997 t.Errorf("incorrect output:")
2998 t.Errorf(" input: %#v", testCase.in)
2999 t.Errorf(" expected: %#v", testCase.out)
3000 t.Errorf(" got: %#v", ctx.result)
3001 }
3002 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003003}
Jiyong Park374510b2018-03-19 18:23:01 +09003004
Jiyong Park37b25202018-07-11 10:49:27 +09003005func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003006 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003007 ctx := testCc(t, `
3008 cc_library_shared {
3009 name: "librecovery",
3010 recovery: true,
3011 }
3012 cc_library_shared {
3013 name: "librecovery32",
3014 recovery: true,
3015 compile_multilib:"32",
3016 }
Jiyong Park5baac542018-08-28 09:55:37 +09003017 cc_library_shared {
3018 name: "libHalInRecovery",
3019 recovery_available: true,
3020 vendor: true,
3021 }
Jiyong Park37b25202018-07-11 10:49:27 +09003022 `)
3023
3024 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003025 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003026 if len(variants) != 1 || !android.InList(arm64, variants) {
3027 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3028 }
3029
3030 variants = ctx.ModuleVariantsForTests("librecovery32")
3031 if android.InList(arm64, variants) {
3032 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3033 }
Jiyong Park5baac542018-08-28 09:55:37 +09003034
3035 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3036 if !recoveryModule.Platform() {
3037 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3038 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003039}
Jiyong Park5baac542018-08-28 09:55:37 +09003040
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003041func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003042 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003043 bp := `
3044 cc_prebuilt_test_library_shared {
3045 name: "test_lib",
3046 relative_install_path: "foo/bar/baz",
3047 srcs: ["srcpath/dontusethispath/baz.so"],
3048 }
3049
3050 cc_test {
3051 name: "main_test",
3052 data_libs: ["test_lib"],
3053 gtest: false,
3054 }
3055 `
3056
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003057 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003058 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003059 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003060 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3061
3062 ctx := testCcWithConfig(t, config)
3063 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3064 testBinary := module.(*Module).linker.(*testBinary)
3065 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3066 if err != nil {
3067 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3068 }
3069 if len(outputFiles) != 1 {
3070 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3071 }
3072 if len(testBinary.dataPaths()) != 1 {
Colin Cross7e2e7942023-11-16 12:56:02 -08003073 t.Errorf("expected exactly one test data file. test data files: [%v]", testBinary.dataPaths())
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003074 }
3075
3076 outputPath := outputFiles[0].String()
3077
3078 if !strings.HasSuffix(outputPath, "/main_test") {
3079 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3080 }
Colin Crossaa255532020-07-03 13:18:24 -07003081 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003082 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3083 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3084 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3085 }
3086}
3087
Jiyong Park7ed9de32018-10-15 22:25:07 +09003088func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003089 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003090 ctx := testCc(t, `
3091 cc_library_shared {
3092 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003093 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003094 stubs: {
3095 symbol_file: "foo.map.txt",
3096 versions: ["1", "2", "3"],
3097 },
3098 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003099
Jiyong Park7ed9de32018-10-15 22:25:07 +09003100 cc_library_shared {
3101 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003102 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003103 shared_libs: ["libFoo#1"],
3104 }`)
3105
3106 variants := ctx.ModuleVariantsForTests("libFoo")
3107 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003108 "android_arm64_armv8-a_shared",
3109 "android_arm64_armv8-a_shared_1",
3110 "android_arm64_armv8-a_shared_2",
3111 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003112 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003113 "android_arm_armv7-a-neon_shared",
3114 "android_arm_armv7-a-neon_shared_1",
3115 "android_arm_armv7-a-neon_shared_2",
3116 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003117 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003118 }
3119 variantsMismatch := false
3120 if len(variants) != len(expectedVariants) {
3121 variantsMismatch = true
3122 } else {
3123 for _, v := range expectedVariants {
3124 if !inList(v, variants) {
3125 variantsMismatch = false
3126 }
3127 }
3128 }
3129 if variantsMismatch {
3130 t.Errorf("variants of libFoo expected:\n")
3131 for _, v := range expectedVariants {
3132 t.Errorf("%q\n", v)
3133 }
3134 t.Errorf(", but got:\n")
3135 for _, v := range variants {
3136 t.Errorf("%q\n", v)
3137 }
3138 }
3139
Colin Cross7113d202019-11-20 16:39:12 -08003140 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003141 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003142 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003143 if !strings.Contains(libFlags, libFoo1StubPath) {
3144 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3145 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003146
Colin Cross7113d202019-11-20 16:39:12 -08003147 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003148 cFlags := libBarCompileRule.Args["cFlags"]
3149 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3150 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3151 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3152 }
Jiyong Park37b25202018-07-11 10:49:27 +09003153}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003154
Liz Kammer48cdbeb2023-03-17 10:17:50 -04003155func TestStubsForLibraryInMultipleApexes(t *testing.T) {
3156 t.Parallel()
3157 ctx := testCc(t, `
3158 cc_library_shared {
3159 name: "libFoo",
3160 srcs: ["foo.c"],
3161 stubs: {
3162 symbol_file: "foo.map.txt",
3163 versions: ["current"],
3164 },
3165 apex_available: ["bar", "a1"],
3166 }
3167
3168 cc_library_shared {
3169 name: "libBar",
3170 srcs: ["bar.c"],
3171 shared_libs: ["libFoo"],
3172 apex_available: ["a1"],
3173 }
3174
3175 cc_library_shared {
3176 name: "libA1",
3177 srcs: ["a1.c"],
3178 shared_libs: ["libFoo"],
3179 apex_available: ["a1"],
3180 }
3181
3182 cc_library_shared {
3183 name: "libBarA1",
3184 srcs: ["bara1.c"],
3185 shared_libs: ["libFoo"],
3186 apex_available: ["bar", "a1"],
3187 }
3188
3189 cc_library_shared {
3190 name: "libAnyApex",
3191 srcs: ["anyApex.c"],
3192 shared_libs: ["libFoo"],
3193 apex_available: ["//apex_available:anyapex"],
3194 }
3195
3196 cc_library_shared {
3197 name: "libBaz",
3198 srcs: ["baz.c"],
3199 shared_libs: ["libFoo"],
3200 apex_available: ["baz"],
3201 }
3202
3203 cc_library_shared {
3204 name: "libQux",
3205 srcs: ["qux.c"],
3206 shared_libs: ["libFoo"],
3207 apex_available: ["qux", "bar"],
3208 }`)
3209
3210 variants := ctx.ModuleVariantsForTests("libFoo")
3211 expectedVariants := []string{
3212 "android_arm64_armv8-a_shared",
3213 "android_arm64_armv8-a_shared_current",
3214 "android_arm_armv7-a-neon_shared",
3215 "android_arm_armv7-a-neon_shared_current",
3216 }
3217 variantsMismatch := false
3218 if len(variants) != len(expectedVariants) {
3219 variantsMismatch = true
3220 } else {
3221 for _, v := range expectedVariants {
3222 if !inList(v, variants) {
3223 variantsMismatch = false
3224 }
3225 }
3226 }
3227 if variantsMismatch {
3228 t.Errorf("variants of libFoo expected:\n")
3229 for _, v := range expectedVariants {
3230 t.Errorf("%q\n", v)
3231 }
3232 t.Errorf(", but got:\n")
3233 for _, v := range variants {
3234 t.Errorf("%q\n", v)
3235 }
3236 }
3237
3238 linkAgainstFoo := []string{"libBarA1"}
3239 linkAgainstFooStubs := []string{"libBar", "libA1", "libBaz", "libQux", "libAnyApex"}
3240
3241 libFooPath := "libFoo/android_arm64_armv8-a_shared/libFoo.so"
3242 for _, lib := range linkAgainstFoo {
3243 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3244 libFlags := libLinkRule.Args["libFlags"]
3245 if !strings.Contains(libFlags, libFooPath) {
3246 t.Errorf("%q: %q is not found in %q", lib, libFooPath, libFlags)
3247 }
3248 }
3249
3250 libFooStubPath := "libFoo/android_arm64_armv8-a_shared_current/libFoo.so"
3251 for _, lib := range linkAgainstFooStubs {
3252 libLinkRule := ctx.ModuleForTests(lib, "android_arm64_armv8-a_shared").Rule("ld")
3253 libFlags := libLinkRule.Args["libFlags"]
3254 if !strings.Contains(libFlags, libFooStubPath) {
3255 t.Errorf("%q: %q is not found in %q", lib, libFooStubPath, libFlags)
3256 }
3257 }
3258}
3259
Jooyung Hanb04a4992020-03-13 18:57:35 +09003260func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003261 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003262 for _, tc := range []struct{ moduleName, expected string }{
3263 {"libc", "__LIBC_API__"},
3264 {"libfoo", "__LIBFOO_API__"},
3265 {"libfoo@1", "__LIBFOO_1_API__"},
3266 {"libfoo-v1", "__LIBFOO_V1_API__"},
3267 {"libfoo.v1", "__LIBFOO_V1_API__"},
3268 } {
3269 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3270 }
3271}
3272
Liz Kammer83cf81b2022-09-22 08:24:20 -04003273func pathsToBase(paths android.Paths) []string {
3274 var ret []string
3275 for _, p := range paths {
3276 ret = append(ret, p.Base())
3277 }
3278 return ret
3279}
3280
3281func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003282 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003283 ctx := testCc(t, `
3284 cc_library_static {
3285 name: "foo",
3286 srcs: ["foo.c"],
3287 }
3288
3289 cc_library_static {
3290 name: "bar",
3291 srcs: ["bar.c"],
3292 }
3293
3294 cc_library_shared {
3295 name: "qux",
3296 srcs: ["qux.c"],
3297 }
3298
3299 cc_library_static {
3300 name: "baz",
3301 srcs: ["baz.c"],
3302 static_libs: ["foo"],
3303 shared_libs: ["qux"],
3304 whole_static_libs: ["bar"],
3305 }`)
3306
3307 variant := "android_arm64_armv8-a_static"
3308 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3309
3310 // For static libraries, the object files of a whole static dep are included in the archive
3311 // directly
3312 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3313 t.Errorf("Expected input objects %q, got %q", w, g)
3314 }
3315
3316 // non whole static dependencies are not linked into the archive
3317 if len(arRule.Implicits) > 0 {
3318 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3319 }
3320}
3321
3322func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003323 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003324 ctx := testCc(t, `
3325 cc_library_static {
3326 name: "foo",
3327 srcs: ["foo.c"],
3328 }
3329
3330 cc_library_static {
3331 name: "bar",
3332 srcs: ["bar.c"],
3333 }
3334
3335 cc_library_shared {
3336 name: "qux",
3337 srcs: ["qux.c"],
3338 }
3339
3340 cc_library_shared {
3341 name: "baz",
3342 srcs: ["baz.c"],
3343 static_libs: ["foo"],
3344 shared_libs: ["qux"],
3345 whole_static_libs: ["bar"],
3346 }`)
3347
3348 variant := "android_arm64_armv8-a_shared"
3349 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3350 libFlags := linkRule.Args["libFlags"]
3351 // When dynamically linking, we expect static dependencies to be found on the command line
3352 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3353 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3354 }
3355 // When dynamically linking, we expect whole static dependencies to be found on the command line
3356 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3357 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3358 }
3359
3360 // When dynamically linking, we expect shared dependencies to be found on the command line
3361 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3362 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3363 }
3364
3365 // We should only have the objects from the shared library srcs, not the whole static dependencies
3366 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3367 t.Errorf("Expected input objects %q, got %q", w, g)
3368 }
3369}
3370
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003371func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003372 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003373 ctx := testCc(t, `
3374 cc_binary {
3375 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003376 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003377 static_executable: true,
3378 }`)
3379
Colin Cross7113d202019-11-20 16:39:12 -08003380 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003381 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3382 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003383 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003384 for _, lib := range systemStaticLibs {
3385 if !strings.Contains(libFlags, lib) {
3386 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3387 }
3388 }
3389 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3390 for _, lib := range systemSharedLibs {
3391 if strings.Contains(libFlags, lib) {
3392 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3393 }
3394 }
3395}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003396
3397func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003398 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003399 ctx := testCc(t, `
3400 cc_binary {
3401 name: "mybin",
3402 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003403 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003404 static_executable: true,
3405 stl: "none",
3406 }
3407
3408 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003409 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003410 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003411 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003412 stl: "none",
3413 }
3414
3415 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003416 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003417 srcs: ["foo.c"],
3418 stl: "none",
3419 stubs: {
3420 versions: ["1"],
3421 },
3422 }`)
3423
Colin Cross0de8a1e2020-09-18 14:15:30 -07003424 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3425 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003426 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003427
3428 if !reflect.DeepEqual(actual, expected) {
3429 t.Errorf("staticDeps orderings were not propagated correctly"+
3430 "\nactual: %v"+
3431 "\nexpected: %v",
3432 actual,
3433 expected,
3434 )
3435 }
3436}
Jooyung Han38002912019-05-16 04:01:54 +09003437
Jooyung Hand48f3c32019-08-23 11:18:57 +09003438func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003439 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003440 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3441 cc_library {
3442 name: "libA",
3443 srcs: ["foo.c"],
3444 shared_libs: ["libB"],
3445 stl: "none",
3446 }
3447
3448 cc_library {
3449 name: "libB",
3450 srcs: ["foo.c"],
3451 enabled: false,
3452 stl: "none",
3453 }
3454 `)
3455}
3456
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003457func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3458 bp := `
3459 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003460 name: "test_afl_fuzz_target",
3461 srcs: ["foo.c"],
3462 host_supported: true,
3463 static_libs: [
3464 "afl_fuzz_static_lib",
3465 ],
3466 shared_libs: [
3467 "afl_fuzz_shared_lib",
3468 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003469 fuzzing_frameworks: {
3470 afl: true,
3471 libfuzzer: false,
3472 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003473 }
3474 cc_library {
3475 name: "afl_fuzz_static_lib",
3476 host_supported: true,
3477 srcs: ["static_file.c"],
3478 }
3479 cc_library {
3480 name: "libfuzzer_only_static_lib",
3481 host_supported: true,
3482 srcs: ["static_file.c"],
3483 }
3484 cc_library {
3485 name: "afl_fuzz_shared_lib",
3486 host_supported: true,
3487 srcs: ["shared_file.c"],
3488 static_libs: [
3489 "second_static_lib",
3490 ],
3491 }
3492 cc_library_headers {
3493 name: "libafl_headers",
3494 vendor_available: true,
3495 host_supported: true,
3496 export_include_dirs: [
3497 "include",
3498 "instrumentation",
3499 ],
3500 }
3501 cc_object {
3502 name: "afl-compiler-rt",
3503 vendor_available: true,
3504 host_supported: true,
3505 cflags: [
3506 "-fPIC",
3507 ],
3508 srcs: [
3509 "instrumentation/afl-compiler-rt.o.c",
3510 ],
3511 }
3512 cc_library {
3513 name: "second_static_lib",
3514 host_supported: true,
3515 srcs: ["second_file.c"],
3516 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003517 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003518 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003519 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003520 srcs: [
3521 "aflpp_driver.c",
3522 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003523 }`
3524
3525 testEnv := map[string]string{
3526 "FUZZ_FRAMEWORK": "AFL",
3527 }
3528
3529 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003530
3531 checkPcGuardFlag := func(
3532 modName string, variantName string, shouldHave bool) {
3533 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3534
3535 cFlags, ok := cc.Args["cFlags"]
3536 if !ok {
3537 t.Errorf("Could not find cFlags for module %s and variant %s",
3538 modName, variantName)
3539 }
3540
3541 if strings.Contains(
3542 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3543 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3544 "Test failed for module %s and variant %s",
3545 !shouldHave, shouldHave, modName, variantName)
3546 }
3547 }
3548
Cory Barkera1da26f2022-06-07 20:12:06 +00003549 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003550 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003551
3552 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003553 checkPcGuardFlag(moduleName, variant+"_static", false)
3554 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003555
3556 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003557 checkPcGuardFlag(moduleName, variant+"_static", false)
3558 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003559
3560 ctx.ModuleForTests("afl_fuzz_shared_lib",
3561 "android_arm64_armv8-a_shared").Rule("cc")
3562 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003563 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3564}
3565
3566func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003567 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003568 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3569}
3570
3571func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003572 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003573 if runtime.GOOS != "linux" {
3574 t.Skip("requires linux")
3575 }
3576
3577 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003578}
3579
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003580// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3581// correctly.
3582func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003583 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003584 ctx := testCc(t, `
3585 cc_fuzz {
3586 name: "fuzz_smoke_test",
3587 srcs: ["foo.c"],
3588 }`)
3589
Paul Duffin075c4172019-12-19 19:06:13 +00003590 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003591 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3592}
3593
Jooyung Han38002912019-05-16 04:01:54 +09003594func assertString(t *testing.T, got, expected string) {
3595 t.Helper()
3596 if got != expected {
3597 t.Errorf("expected %q got %q", expected, got)
3598 }
3599}
3600
3601func assertArrayString(t *testing.T, got, expected []string) {
3602 t.Helper()
3603 if len(got) != len(expected) {
3604 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3605 return
3606 }
3607 for i := range got {
3608 if got[i] != expected[i] {
3609 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3610 i, expected[i], expected, got[i], got)
3611 return
3612 }
3613 }
3614}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003615
Jooyung Han0302a842019-10-30 18:43:49 +09003616func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3617 t.Helper()
Cole Faust18994c72023-02-28 16:02:16 -08003618 assertArrayString(t, android.SortedKeys(m), expected)
Jooyung Han0302a842019-10-30 18:43:49 +09003619}
3620
Colin Crosse1bb5d02019-09-24 14:55:04 -07003621func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003622 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003623 ctx := testCc(t, `
3624 cc_defaults {
3625 name: "defaults",
3626 srcs: ["foo.c"],
3627 static: {
3628 srcs: ["bar.c"],
3629 },
3630 shared: {
3631 srcs: ["baz.c"],
3632 },
3633 }
3634
3635 cc_library_static {
3636 name: "libstatic",
3637 defaults: ["defaults"],
3638 }
3639
3640 cc_library_shared {
3641 name: "libshared",
3642 defaults: ["defaults"],
3643 }
3644
3645 cc_library {
3646 name: "libboth",
3647 defaults: ["defaults"],
3648 }
3649
3650 cc_binary {
3651 name: "binary",
3652 defaults: ["defaults"],
3653 }`)
3654
Colin Cross7113d202019-11-20 16:39:12 -08003655 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003656 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3657 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3658 }
Colin Cross7113d202019-11-20 16:39:12 -08003659 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003660 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3661 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3662 }
Colin Cross7113d202019-11-20 16:39:12 -08003663 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003664 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3665 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3666 }
3667
Colin Cross7113d202019-11-20 16:39:12 -08003668 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003669 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3670 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3671 }
Colin Cross7113d202019-11-20 16:39:12 -08003672 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003673 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3674 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3675 }
3676}
Colin Crosseabaedd2020-02-06 17:01:55 -08003677
3678func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003679 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003680 bp := `
3681 cc_defaults {
3682 name: "libfoo_defaults",
3683 srcs: ["foo.c"],
3684 cppflags: ["-DFOO"],
3685 product_variables: {
3686 debuggable: {
3687 cppflags: ["-DBAR"],
3688 },
3689 },
3690 }
3691
3692 cc_library {
3693 name: "libfoo",
3694 defaults: ["libfoo_defaults"],
3695 }
3696 `
3697
Paul Duffin8567f222021-03-23 00:02:06 +00003698 result := android.GroupFixturePreparers(
3699 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003700 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003701
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003702 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3703 variables.Debuggable = BoolPtr(true)
3704 }),
3705 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003706
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003707 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003708 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003709}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003710
3711func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3712 t.Parallel()
3713 bp := `
3714 cc_library_static {
3715 name: "libfoo",
3716 srcs: ["foo.c"],
3717 whole_static_libs: ["libbar"],
3718 }
3719
3720 cc_library_static {
3721 name: "libbar",
3722 whole_static_libs: ["libmissing"],
3723 }
3724 `
3725
Paul Duffin8567f222021-03-23 00:02:06 +00003726 result := android.GroupFixturePreparers(
3727 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003728 android.PrepareForTestWithAllowMissingDependencies,
3729 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003730
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003731 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003732 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003733
Paul Duffine84b1332021-03-12 11:59:43 +00003734 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003735
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003736 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003737 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003738}
Colin Crosse9fe2942020-11-10 18:12:15 -08003739
3740func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003741 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003742 bp := `
3743 cc_binary {
3744 name: "bin",
3745 host_supported: true,
3746 shared_libs: ["libshared"],
3747 runtime_libs: ["libruntime"],
3748 srcs: [":gen"],
3749 }
3750
3751 cc_library_shared {
3752 name: "libshared",
3753 host_supported: true,
3754 shared_libs: ["libtransitive"],
3755 }
3756
3757 cc_library_shared {
3758 name: "libtransitive",
3759 host_supported: true,
3760 }
3761
3762 cc_library_shared {
3763 name: "libruntime",
3764 host_supported: true,
3765 }
3766
3767 cc_binary_host {
3768 name: "tool",
3769 srcs: ["foo.cpp"],
3770 }
3771
3772 genrule {
3773 name: "gen",
3774 tools: ["tool"],
3775 out: ["gen.cpp"],
3776 cmd: "$(location tool) $(out)",
3777 }
3778 `
3779
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003780 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003781 ctx := testCcWithConfig(t, config)
3782
3783 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3784 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3785 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3786 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3787 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3788
3789 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3790 t.Errorf("expected host bin dependency %q, got %q", w, g)
3791 }
3792
3793 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3794 t.Errorf("expected host bin dependency %q, got %q", w, g)
3795 }
3796
3797 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3798 t.Errorf("expected host bin dependency %q, got %q", w, g)
3799 }
3800
3801 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3802 t.Errorf("expected host bin dependency %q, got %q", w, g)
3803 }
3804
3805 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3806 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3807 }
3808
3809 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3810 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3811 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3812 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3813
3814 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3815 t.Errorf("expected device bin dependency %q, got %q", w, g)
3816 }
3817
3818 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3819 t.Errorf("expected device bin dependency %q, got %q", w, g)
3820 }
3821
3822 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3823 t.Errorf("expected device bin dependency %q, got %q", w, g)
3824 }
3825
3826 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3827 t.Errorf("expected device bin dependency %q, got %q", w, g)
3828 }
3829
3830 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3831 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3832 }
3833
3834}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003835
3836func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003837 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09003838 ctx := testCc(t, `
3839 cc_library_shared {
3840 name: "libclient",
3841 srcs: ["foo.c"],
3842 shared_libs: ["libfoo#1"],
3843 }
3844
3845 cc_library_shared {
3846 name: "libfoo",
3847 srcs: ["foo.c"],
3848 shared_libs: ["libbar"],
3849 export_shared_lib_headers: ["libbar"],
3850 stubs: {
3851 symbol_file: "foo.map.txt",
3852 versions: ["1", "2", "3"],
3853 },
3854 }
3855
3856 cc_library_shared {
3857 name: "libbar",
3858 export_include_dirs: ["include/libbar"],
3859 srcs: ["foo.c"],
3860 }`)
3861
3862 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3863
3864 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3865 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3866 }
3867}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003868
Vinh Tran09581952023-05-16 16:03:20 -04003869func TestAidlLibraryWithHeaders(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04003870 t.Parallel()
3871 ctx := android.GroupFixturePreparers(
3872 prepareForCcTest,
3873 aidl_library.PrepareForTestWithAidlLibrary,
3874 android.MockFS{
3875 "package_bar/Android.bp": []byte(`
3876 aidl_library {
3877 name: "bar",
3878 srcs: ["x/y/Bar.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003879 hdrs: ["x/HeaderBar.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003880 strip_import_prefix: "x",
3881 }
3882 `)}.AddToFixture(),
3883 android.MockFS{
3884 "package_foo/Android.bp": []byte(`
3885 aidl_library {
3886 name: "foo",
3887 srcs: ["a/b/Foo.aidl"],
Vinh Tran09581952023-05-16 16:03:20 -04003888 hdrs: ["a/HeaderFoo.aidl"],
Vinh Tran367d89d2023-04-28 11:21:25 -04003889 strip_import_prefix: "a",
3890 deps: ["bar"],
3891 }
3892 cc_library {
3893 name: "libfoo",
3894 aidl: {
3895 libs: ["foo"],
3896 }
3897 }
3898 `),
3899 }.AddToFixture(),
3900 ).RunTest(t).TestContext
3901
3902 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Vinh Tran09581952023-05-16 16:03:20 -04003903
3904 android.AssertPathsRelativeToTopEquals(
3905 t,
3906 "aidl headers",
3907 []string{
3908 "package_bar/x/HeaderBar.aidl",
3909 "package_foo/a/HeaderFoo.aidl",
3910 "package_foo/a/b/Foo.aidl",
3911 "out/soong/.intermediates/package_foo/libfoo/android_arm64_armv8-a_static/gen/aidl_library.sbox.textproto",
3912 },
3913 libfoo.Rule("aidl_library").Implicits,
3914 )
3915
Colin Crossf61d03d2023-11-02 16:56:39 -07003916 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl_library.sbox.textproto"))
Vinh Tran367d89d2023-04-28 11:21:25 -04003917 aidlCommand := manifest.Commands[0].GetCommand()
3918
3919 expectedAidlFlags := "-Ipackage_foo/a -Ipackage_bar/x"
3920 if !strings.Contains(aidlCommand, expectedAidlFlags) {
3921 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlags)
3922 }
3923
3924 outputs := strings.Join(libfoo.AllOutputs(), " ")
3925
Vinh Tran09581952023-05-16 16:03:20 -04003926 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BpFoo.h")
3927 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/BnFoo.h")
3928 android.AssertStringDoesContain(t, "aidl-generated header", outputs, "gen/aidl_library/b/Foo.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003929 android.AssertStringDoesContain(t, "aidl-generated cpp", outputs, "b/Foo.cpp")
3930 // Confirm that the aidl header doesn't get compiled to cpp and h files
Vinh Tran09581952023-05-16 16:03:20 -04003931 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BpBar.h")
3932 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/BnBar.h")
3933 android.AssertStringDoesNotContain(t, "aidl-generated header", outputs, "gen/aidl_library/y/Bar.h")
Vinh Tran367d89d2023-04-28 11:21:25 -04003934 android.AssertStringDoesNotContain(t, "aidl-generated cpp", outputs, "y/Bar.cpp")
3935}
3936
Jooyung Hane197d8b2021-01-05 10:33:16 +09003937func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003938 t.Parallel()
Vinh Tran367d89d2023-04-28 11:21:25 -04003939 ctx := android.GroupFixturePreparers(
3940 prepareForCcTest,
3941 aidl_library.PrepareForTestWithAidlLibrary,
3942 ).RunTestWithBp(t, `
Jooyung Hane197d8b2021-01-05 10:33:16 +09003943 cc_library {
3944 name: "libfoo",
3945 srcs: ["a/Foo.aidl"],
3946 aidl: { flags: ["-Werror"], },
3947 }
3948 `)
3949
3950 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
Colin Crossf61d03d2023-11-02 16:56:39 -07003951 manifest := android.RuleBuilderSboxProtoForTests(t, ctx.TestContext, libfoo.Output("aidl.sbox.textproto"))
Jooyung Hane197d8b2021-01-05 10:33:16 +09003952 aidlCommand := manifest.Commands[0].GetCommand()
3953 expectedAidlFlag := "-Werror"
3954 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3955 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3956 }
3957}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003958
Jooyung Han07f70c02021-11-06 07:08:45 +09003959func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003960 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09003961 for _, tc := range []struct {
3962 name string
3963 sdkVersion string
3964 variant string
3965 expected string
3966 }{
3967 {
3968 name: "default is current",
3969 sdkVersion: "",
3970 variant: "android_arm64_armv8-a_static",
3971 expected: "platform_apis",
3972 },
3973 {
3974 name: "use sdk_version",
3975 sdkVersion: `sdk_version: "29"`,
3976 variant: "android_arm64_armv8-a_static",
3977 expected: "platform_apis",
3978 },
3979 {
3980 name: "use sdk_version(sdk variant)",
3981 sdkVersion: `sdk_version: "29"`,
3982 variant: "android_arm64_armv8-a_sdk_static",
3983 expected: "29",
3984 },
3985 {
3986 name: "use min_sdk_version",
3987 sdkVersion: `min_sdk_version: "29"`,
3988 variant: "android_arm64_armv8-a_static",
3989 expected: "29",
3990 },
3991 } {
3992 t.Run(tc.name, func(t *testing.T) {
3993 ctx := testCc(t, `
3994 cc_library {
3995 name: "libfoo",
3996 stl: "none",
3997 srcs: ["a/Foo.aidl"],
3998 `+tc.sdkVersion+`
3999 }
4000 `)
4001 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
Colin Crossf61d03d2023-11-02 16:56:39 -07004002 manifest := android.RuleBuilderSboxProtoForTests(t, ctx, libfoo.Output("aidl.sbox.textproto"))
Jooyung Han07f70c02021-11-06 07:08:45 +09004003 aidlCommand := manifest.Commands[0].GetCommand()
4004 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4005 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4006 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4007 }
4008 })
4009 }
4010}
4011
Vinh Tran09581952023-05-16 16:03:20 -04004012func TestInvalidAidlProp(t *testing.T) {
4013 t.Parallel()
4014
4015 testCases := []struct {
4016 description string
4017 bp string
4018 }{
4019 {
4020 description: "Invalid use of aidl.libs and aidl.include_dirs",
4021 bp: `
4022 cc_library {
4023 name: "foo",
4024 aidl: {
4025 libs: ["foo_aidl"],
4026 include_dirs: ["bar/include"],
4027 }
4028 }
4029 `,
4030 },
4031 {
4032 description: "Invalid use of aidl.libs and aidl.local_include_dirs",
4033 bp: `
4034 cc_library {
4035 name: "foo",
4036 aidl: {
4037 libs: ["foo_aidl"],
4038 local_include_dirs: ["include"],
4039 }
4040 }
4041 `,
4042 },
4043 }
4044
4045 for _, testCase := range testCases {
4046 t.Run(testCase.description, func(t *testing.T) {
4047 bp := `
4048 aidl_library {
4049 name: "foo_aidl",
4050 srcs: ["Foo.aidl"],
4051 } ` + testCase.bp
4052 android.GroupFixturePreparers(
4053 prepareForCcTest,
4054 aidl_library.PrepareForTestWithAidlLibrary.
4055 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern("For aidl headers, please only use aidl.libs prop")),
4056 ).RunTestWithBp(t, bp)
4057 })
4058 }
4059}
4060
Jiyong Parka008fb02021-03-16 17:15:53 +09004061func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004062 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004063 ctx := testCc(t, `
4064 cc_library_shared {
4065 name: "libfoo",
4066 srcs: ["foo.c"],
4067 min_sdk_version: "29",
4068 }`)
4069
4070 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4071 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4072}
4073
Vinh Tranf1924742022-06-24 16:40:11 -04004074func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004075 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004076 bp := `
4077 cc_library_shared {
4078 name: "libfoo",
4079 srcs: ["foo.c"],
4080 min_sdk_version: "S",
4081 }
4082 `
4083 result := android.GroupFixturePreparers(
4084 prepareForCcTest,
4085 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4086 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4087 }),
4088 ).RunTestWithBp(t, bp)
4089 ctx := result.TestContext
4090 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4091 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4092}
4093
Paul Duffin3cb603e2021-02-19 13:57:10 +00004094func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004095 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004096
4097 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4098 // embedded newline characters alone.
4099 trimIndentingSpaces := func(s string) string {
4100 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4101 }
4102
4103 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4104 t.Helper()
4105 expected = trimIndentingSpaces(expected)
4106 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4107 if expected != actual {
4108 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4109 }
4110 }
4111
4112 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4113
4114 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4115 t.Helper()
Colin Cross5a377182023-12-14 14:46:23 -08004116 exported, _ := android.SingletonModuleProvider(ctx, module, FlagExporterInfoProvider)
Paul Duffin3cb603e2021-02-19 13:57:10 +00004117 name := module.Name()
4118
4119 for _, checker := range checkers {
4120 checker(t, name, exported)
4121 }
4122 }
4123
4124 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4125 return func(t *testing.T, name string, exported FlagExporterInfo) {
4126 t.Helper()
4127 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4128 }
4129 }
4130
4131 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4132 return func(t *testing.T, name string, exported FlagExporterInfo) {
4133 t.Helper()
4134 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4135 }
4136 }
4137
4138 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4139 return func(t *testing.T, name string, exported FlagExporterInfo) {
4140 t.Helper()
4141 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4142 }
4143 }
4144
4145 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4146 return func(t *testing.T, name string, exported FlagExporterInfo) {
4147 t.Helper()
4148 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4149 }
4150 }
4151
4152 genRuleModules := `
4153 genrule {
4154 name: "genrule_foo",
4155 cmd: "generate-foo",
4156 out: [
4157 "generated_headers/foo/generated_header.h",
4158 ],
4159 export_include_dirs: [
4160 "generated_headers",
4161 ],
4162 }
4163
4164 genrule {
4165 name: "genrule_bar",
4166 cmd: "generate-bar",
4167 out: [
4168 "generated_headers/bar/generated_header.h",
4169 ],
4170 export_include_dirs: [
4171 "generated_headers",
4172 ],
4173 }
4174 `
4175
4176 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4177 ctx := testCc(t, genRuleModules+`
4178 cc_library {
4179 name: "libfoo",
4180 srcs: ["foo.c"],
4181 export_include_dirs: ["foo/standard"],
4182 export_system_include_dirs: ["foo/system"],
4183 generated_headers: ["genrule_foo"],
4184 export_generated_headers: ["genrule_foo"],
4185 }
4186
4187 cc_library {
4188 name: "libbar",
4189 srcs: ["bar.c"],
4190 shared_libs: ["libfoo"],
4191 export_include_dirs: ["bar/standard"],
4192 export_system_include_dirs: ["bar/system"],
4193 generated_headers: ["genrule_bar"],
4194 export_generated_headers: ["genrule_bar"],
4195 }
4196 `)
4197 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4198 checkIncludeDirs(t, ctx, foo,
4199 expectedIncludeDirs(`
4200 foo/standard
4201 .intermediates/genrule_foo/gen/generated_headers
4202 `),
4203 expectedSystemIncludeDirs(`foo/system`),
4204 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4205 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4206 )
4207
4208 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4209 checkIncludeDirs(t, ctx, bar,
4210 expectedIncludeDirs(`
4211 bar/standard
4212 .intermediates/genrule_bar/gen/generated_headers
4213 `),
4214 expectedSystemIncludeDirs(`bar/system`),
4215 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4216 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4217 )
4218 })
4219
4220 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4221 ctx := testCc(t, genRuleModules+`
4222 cc_library {
4223 name: "libfoo",
4224 srcs: ["foo.c"],
4225 export_include_dirs: ["foo/standard"],
4226 export_system_include_dirs: ["foo/system"],
4227 generated_headers: ["genrule_foo"],
4228 export_generated_headers: ["genrule_foo"],
4229 }
4230
4231 cc_library {
4232 name: "libbar",
4233 srcs: ["bar.c"],
4234 whole_static_libs: ["libfoo"],
4235 export_include_dirs: ["bar/standard"],
4236 export_system_include_dirs: ["bar/system"],
4237 generated_headers: ["genrule_bar"],
4238 export_generated_headers: ["genrule_bar"],
4239 }
4240 `)
4241 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4242 checkIncludeDirs(t, ctx, foo,
4243 expectedIncludeDirs(`
4244 foo/standard
4245 .intermediates/genrule_foo/gen/generated_headers
4246 `),
4247 expectedSystemIncludeDirs(`foo/system`),
4248 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4249 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4250 )
4251
4252 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4253 checkIncludeDirs(t, ctx, bar,
4254 expectedIncludeDirs(`
4255 bar/standard
4256 foo/standard
4257 .intermediates/genrule_foo/gen/generated_headers
4258 .intermediates/genrule_bar/gen/generated_headers
4259 `),
4260 expectedSystemIncludeDirs(`
4261 bar/system
4262 foo/system
4263 `),
4264 expectedGeneratedHeaders(`
4265 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4266 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4267 `),
4268 expectedOrderOnlyDeps(`
4269 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4270 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4271 `),
4272 )
4273 })
4274
Paul Duffin3cb603e2021-02-19 13:57:10 +00004275 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
Vinh Tran367d89d2023-04-28 11:21:25 -04004276 ctx := android.GroupFixturePreparers(
4277 prepareForCcTest,
4278 aidl_library.PrepareForTestWithAidlLibrary,
4279 ).RunTestWithBp(t, `
4280 aidl_library {
4281 name: "libfoo_aidl",
4282 srcs: ["x/y/Bar.aidl"],
4283 strip_import_prefix: "x",
4284 }
Paul Duffin3cb603e2021-02-19 13:57:10 +00004285 cc_library_shared {
4286 name: "libfoo",
4287 srcs: [
4288 "foo.c",
4289 "b.aidl",
4290 "a.proto",
4291 ],
4292 aidl: {
Vinh Tran367d89d2023-04-28 11:21:25 -04004293 libs: ["libfoo_aidl"],
Paul Duffin3cb603e2021-02-19 13:57:10 +00004294 export_aidl_headers: true,
4295 }
4296 }
Vinh Tran367d89d2023-04-28 11:21:25 -04004297 `).TestContext
Paul Duffin3cb603e2021-02-19 13:57:10 +00004298 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4299 checkIncludeDirs(t, ctx, foo,
4300 expectedIncludeDirs(`
4301 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
Vinh Tran09581952023-05-16 16:03:20 -04004302 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library
Paul Duffin3cb603e2021-02-19 13:57:10 +00004303 `),
4304 expectedSystemIncludeDirs(``),
4305 expectedGeneratedHeaders(`
4306 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4307 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4308 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004309 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4310 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4311 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004312 `),
4313 expectedOrderOnlyDeps(`
4314 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4315 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4316 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Vinh Tran09581952023-05-16 16:03:20 -04004317 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/Bar.h
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BnBar.h
4319 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl_library/y/BpBar.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004320 `),
4321 )
4322 })
4323
Paul Duffin3cb603e2021-02-19 13:57:10 +00004324 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4325 ctx := testCc(t, genRuleModules+`
4326 cc_library_shared {
4327 name: "libfoo",
4328 srcs: [
4329 "foo.c",
4330 "b.aidl",
4331 "a.proto",
4332 ],
4333 proto: {
4334 export_proto_headers: true,
4335 }
4336 }
4337 `)
4338 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4339 checkIncludeDirs(t, ctx, foo,
4340 expectedIncludeDirs(`
4341 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4342 `),
4343 expectedSystemIncludeDirs(``),
4344 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004345 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4346 `),
4347 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004348 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4349 `),
4350 )
4351 })
4352
Paul Duffin33056e82021-02-19 13:49:08 +00004353 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004354 ctx := testCc(t, genRuleModules+`
4355 cc_library_shared {
4356 name: "libfoo",
4357 srcs: [
4358 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004359 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004360 "b.aidl",
4361 "a.proto",
4362 ],
4363 }
4364 `)
4365 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4366 checkIncludeDirs(t, ctx, foo,
4367 expectedIncludeDirs(`
4368 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4369 `),
4370 expectedSystemIncludeDirs(``),
4371 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004372 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004373 `),
4374 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004375 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4376 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004377 `),
4378 )
4379 })
4380}
Colin Crossae628182021-06-14 16:52:28 -07004381
4382func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004383 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004384 baseExpectedFlags := []string{
4385 "${config.ArmThumbCflags}",
4386 "${config.ArmCflags}",
4387 "${config.CommonGlobalCflags}",
4388 "${config.DeviceGlobalCflags}",
4389 "${config.ExternalCflags}",
4390 "${config.ArmToolchainCflags}",
4391 "${config.ArmArmv7ANeonCflags}",
4392 "${config.ArmGenericCflags}",
4393 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004394 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004395 }
4396
4397 expectedIncludes := []string{
4398 "external/foo/android_arm_export_include_dirs",
4399 "external/foo/lib32_export_include_dirs",
4400 "external/foo/arm_export_include_dirs",
4401 "external/foo/android_export_include_dirs",
4402 "external/foo/linux_export_include_dirs",
4403 "external/foo/export_include_dirs",
4404 "external/foo/android_arm_local_include_dirs",
4405 "external/foo/lib32_local_include_dirs",
4406 "external/foo/arm_local_include_dirs",
4407 "external/foo/android_local_include_dirs",
4408 "external/foo/linux_local_include_dirs",
4409 "external/foo/local_include_dirs",
4410 "external/foo",
4411 "external/foo/libheader1",
4412 "external/foo/libheader2",
4413 "external/foo/libwhole1",
4414 "external/foo/libwhole2",
4415 "external/foo/libstatic1",
4416 "external/foo/libstatic2",
4417 "external/foo/libshared1",
4418 "external/foo/libshared2",
4419 "external/foo/liblinux",
4420 "external/foo/libandroid",
4421 "external/foo/libarm",
4422 "external/foo/lib32",
4423 "external/foo/libandroid_arm",
4424 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004425 }
4426
4427 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4428 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4429
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004430 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesfb294e32023-06-14 10:42:45 -07004431 cstd := []string{"-std=gnu17", "-std=conly"}
Elliott Hughesc79d9e32022-01-13 14:56:02 -08004432 cppstd := []string{"-std=gnu++20", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004433
4434 lastIncludes := []string{
4435 "out/soong/ndk/sysroot/usr/include",
4436 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4437 }
4438
4439 combineSlices := func(slices ...[]string) []string {
4440 var ret []string
4441 for _, s := range slices {
4442 ret = append(ret, s...)
4443 }
4444 return ret
4445 }
4446
4447 testCases := []struct {
4448 name string
4449 src string
4450 expected []string
4451 }{
4452 {
4453 name: "c",
4454 src: "foo.c",
Yi Kong13beeed2023-07-15 03:09:00 +09004455 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004456 },
4457 {
4458 name: "cc",
4459 src: "foo.cc",
Yi Kong13beeed2023-07-15 03:09:00 +09004460 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004461 },
4462 {
4463 name: "assemble",
4464 src: "foo.s",
Yi Kong13beeed2023-07-15 03:09:00 +09004465 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004466 },
4467 }
4468
4469 for _, tc := range testCases {
4470 t.Run(tc.name, func(t *testing.T) {
4471 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004472 cc_library {
4473 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004474 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004475 cflags: ["-std=candcpp"],
4476 conlyflags: ["-std=conly"],
4477 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004478 local_include_dirs: ["local_include_dirs"],
4479 export_include_dirs: ["export_include_dirs"],
4480 export_system_include_dirs: ["export_system_include_dirs"],
4481 static_libs: ["libstatic1", "libstatic2"],
4482 whole_static_libs: ["libwhole1", "libwhole2"],
4483 shared_libs: ["libshared1", "libshared2"],
4484 header_libs: ["libheader1", "libheader2"],
4485 target: {
4486 android: {
4487 shared_libs: ["libandroid"],
4488 local_include_dirs: ["android_local_include_dirs"],
4489 export_include_dirs: ["android_export_include_dirs"],
4490 },
4491 android_arm: {
4492 shared_libs: ["libandroid_arm"],
4493 local_include_dirs: ["android_arm_local_include_dirs"],
4494 export_include_dirs: ["android_arm_export_include_dirs"],
4495 },
4496 linux: {
4497 shared_libs: ["liblinux"],
4498 local_include_dirs: ["linux_local_include_dirs"],
4499 export_include_dirs: ["linux_export_include_dirs"],
4500 },
4501 },
4502 multilib: {
4503 lib32: {
4504 shared_libs: ["lib32"],
4505 local_include_dirs: ["lib32_local_include_dirs"],
4506 export_include_dirs: ["lib32_export_include_dirs"],
4507 },
4508 },
4509 arch: {
4510 arm: {
4511 shared_libs: ["libarm"],
4512 local_include_dirs: ["arm_local_include_dirs"],
4513 export_include_dirs: ["arm_export_include_dirs"],
4514 },
4515 },
4516 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004517 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004518 }
4519
4520 cc_library_headers {
4521 name: "libheader1",
4522 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004523 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004524 stl: "none",
4525 }
4526
4527 cc_library_headers {
4528 name: "libheader2",
4529 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004530 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004531 stl: "none",
4532 }
Liz Kammer08572c62021-09-30 10:11:04 -04004533 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004534
Liz Kammer08572c62021-09-30 10:11:04 -04004535 libs := []string{
4536 "libstatic1",
4537 "libstatic2",
4538 "libwhole1",
4539 "libwhole2",
4540 "libshared1",
4541 "libshared2",
4542 "libandroid",
4543 "libandroid_arm",
4544 "liblinux",
4545 "lib32",
4546 "libarm",
4547 }
Colin Crossae628182021-06-14 16:52:28 -07004548
Liz Kammer08572c62021-09-30 10:11:04 -04004549 for _, lib := range libs {
4550 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004551 cc_library {
4552 name: "%s",
4553 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004554 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004555 stl: "none",
4556 }
4557 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004558 }
4559
4560 ctx := android.GroupFixturePreparers(
4561 PrepareForIntegrationTestWithCc,
4562 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4563 ).RunTest(t)
4564 // Use the arm variant instead of the arm64 variant so that it gets headers from
4565 // ndk_libandroid_support to test LateStaticLibs.
4566 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4567
4568 var includes []string
4569 flags := strings.Split(cflags, " ")
4570 for _, flag := range flags {
4571 if strings.HasPrefix(flag, "-I") {
4572 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4573 } else if flag == "-isystem" {
4574 // skip isystem, include next
4575 } else if len(flag) > 0 {
4576 includes = append(includes, flag)
4577 }
4578 }
4579
4580 android.AssertArrayString(t, "includes", tc.expected, includes)
4581 })
Colin Crossae628182021-06-14 16:52:28 -07004582 }
4583
Colin Crossae628182021-06-14 16:52:28 -07004584}
Alixb5f6d9e2022-04-20 23:00:58 +00004585
zijunzhao933e3802023-01-12 07:26:20 +00004586func TestAddnoOverride64GlobalCflags(t *testing.T) {
4587 t.Parallel()
4588 ctx := testCc(t, `
4589 cc_library_shared {
4590 name: "libclient",
4591 srcs: ["foo.c"],
4592 shared_libs: ["libfoo#1"],
4593 }
4594
4595 cc_library_shared {
4596 name: "libfoo",
4597 srcs: ["foo.c"],
4598 shared_libs: ["libbar"],
4599 export_shared_lib_headers: ["libbar"],
4600 stubs: {
4601 symbol_file: "foo.map.txt",
4602 versions: ["1", "2", "3"],
4603 },
4604 }
4605
4606 cc_library_shared {
4607 name: "libbar",
4608 export_include_dirs: ["include/libbar"],
4609 srcs: ["foo.c"],
4610 }`)
4611
4612 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4613
4614 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4615 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4616 }
4617}
4618
Alixb5f6d9e2022-04-20 23:00:58 +00004619func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004620 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004621 tests := []struct {
4622 name string
4623 clang bool
4624 BuildBrokenClangProperty bool
4625 err string
4626 }{
4627 {
4628 name: "error when clang is set to false",
4629 clang: false,
4630 err: "is no longer supported",
4631 },
4632 {
4633 name: "error when clang is set to true",
4634 clang: true,
4635 err: "property is deprecated, see Changes.md",
4636 },
4637 {
4638 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4639 clang: true,
4640 BuildBrokenClangProperty: true,
4641 },
4642 }
4643
4644 for _, test := range tests {
4645 t.Run(test.name, func(t *testing.T) {
4646 bp := fmt.Sprintf(`
4647 cc_library {
4648 name: "foo",
4649 clang: %t,
4650 }`, test.clang)
4651
4652 if test.err == "" {
4653 android.GroupFixturePreparers(
4654 prepareForCcTest,
4655 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4656 if test.BuildBrokenClangProperty {
4657 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4658 }
4659 }),
4660 ).RunTestWithBp(t, bp)
4661 } else {
4662 prepareForCcTest.
4663 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4664 RunTestWithBp(t, bp)
4665 }
4666 })
4667 }
4668}
Alix Espinoef47e542022-09-14 19:10:51 +00004669
4670func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004671 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004672 tests := []struct {
4673 name string
4674 clangAsFlags []string
4675 BuildBrokenClangAsFlags bool
4676 err string
4677 }{
4678 {
4679 name: "error when clang_asflags is set",
4680 clangAsFlags: []string{"-a", "-b"},
4681 err: "clang_asflags: property is deprecated",
4682 },
4683 {
4684 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4685 clangAsFlags: []string{"-a", "-b"},
4686 BuildBrokenClangAsFlags: true,
4687 },
4688 }
4689
4690 for _, test := range tests {
4691 t.Run(test.name, func(t *testing.T) {
4692 bp := fmt.Sprintf(`
4693 cc_library {
4694 name: "foo",
4695 clang_asflags: %s,
4696 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4697
4698 if test.err == "" {
4699 android.GroupFixturePreparers(
4700 prepareForCcTest,
4701 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4702 if test.BuildBrokenClangAsFlags {
4703 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4704 }
4705 }),
4706 ).RunTestWithBp(t, bp)
4707 } else {
4708 prepareForCcTest.
4709 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4710 RunTestWithBp(t, bp)
4711 }
4712 })
4713 }
4714}
4715
4716func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004717 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004718 tests := []struct {
4719 name string
4720 clangCFlags []string
4721 BuildBrokenClangCFlags bool
4722 err string
4723 }{
4724 {
4725 name: "error when clang_cflags is set",
4726 clangCFlags: []string{"-a", "-b"},
4727 err: "clang_cflags: property is deprecated",
4728 },
4729 {
4730 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4731 clangCFlags: []string{"-a", "-b"},
4732 BuildBrokenClangCFlags: true,
4733 },
4734 }
4735
4736 for _, test := range tests {
4737 t.Run(test.name, func(t *testing.T) {
4738 bp := fmt.Sprintf(`
4739 cc_library {
4740 name: "foo",
4741 clang_cflags: %s,
4742 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4743
4744 if test.err == "" {
4745 android.GroupFixturePreparers(
4746 prepareForCcTest,
4747 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4748 if test.BuildBrokenClangCFlags {
4749 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4750 }
4751 }),
4752 ).RunTestWithBp(t, bp)
4753 } else {
4754 prepareForCcTest.
4755 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4756 RunTestWithBp(t, bp)
4757 }
4758 })
4759 }
4760}
Wei Li5f5d2712023-12-11 15:40:29 -08004761
4762func TestStrippedAllOutputFile(t *testing.T) {
4763 t.Parallel()
4764 bp := `
4765 cc_library {
4766 name: "test_lib",
4767 srcs: ["test_lib.cpp"],
4768 dist: {
4769 targets: [ "dist_target" ],
4770 tag: "stripped_all",
4771 }
4772 }
4773 `
4774 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
4775 ctx := testCcWithConfig(t, config)
4776 module := ctx.ModuleForTests("test_lib", "android_arm_armv7-a-neon_shared").Module()
4777 outputFile, err := module.(android.OutputFileProducer).OutputFiles("stripped_all")
4778 if err != nil {
4779 t.Errorf("Expected cc_library to produce output files, error: %s", err)
4780 return
4781 }
4782 if !strings.HasSuffix(outputFile.Strings()[0], "/stripped_all/test_lib.so") {
4783 t.Errorf("Unexpected output file: %s", outputFile.Strings()[0])
4784 return
4785 }
4786}