blob: 62adfd3ae49c8b381679efb45952eb1b1a384217 [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
27 "android/soong/android"
Sam Delmerico4e115cc2023-01-19 15:36:52 -050028 "android/soong/bazel/cquery"
Colin Cross74d1ec02015-04-28 13:30:13 -070029)
30
Jiyong Park6a43f042017-10-12 23:05:00 +090031func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000032 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090033}
34
Paul Duffin2e6f90e2021-03-22 23:20:25 +000035var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000036 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000037 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
38 variables.DeviceVndkVersion = StringPtr("current")
39 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090040 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000041 }),
42)
43
Paul Duffin8567f222021-03-23 00:02:06 +000044// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000045//
46// See testCc for an explanation as to how to stop using this deprecated method.
47//
48// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080049func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070050 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000051 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000052 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090053}
54
Paul Duffin8567f222021-03-23 00:02:06 +000055// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000056//
Paul Duffin8567f222021-03-23 00:02:06 +000057// 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 +000058// easier to customize the test behavior.
59//
60// 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 +000061// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000062// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
63// that it did not change the test behavior unexpectedly.
64//
65// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080066func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080067 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000068 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000069 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080070}
71
Paul Duffin8567f222021-03-23 00:02:06 +000072// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000073//
74// See testCc for an explanation as to how to stop using this deprecated method.
75//
76// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080077func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080078 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000079 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090080 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080081
Colin Cross98be1bb2019-12-13 20:41:13 -080082 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080083}
84
Paul Duffin8567f222021-03-23 00:02:06 +000085// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000086//
87// See testCc for an explanation as to how to stop using this deprecated method.
88//
89// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090090func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
91 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000092 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090093 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090094 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090095
96 return testCcWithConfig(t, config)
97}
98
Paul Duffin8567f222021-03-23 00:02:06 +000099// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000100//
101// See testCc for an explanation as to how to stop using this deprecated method.
102//
103// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900104func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800105 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800106
Paul Duffin8567f222021-03-23 00:02:06 +0000107 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000108 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
109 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800110}
111
Paul Duffin8567f222021-03-23 00:02:06 +0000112// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000113//
114// See testCc for an explanation as to how to stop using this deprecated method.
115//
116// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900117func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900118 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000119 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900121 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900122 testCcErrorWithConfig(t, pattern, config)
123 return
124}
125
Paul Duffin8567f222021-03-23 00:02:06 +0000126// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000127//
128// See testCc for an explanation as to how to stop using this deprecated method.
129//
130// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900131func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900132 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000133 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900134 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
135 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900136 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137 testCcErrorWithConfig(t, pattern, config)
138 return
139}
140
Logan Chienf3511742017-10-31 18:04:35 +0800141const (
Colin Cross7113d202019-11-20 16:39:12 -0800142 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900143 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
144 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800145 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800146)
147
Paul Duffindb462dd2021-03-21 22:01:55 +0000148// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
149// running it in a fixture that requires all source files to exist.
150func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
151 android.GroupFixturePreparers(
152 PrepareForTestWithCcDefaultModules,
153 android.PrepareForTestDisallowNonExistentPaths,
154 ).RunTest(t)
155}
156
Jiyong Park6a43f042017-10-12 23:05:00 +0900157func TestVendorSrc(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400158 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900159 ctx := testCc(t, `
160 cc_library {
161 name: "libTest",
162 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700163 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800164 nocrt: true,
165 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900166 vendor_available: true,
167 target: {
168 vendor: {
169 srcs: ["bar.c"],
170 },
171 },
172 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900173 `)
174
Logan Chienf3511742017-10-31 18:04:35 +0800175 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900176 var objs []string
177 for _, o := range ld.Inputs {
178 objs = append(objs, o.Base())
179 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800180 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900181 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
182 }
183}
184
Justin Yun7f99ec72021-04-12 13:19:28 +0900185func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
186 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
187 partitionDefined := false
188 checkPartition := func(specific bool, partition string) {
189 if specific {
190 if expected != partition && !partitionDefined {
191 // The variant is installed to the 'partition'
192 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
193 }
194 partitionDefined = true
195 } else {
196 // The variant is not installed to the 'partition'
197 if expected == partition {
198 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
199 }
200 }
201 }
202 socSpecific := func(m *Module) bool {
203 return m.SocSpecific() || m.socSpecificModuleContext()
204 }
205 deviceSpecific := func(m *Module) bool {
206 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
207 }
208 productSpecific := func(m *Module) bool {
209 return m.ProductSpecific() || m.productSpecificModuleContext()
210 }
211 systemExtSpecific := func(m *Module) bool {
212 return m.SystemExtSpecific()
213 }
214 checkPartition(socSpecific(mod), "vendor")
215 checkPartition(deviceSpecific(mod), "odm")
216 checkPartition(productSpecific(mod), "product")
217 checkPartition(systemExtSpecific(mod), "system_ext")
218 if !partitionDefined && expected != "system" {
219 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
220 " but installed to system partition", variant, name, expected)
221 }
222}
223
224func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400225 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900226 t.Helper()
227 ctx := prepareForCcTest.RunTestWithBp(t, `
228 cc_library {
229 name: "libsystem",
230 }
231 cc_library {
232 name: "libsystem_ext",
233 system_ext_specific: true,
234 }
235 cc_library {
236 name: "libproduct",
237 product_specific: true,
238 }
239 cc_library {
240 name: "libvendor",
241 vendor: true,
242 }
243 cc_library {
244 name: "libodm",
245 device_specific: true,
246 }
247 cc_library {
248 name: "liball_available",
249 vendor_available: true,
250 product_available: true,
251 }
252 cc_library {
253 name: "libsystem_ext_all_available",
254 system_ext_specific: true,
255 vendor_available: true,
256 product_available: true,
257 }
258 cc_library {
259 name: "liball_available_odm",
260 odm_available: true,
261 product_available: true,
262 }
263 cc_library {
264 name: "libproduct_vendoravailable",
265 product_specific: true,
266 vendor_available: true,
267 }
268 cc_library {
269 name: "libproduct_odmavailable",
270 product_specific: true,
271 odm_available: true,
272 }
273 `).TestContext
274
275 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
276 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
277 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
278 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
279 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
280
281 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
282 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
283 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
284
285 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
286 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
287 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
288
289 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
290 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
291 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
292
293 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
294 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
295
296 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
297 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
298}
299
Logan Chienf3511742017-10-31 18:04:35 +0800300func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900301 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800302
Logan Chiend3c59a22018-03-29 14:08:15 +0800303 t.Helper()
304
Justin Yun0ecf0b22020-02-28 15:07:59 +0900305 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800306
307 // Check library properties.
308 lib, ok := mod.compiler.(*libraryDecorator)
309 if !ok {
310 t.Errorf("%q must have libraryDecorator", name)
311 } else if lib.baseInstaller.subDir != subDir {
312 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
313 lib.baseInstaller.subDir)
314 }
315
316 // Check VNDK properties.
317 if mod.vndkdep == nil {
318 t.Fatalf("%q must have `vndkdep`", name)
319 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700320 if !mod.IsVndk() {
321 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800322 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400323 if mod.IsVndkSp() != isVndkSp {
324 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800325 }
326
327 // Check VNDK extension properties.
328 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500329 if mod.IsVndkExt() != isVndkExt {
330 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800331 }
332
333 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
334 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
335 }
336}
337
Jooyung Han2216fb12019-11-06 16:46:15 +0900338func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
339 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800340 content := android.ContentFromFileRuleForTests(t, params)
341 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900342 assertArrayString(t, actual, expected)
343}
344
Jooyung Han097087b2019-10-22 19:32:18 +0900345func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
346 t.Helper()
347 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900348 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
349}
350
351func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
352 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800353 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800354 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900355}
356
Logan Chienf3511742017-10-31 18:04:35 +0800357func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400358 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800359 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800360 cc_library {
361 name: "libvndk",
362 vendor_available: true,
363 vndk: {
364 enabled: true,
365 },
366 nocrt: true,
367 }
368
369 cc_library {
370 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900371 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800372 vndk: {
373 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900374 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800375 },
376 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900377 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800378 }
379
380 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900381 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800382 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900383 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800384 vndk: {
385 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900386 },
387 nocrt: true,
388 target: {
389 vendor: {
390 cflags: ["-DTEST"],
391 },
392 product: {
393 cflags: ["-DTEST"],
394 },
395 },
396 }
397
398 cc_library {
399 name: "libvndk_sp",
400 vendor_available: true,
401 vndk: {
402 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800403 support_system_process: true,
404 },
405 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900406 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800407 }
408
409 cc_library {
410 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900411 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800412 vndk: {
413 enabled: true,
414 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900415 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800416 },
417 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900418 target: {
419 vendor: {
420 suffix: "-x",
421 },
422 },
Logan Chienf3511742017-10-31 18:04:35 +0800423 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900424
425 cc_library {
426 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900427 vendor_available: true,
428 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900429 vndk: {
430 enabled: true,
431 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900432 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900433 },
434 nocrt: true,
435 target: {
436 vendor: {
437 suffix: "-x",
438 },
439 product: {
440 suffix: "-x",
441 },
442 },
443 }
444
Justin Yun450ae722021-04-16 19:58:18 +0900445 cc_library {
446 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700447 llndk: {
448 symbol_file: "libllndk.map.txt",
449 export_llndk_headers: ["libllndk_headers"],
450 }
Justin Yun450ae722021-04-16 19:58:18 +0900451 }
452
Justin Yun611e8862021-05-24 18:17:33 +0900453 cc_library {
454 name: "libclang_rt.hwasan-llndk",
455 llndk: {
456 symbol_file: "libclang_rt.hwasan.map.txt",
457 }
458 }
459
Colin Cross627280f2021-04-26 16:53:58 -0700460 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900461 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700462 llndk: {
463 llndk_headers: true,
464 },
Justin Yun450ae722021-04-16 19:58:18 +0900465 export_include_dirs: ["include"],
466 }
467
Colin Crosse4e44bc2020-12-28 13:50:21 -0800468 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900469 name: "llndk.libraries.txt",
470 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800471 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900472 name: "vndkcore.libraries.txt",
473 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800474 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900475 name: "vndksp.libraries.txt",
476 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800477 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900478 name: "vndkprivate.libraries.txt",
479 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800480 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900481 name: "vndkproduct.libraries.txt",
482 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800483 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900484 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800485 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900486 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800487 `
488
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000489 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800490 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900491 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900492 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800493
494 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800495
Jooyung Han261e1582020-10-20 18:54:21 +0900496 // subdir == "" because VNDK libs are not supposed to be installed separately.
497 // They are installed as part of VNDK APEX instead.
498 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
499 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900500 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900501 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
502 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900503 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900504
Justin Yun6977e8a2020-10-29 18:24:11 +0900505 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
506 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900507
Inseob Kim1f086e22019-05-09 13:29:15 +0900508 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900509 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000510 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900511
512 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
513 "arm64", "armv8-a"))
514 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
515 "arm", "armv7-a-neon"))
516
517 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
518 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900519 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
520
Inseob Kim1f086e22019-05-09 13:29:15 +0900521 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
522 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900523 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900524
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900525 variant := "android_vendor.29_arm64_armv8-a_shared"
526 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900527
Inseob Kim7f283f42020-06-01 21:53:49 +0900528 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
529
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400530 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
531 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
532 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
533 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
534 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
535 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
536 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
537 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900538
Jooyung Han39edb6c2019-11-06 16:53:07 +0900539 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800540 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
541 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
542 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
543 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
544 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900545
Jooyung Han097087b2019-10-22 19:32:18 +0900546 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
547 "LLNDK: libc.so",
548 "LLNDK: libdl.so",
549 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900550 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900551 "LLNDK: libm.so",
552 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900553 "VNDK-SP: libvndk_sp-x.so",
554 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900555 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900556 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900557 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900558 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900559 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900560 "VNDK-private: libvndk-private.so",
561 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900562 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900563 "VNDK-product: libc++.so",
564 "VNDK-product: libvndk_product.so",
565 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900566 })
Justin Yun611e8862021-05-24 18:17:33 +0900567 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 +0900568 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
569 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
570 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 +0900571 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 +0900572 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
573}
574
Yo Chiangbba545e2020-06-09 16:15:37 +0800575func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400576 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800577 ctx := testCc(t, `
578 cc_library {
579 name: "libvndk_host_supported",
580 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900581 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800582 vndk: {
583 enabled: true,
584 },
585 host_supported: true,
586 }
587
588 cc_library {
589 name: "libvndk_host_supported_but_disabled_on_device",
590 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900591 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800592 vndk: {
593 enabled: true,
594 },
595 host_supported: true,
596 enabled: false,
597 target: {
598 host: {
599 enabled: true,
600 }
601 }
602 }
603
Colin Crosse4e44bc2020-12-28 13:50:21 -0800604 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800605 name: "vndkcore.libraries.txt",
606 }
607 `)
608
609 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
610}
611
Jooyung Han2216fb12019-11-06 16:46:15 +0900612func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400613 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800614 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800615 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900616 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800617 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800618 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000619 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800620 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900621 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800622 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900623
Colin Cross45bce852021-11-11 22:47:54 -0800624 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700625 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900626 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900627}
628
629func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400630 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800631 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900632 cc_library {
633 name: "libvndk",
634 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900635 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900636 vndk: {
637 enabled: true,
638 },
639 nocrt: true,
640 }
641
642 cc_library {
643 name: "libvndk_sp",
644 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900645 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900646 vndk: {
647 enabled: true,
648 support_system_process: true,
649 },
650 nocrt: true,
651 }
652
653 cc_library {
654 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900655 vendor_available: true,
656 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900657 vndk: {
658 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900659 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900660 },
661 nocrt: true,
662 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900663
Colin Crosse4e44bc2020-12-28 13:50:21 -0800664 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900665 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800666 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900667 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800668 `
669
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000670 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800671 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900672 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800673 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
674
675 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
676
677 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900678
Jooyung Han2216fb12019-11-06 16:46:15 +0900679 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900680}
681
Chris Parsons79d66a52020-06-05 17:26:16 -0400682func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400683 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400684 bp := `
685 cc_test_library {
686 name: "test_lib",
687 srcs: ["test_lib.cpp"],
688 gtest: false,
689 }
690
691 cc_test {
692 name: "main_test",
693 data_libs: ["test_lib"],
694 gtest: false,
695 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400696 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400697
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000698 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400699 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900700 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400701 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
702
703 ctx := testCcWithConfig(t, config)
704 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
705 testBinary := module.(*Module).linker.(*testBinary)
706 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
707 if err != nil {
708 t.Errorf("Expected cc_test to produce output files, error: %s", err)
709 return
710 }
711 if len(outputFiles) != 1 {
712 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
713 return
714 }
715 if len(testBinary.dataPaths()) != 1 {
716 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
717 return
718 }
719
720 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400721 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400722
723 if !strings.HasSuffix(outputPath, "/main_test") {
724 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
725 return
726 }
727 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
728 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
729 return
730 }
731}
732
Chris Parsons216e10a2020-07-09 17:12:52 -0400733func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400734 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400735 bp := `
736 cc_test_library {
737 name: "test_lib",
738 srcs: ["test_lib.cpp"],
739 relative_install_path: "foo/bar/baz",
740 gtest: false,
741 }
742
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400743 cc_binary {
744 name: "test_bin",
745 relative_install_path: "foo/bar/baz",
746 compile_multilib: "both",
747 }
748
Chris Parsons216e10a2020-07-09 17:12:52 -0400749 cc_test {
750 name: "main_test",
751 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400752 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400753 gtest: false,
754 }
755 `
756
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000757 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400758 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900759 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400760 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
761
762 ctx := testCcWithConfig(t, config)
763 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
764 testBinary := module.(*Module).linker.(*testBinary)
765 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
766 if err != nil {
767 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
768 }
769 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400770 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400771 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400772 if len(testBinary.dataPaths()) != 2 {
773 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400774 }
775
776 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400777
778 if !strings.HasSuffix(outputPath, "/main_test") {
779 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
780 }
Colin Crossaa255532020-07-03 13:18:24 -0700781 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400782 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
783 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400784 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400785 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400786 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
787 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
788 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
789 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400790}
791
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000792func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400793 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000794 bp := `
795 cc_test {
796 name: "main_test",
797 srcs: ["main_test.cpp"],
798 test_suites: [
799 "suite_1",
800 "suite_2",
801 ],
802 gtest: false,
803 }
804 `
805
806 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
807 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
808
809 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
810 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
811 if len(compatEntries) != 2 {
812 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
813 }
814 if compatEntries[0] != "suite_1" {
815 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
816 " but was '%s'", compatEntries[0])
817 }
818 if compatEntries[1] != "suite_2" {
819 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
820 " but was '%s'", compatEntries[1])
821 }
822}
823
824func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400825 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000826 bp := `
827 cc_test_library {
828 name: "main_test_lib",
829 srcs: ["main_test_lib.cpp"],
830 test_suites: [
831 "suite_1",
832 "suite_2",
833 ],
834 gtest: false,
835 }
836 `
837
838 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
839 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
840
841 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
842 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
843 if len(compatEntries) != 2 {
844 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
845 }
846 if compatEntries[0] != "suite_1" {
847 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
848 " but was '%s'", compatEntries[0])
849 }
850 if compatEntries[1] != "suite_2" {
851 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
852 " but was '%s'", compatEntries[1])
853 }
854}
855
Jooyung Han0302a842019-10-30 18:43:49 +0900856func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400857 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900858 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900859 cc_library {
860 name: "libvndk",
861 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900862 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900863 vndk: {
864 enabled: true,
865 },
866 nocrt: true,
867 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900868 cc_library {
869 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900870 vendor_available: true,
871 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900872 vndk: {
873 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900874 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900875 },
876 nocrt: true,
877 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800878
879 cc_library {
880 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700881 llndk: {
882 symbol_file: "libllndk.map.txt",
883 export_llndk_headers: ["libllndk_headers"],
884 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800885 }
886
Colin Cross627280f2021-04-26 16:53:58 -0700887 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800888 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700889 llndk: {
890 symbol_file: "libllndk.map.txt",
891 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800892 export_include_dirs: ["include"],
893 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900894 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900895
896 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
897 "LLNDK: libc.so",
898 "LLNDK: libdl.so",
899 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800900 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900901 "LLNDK: libm.so",
902 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900903 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900904 "VNDK-core: libvndk.so",
905 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900906 "VNDK-private: libvndk-private.so",
907 "VNDK-product: libc++.so",
908 "VNDK-product: libvndk-private.so",
909 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900910 })
Logan Chienf3511742017-10-31 18:04:35 +0800911}
912
Justin Yun63e9ec72020-10-29 16:49:43 +0900913func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400914 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900915 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900916 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900917 cc_library {
918 name: "libvndk",
919 vndk: {
920 enabled: true,
921 },
922 nocrt: true,
923 }
924 `)
925
Justin Yunc0d8c492021-01-07 17:45:31 +0900926 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900927 cc_library {
928 name: "libvndk",
929 product_available: true,
930 vndk: {
931 enabled: true,
932 },
933 nocrt: true,
934 }
935 `)
936
Justin Yun6977e8a2020-10-29 18:24:11 +0900937 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
938 cc_library {
939 name: "libvndkprop",
940 vendor_available: true,
941 product_available: true,
942 vndk: {
943 enabled: true,
944 },
945 nocrt: true,
946 target: {
947 vendor: {
948 cflags: ["-DTEST",],
949 },
950 },
951 }
952 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900953}
954
Logan Chiend3c59a22018-03-29 14:08:15 +0800955func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400956 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800957 // Check whether an error is emitted when a VNDK lib depends on a system lib.
958 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
959 cc_library {
960 name: "libvndk",
961 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900962 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800963 vndk: {
964 enabled: true,
965 },
966 shared_libs: ["libfwk"], // Cause error
967 nocrt: true,
968 }
969
970 cc_library {
971 name: "libfwk",
972 nocrt: true,
973 }
974 `)
975
976 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
977 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
978 cc_library {
979 name: "libvndk",
980 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900981 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800982 vndk: {
983 enabled: true,
984 },
985 shared_libs: ["libvendor"], // Cause error
986 nocrt: true,
987 }
988
989 cc_library {
990 name: "libvendor",
991 vendor: true,
992 nocrt: true,
993 }
994 `)
995
996 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
997 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
998 cc_library {
999 name: "libvndk_sp",
1000 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001001 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001002 vndk: {
1003 enabled: true,
1004 support_system_process: true,
1005 },
1006 shared_libs: ["libfwk"], // Cause error
1007 nocrt: true,
1008 }
1009
1010 cc_library {
1011 name: "libfwk",
1012 nocrt: true,
1013 }
1014 `)
1015
1016 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1017 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1018 cc_library {
1019 name: "libvndk_sp",
1020 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001021 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001022 vndk: {
1023 enabled: true,
1024 support_system_process: true,
1025 },
1026 shared_libs: ["libvendor"], // Cause error
1027 nocrt: true,
1028 }
1029
1030 cc_library {
1031 name: "libvendor",
1032 vendor: true,
1033 nocrt: true,
1034 }
1035 `)
1036
1037 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1038 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1039 cc_library {
1040 name: "libvndk_sp",
1041 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001042 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001043 vndk: {
1044 enabled: true,
1045 support_system_process: true,
1046 },
1047 shared_libs: ["libvndk"], // Cause error
1048 nocrt: true,
1049 }
1050
1051 cc_library {
1052 name: "libvndk",
1053 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001054 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001055 vndk: {
1056 enabled: true,
1057 },
1058 nocrt: true,
1059 }
1060 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001061
1062 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1063 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1064 cc_library {
1065 name: "libvndk",
1066 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001067 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001068 vndk: {
1069 enabled: true,
1070 },
1071 shared_libs: ["libnonvndk"],
1072 nocrt: true,
1073 }
1074
1075 cc_library {
1076 name: "libnonvndk",
1077 vendor_available: true,
1078 nocrt: true,
1079 }
1080 `)
1081
1082 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1083 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1084 cc_library {
1085 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001086 vendor_available: true,
1087 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001088 vndk: {
1089 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001090 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001091 },
1092 shared_libs: ["libnonvndk"],
1093 nocrt: true,
1094 }
1095
1096 cc_library {
1097 name: "libnonvndk",
1098 vendor_available: true,
1099 nocrt: true,
1100 }
1101 `)
1102
1103 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1104 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1105 cc_library {
1106 name: "libvndksp",
1107 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001108 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001109 vndk: {
1110 enabled: true,
1111 support_system_process: true,
1112 },
1113 shared_libs: ["libnonvndk"],
1114 nocrt: true,
1115 }
1116
1117 cc_library {
1118 name: "libnonvndk",
1119 vendor_available: true,
1120 nocrt: true,
1121 }
1122 `)
1123
1124 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1125 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1126 cc_library {
1127 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001128 vendor_available: true,
1129 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001130 vndk: {
1131 enabled: true,
1132 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001133 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001134 },
1135 shared_libs: ["libnonvndk"],
1136 nocrt: true,
1137 }
1138
1139 cc_library {
1140 name: "libnonvndk",
1141 vendor_available: true,
1142 nocrt: true,
1143 }
1144 `)
1145}
1146
1147func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001148 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001149 // okay to link : LLNDK -> double_loadable VNDK
1150 testCc(t, `
1151 cc_library {
1152 name: "libllndk",
1153 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001154 llndk: {
1155 symbol_file: "libllndk.map.txt",
1156 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001157 }
1158
1159 cc_library {
1160 name: "libdoubleloadable",
1161 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001162 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001163 vndk: {
1164 enabled: true,
1165 },
1166 double_loadable: true,
1167 }
1168 `)
1169 // okay to link : LLNDK -> VNDK-SP
1170 testCc(t, `
1171 cc_library {
1172 name: "libllndk",
1173 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001174 llndk: {
1175 symbol_file: "libllndk.map.txt",
1176 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001177 }
1178
1179 cc_library {
1180 name: "libvndksp",
1181 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001182 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001183 vndk: {
1184 enabled: true,
1185 support_system_process: true,
1186 },
1187 }
1188 `)
1189 // okay to link : double_loadable -> double_loadable
1190 testCc(t, `
1191 cc_library {
1192 name: "libdoubleloadable1",
1193 shared_libs: ["libdoubleloadable2"],
1194 vendor_available: true,
1195 double_loadable: true,
1196 }
1197
1198 cc_library {
1199 name: "libdoubleloadable2",
1200 vendor_available: true,
1201 double_loadable: true,
1202 }
1203 `)
1204 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1205 testCc(t, `
1206 cc_library {
1207 name: "libdoubleloadable",
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 double_loadable: true,
1214 shared_libs: ["libnondoubleloadable"],
1215 }
1216
1217 cc_library {
1218 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001219 vendor_available: true,
1220 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 vndk: {
1222 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001223 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001224 },
1225 double_loadable: true,
1226 }
1227 `)
1228 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1229 testCc(t, `
1230 cc_library {
1231 name: "libllndk",
1232 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001233 llndk: {
1234 symbol_file: "libllndk.map.txt",
1235 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001236 }
1237
1238 cc_library {
1239 name: "libcoreonly",
1240 shared_libs: ["libvendoravailable"],
1241 }
1242
1243 // indirect dependency of LLNDK
1244 cc_library {
1245 name: "libvendoravailable",
1246 vendor_available: true,
1247 double_loadable: true,
1248 }
1249 `)
1250}
1251
1252func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001253 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001254 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1255 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1256 cc_library {
1257 name: "libllndk",
1258 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001259 llndk: {
1260 symbol_file: "libllndk.map.txt",
1261 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001262 }
1263
1264 cc_library {
1265 name: "libnondoubleloadable",
1266 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001267 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001268 vndk: {
1269 enabled: true,
1270 },
1271 }
1272 `)
1273
1274 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1275 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1276 cc_library {
1277 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001278 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001279 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001280 llndk: {
1281 symbol_file: "libllndk.map.txt",
1282 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001283 }
1284
1285 cc_library {
1286 name: "libnondoubleloadable",
1287 vendor_available: true,
1288 }
1289 `)
1290
Jooyung Hana70f0672019-01-18 15:20:43 +09001291 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1292 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1293 cc_library {
1294 name: "libllndk",
1295 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001296 llndk: {
1297 symbol_file: "libllndk.map.txt",
1298 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001299 }
1300
1301 cc_library {
1302 name: "libcoreonly",
1303 shared_libs: ["libvendoravailable"],
1304 }
1305
1306 // indirect dependency of LLNDK
1307 cc_library {
1308 name: "libvendoravailable",
1309 vendor_available: true,
1310 }
1311 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001312
1313 // The error is not from 'client' but from 'libllndk'
1314 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1315 cc_library {
1316 name: "client",
1317 vendor_available: true,
1318 double_loadable: true,
1319 shared_libs: ["libllndk"],
1320 }
1321 cc_library {
1322 name: "libllndk",
1323 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001324 llndk: {
1325 symbol_file: "libllndk.map.txt",
1326 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001327 }
1328 cc_library {
1329 name: "libnondoubleloadable",
1330 vendor_available: true,
1331 }
1332 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001333}
1334
Jooyung Han479ca172020-10-19 18:51:07 +09001335func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001336 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001337 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1338 cc_library {
1339 name: "libvndksp",
1340 shared_libs: ["libanothervndksp"],
1341 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001342 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001343 vndk: {
1344 enabled: true,
1345 support_system_process: true,
1346 }
1347 }
1348
1349 cc_library {
1350 name: "libllndk",
1351 shared_libs: ["libanothervndksp"],
1352 }
1353
Jooyung Han479ca172020-10-19 18:51:07 +09001354 cc_library {
1355 name: "libanothervndksp",
1356 vendor_available: true,
1357 }
1358 `)
1359}
1360
Logan Chienf3511742017-10-31 18:04:35 +08001361func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001362 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001363 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001364 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001365 cc_library {
1366 name: "libvndk",
1367 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001368 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001369 vndk: {
1370 enabled: true,
1371 },
1372 nocrt: true,
1373 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001374 cc_library {
1375 name: "libvndk2",
1376 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001377 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001378 vndk: {
1379 enabled: true,
1380 },
1381 target: {
1382 vendor: {
1383 suffix: "-suffix",
1384 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001385 product: {
1386 suffix: "-suffix",
1387 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001388 },
1389 nocrt: true,
1390 }
Logan Chienf3511742017-10-31 18:04:35 +08001391
1392 cc_library {
1393 name: "libvndk_ext",
1394 vendor: true,
1395 vndk: {
1396 enabled: true,
1397 extends: "libvndk",
1398 },
1399 nocrt: true,
1400 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001401
1402 cc_library {
1403 name: "libvndk2_ext",
1404 vendor: true,
1405 vndk: {
1406 enabled: true,
1407 extends: "libvndk2",
1408 },
1409 nocrt: true,
1410 }
Logan Chienf3511742017-10-31 18:04:35 +08001411
Justin Yun0ecf0b22020-02-28 15:07:59 +09001412 cc_library {
1413 name: "libvndk_ext_product",
1414 product_specific: true,
1415 vndk: {
1416 enabled: true,
1417 extends: "libvndk",
1418 },
1419 nocrt: true,
1420 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001421
Justin Yun0ecf0b22020-02-28 15:07:59 +09001422 cc_library {
1423 name: "libvndk2_ext_product",
1424 product_specific: true,
1425 vndk: {
1426 enabled: true,
1427 extends: "libvndk2",
1428 },
1429 nocrt: true,
1430 }
1431 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001432 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001433 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1434 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001435 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001436
1437 ctx := testCcWithConfig(t, config)
1438
1439 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1440 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1441
1442 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1443 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1444
1445 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1446 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001447}
1448
Logan Chiend3c59a22018-03-29 14:08:15 +08001449func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001450 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001451 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1452 ctx := testCcNoVndk(t, `
1453 cc_library {
1454 name: "libvndk",
1455 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001456 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001457 vndk: {
1458 enabled: true,
1459 },
1460 nocrt: true,
1461 }
1462
1463 cc_library {
1464 name: "libvndk_ext",
1465 vendor: true,
1466 vndk: {
1467 enabled: true,
1468 extends: "libvndk",
1469 },
1470 nocrt: true,
1471 }
1472 `)
1473
1474 // Ensures that the core variant of "libvndk_ext" can be found.
1475 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1476 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1477 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1478 }
1479}
1480
Justin Yun0ecf0b22020-02-28 15:07:59 +09001481func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001482 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001483 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001484 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001485 cc_library {
1486 name: "libvndk",
1487 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001488 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001489 vndk: {
1490 enabled: true,
1491 },
1492 nocrt: true,
1493 }
1494
1495 cc_library {
1496 name: "libvndk_ext_product",
1497 product_specific: true,
1498 vndk: {
1499 enabled: true,
1500 extends: "libvndk",
1501 },
1502 nocrt: true,
1503 }
1504 `)
1505
1506 // Ensures that the core variant of "libvndk_ext_product" can be found.
1507 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1508 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1509 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1510 }
1511}
1512
Logan Chienf3511742017-10-31 18:04:35 +08001513func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001514 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001515 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001516 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001517 cc_library {
1518 name: "libvndk",
1519 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001520 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001521 vndk: {
1522 enabled: true,
1523 },
1524 nocrt: true,
1525 }
1526
1527 cc_library {
1528 name: "libvndk_ext",
1529 vndk: {
1530 enabled: true,
1531 extends: "libvndk",
1532 },
1533 nocrt: true,
1534 }
1535 `)
1536
1537 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1538 cc_library {
1539 name: "libvndk",
1540 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001541 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001542 vndk: {
1543 enabled: true,
1544 },
1545 nocrt: true,
1546 }
1547
1548 cc_library {
1549 name: "libvndk_ext",
1550 vendor: true,
1551 vndk: {
1552 enabled: true,
1553 },
1554 nocrt: true,
1555 }
1556 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001557
1558 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1559 cc_library {
1560 name: "libvndk",
1561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001562 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001563 vndk: {
1564 enabled: true,
1565 },
1566 nocrt: true,
1567 }
1568
1569 cc_library {
1570 name: "libvndk_ext_product",
1571 product_specific: true,
1572 vndk: {
1573 enabled: true,
1574 },
1575 nocrt: true,
1576 }
1577 `)
1578
1579 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1580 cc_library {
1581 name: "libvndk",
1582 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001583 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001584 vndk: {
1585 enabled: true,
1586 },
1587 nocrt: true,
1588 }
1589
1590 cc_library {
1591 name: "libvndk_ext_product",
1592 product_specific: true,
1593 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001594 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001595 vndk: {
1596 enabled: true,
1597 extends: "libvndk",
1598 },
1599 nocrt: true,
1600 }
1601 `)
Logan Chienf3511742017-10-31 18:04:35 +08001602}
1603
1604func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001605 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001606 // This test ensures an error is emitted for inconsistent support_system_process.
1607 testCcError(t, "module \".*\" with mismatched support_system_process", `
1608 cc_library {
1609 name: "libvndk",
1610 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001611 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001612 vndk: {
1613 enabled: 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",
1624 support_system_process: true,
1625 },
1626 nocrt: true,
1627 }
1628 `)
1629
1630 testCcError(t, "module \".*\" with mismatched support_system_process", `
1631 cc_library {
1632 name: "libvndk_sp",
1633 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001634 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001635 vndk: {
1636 enabled: true,
1637 support_system_process: true,
1638 },
1639 nocrt: true,
1640 }
1641
1642 cc_library {
1643 name: "libvndk_ext",
1644 vendor: true,
1645 vndk: {
1646 enabled: true,
1647 extends: "libvndk_sp",
1648 },
1649 nocrt: true,
1650 }
1651 `)
1652}
1653
1654func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001655 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001656 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001657 // with `private: true`.
1658 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001659 cc_library {
1660 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001661 vendor_available: true,
1662 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001663 vndk: {
1664 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001665 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001666 },
1667 nocrt: true,
1668 }
1669
1670 cc_library {
1671 name: "libvndk_ext",
1672 vendor: true,
1673 vndk: {
1674 enabled: true,
1675 extends: "libvndk",
1676 },
1677 nocrt: true,
1678 }
1679 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001680
Justin Yunfd9e8042020-12-23 18:23:14 +09001681 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001682 cc_library {
1683 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001684 vendor_available: true,
1685 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001686 vndk: {
1687 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001688 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001689 },
1690 nocrt: true,
1691 }
1692
1693 cc_library {
1694 name: "libvndk_ext_product",
1695 product_specific: true,
1696 vndk: {
1697 enabled: true,
1698 extends: "libvndk",
1699 },
1700 nocrt: true,
1701 }
1702 `)
Logan Chienf3511742017-10-31 18:04:35 +08001703}
1704
Logan Chiend3c59a22018-03-29 14:08:15 +08001705func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001706 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001707 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001708 testCc(t, `
1709 cc_library {
1710 name: "libvndk",
1711 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001712 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001713 vndk: {
1714 enabled: true,
1715 },
1716 nocrt: true,
1717 }
1718
1719 cc_library {
1720 name: "libvndk_ext",
1721 vendor: true,
1722 vndk: {
1723 enabled: true,
1724 extends: "libvndk",
1725 },
1726 nocrt: true,
1727 }
1728
1729 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001730 name: "libvndk_sp",
1731 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001732 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001733 vndk: {
1734 enabled: true,
1735 support_system_process: true,
1736 },
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_sp_ext",
1742 vendor: true,
1743 vndk: {
1744 enabled: true,
1745 extends: "libvndk_sp",
1746 support_system_process: true,
1747 },
1748 nocrt: true,
1749 }
1750
1751 cc_library {
1752 name: "libvendor",
1753 vendor: true,
1754 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1755 nocrt: true,
1756 }
1757 `)
1758}
1759
Logan Chiend3c59a22018-03-29 14:08:15 +08001760func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001761 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001762 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001763 testCc(t, `
1764 cc_library {
1765 name: "libvndk",
1766 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001767 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001768 vndk: {
1769 enabled: true,
1770 },
1771 nocrt: true,
1772 }
1773
1774 cc_library {
1775 name: "libvndk_ext",
1776 vendor: true,
1777 vndk: {
1778 enabled: true,
1779 extends: "libvndk",
1780 },
1781 shared_libs: ["libvendor"],
1782 nocrt: true,
1783 }
1784
1785 cc_library {
1786 name: "libvendor",
1787 vendor: true,
1788 nocrt: true,
1789 }
1790 `)
Logan Chienf3511742017-10-31 18:04:35 +08001791
Logan Chiend3c59a22018-03-29 14:08:15 +08001792 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1793 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001794 cc_library {
1795 name: "libvndk_sp",
1796 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001797 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001798 vndk: {
1799 enabled: true,
1800 support_system_process: true,
1801 },
1802 nocrt: true,
1803 }
1804
1805 cc_library {
1806 name: "libvndk_sp_ext",
1807 vendor: true,
1808 vndk: {
1809 enabled: true,
1810 extends: "libvndk_sp",
1811 support_system_process: true,
1812 },
1813 shared_libs: ["libvendor"], // Cause an error
1814 nocrt: true,
1815 }
1816
1817 cc_library {
1818 name: "libvendor",
1819 vendor: true,
1820 nocrt: true,
1821 }
1822 `)
1823}
1824
Justin Yun0ecf0b22020-02-28 15:07:59 +09001825func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001826 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001827 bp := `
1828 cc_library {
1829 name: "libvndk",
1830 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001831 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001832 vndk: {
1833 enabled: true,
1834 },
1835 nocrt: true,
1836 }
1837
1838 cc_library {
1839 name: "libvndk_ext_product",
1840 product_specific: true,
1841 vndk: {
1842 enabled: true,
1843 extends: "libvndk",
1844 },
1845 shared_libs: ["libproduct_for_vndklibs"],
1846 nocrt: true,
1847 }
1848
1849 cc_library {
1850 name: "libvndk_sp",
1851 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001852 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001853 vndk: {
1854 enabled: true,
1855 support_system_process: true,
1856 },
1857 nocrt: true,
1858 }
1859
1860 cc_library {
1861 name: "libvndk_sp_ext_product",
1862 product_specific: true,
1863 vndk: {
1864 enabled: true,
1865 extends: "libvndk_sp",
1866 support_system_process: true,
1867 },
1868 shared_libs: ["libproduct_for_vndklibs"],
1869 nocrt: true,
1870 }
1871
1872 cc_library {
1873 name: "libproduct",
1874 product_specific: true,
1875 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1876 nocrt: true,
1877 }
1878
1879 cc_library {
1880 name: "libproduct_for_vndklibs",
1881 product_specific: true,
1882 nocrt: true,
1883 }
1884 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001885 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001886 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1887 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001888 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001889
1890 testCcWithConfig(t, config)
1891}
1892
Logan Chiend3c59a22018-03-29 14:08:15 +08001893func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001894 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001895 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1896 // library.
1897 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1898 cc_library {
1899 name: "libvndk",
1900 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001901 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001902 vndk: {
1903 enabled: true,
1904 },
1905 nocrt: true,
1906 }
1907
1908 cc_library {
1909 name: "libvndk_sp",
1910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001911 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001912 vndk: {
1913 enabled: true,
1914 support_system_process: true,
1915 },
1916 nocrt: true,
1917 }
1918
1919 cc_library {
1920 name: "libvndk_sp_ext",
1921 vendor: true,
1922 vndk: {
1923 enabled: true,
1924 extends: "libvndk_sp",
1925 support_system_process: true,
1926 },
1927 shared_libs: ["libvndk"], // Cause an error
1928 nocrt: true,
1929 }
1930 `)
1931
1932 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1933 // library.
1934 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1935 cc_library {
1936 name: "libvndk",
1937 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001938 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001939 vndk: {
1940 enabled: true,
1941 },
1942 nocrt: true,
1943 }
1944
1945 cc_library {
1946 name: "libvndk_ext",
1947 vendor: true,
1948 vndk: {
1949 enabled: true,
1950 extends: "libvndk",
1951 },
1952 nocrt: true,
1953 }
1954
1955 cc_library {
1956 name: "libvndk_sp",
1957 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001958 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001959 vndk: {
1960 enabled: true,
1961 support_system_process: true,
1962 },
1963 nocrt: true,
1964 }
1965
1966 cc_library {
1967 name: "libvndk_sp_ext",
1968 vendor: true,
1969 vndk: {
1970 enabled: true,
1971 extends: "libvndk_sp",
1972 support_system_process: true,
1973 },
1974 shared_libs: ["libvndk_ext"], // Cause an error
1975 nocrt: true,
1976 }
1977 `)
1978}
1979
1980func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001981 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001982 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1983 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001984 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1985 cc_library {
1986 name: "libvndk",
1987 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001988 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001989 vndk: {
1990 enabled: true,
1991 },
1992 nocrt: true,
1993 }
1994
1995 cc_library {
1996 name: "libvndk_ext",
1997 vendor: true,
1998 vndk: {
1999 enabled: true,
2000 extends: "libvndk",
2001 },
2002 nocrt: true,
2003 }
2004
2005 cc_library {
2006 name: "libvndk2",
2007 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002008 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002009 vndk: {
2010 enabled: true,
2011 },
2012 shared_libs: ["libvndk_ext"],
2013 nocrt: true,
2014 }
2015 `)
2016
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002017 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002018 cc_library {
2019 name: "libvndk",
2020 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002021 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002022 vndk: {
2023 enabled: true,
2024 },
2025 nocrt: true,
2026 }
2027
2028 cc_library {
2029 name: "libvndk_ext",
2030 vendor: true,
2031 vndk: {
2032 enabled: true,
2033 extends: "libvndk",
2034 },
2035 nocrt: true,
2036 }
2037
2038 cc_library {
2039 name: "libvndk2",
2040 vendor_available: true,
2041 vndk: {
2042 enabled: true,
2043 },
2044 target: {
2045 vendor: {
2046 shared_libs: ["libvndk_ext"],
2047 },
2048 },
2049 nocrt: true,
2050 }
2051 `)
2052
2053 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2054 cc_library {
2055 name: "libvndk_sp",
2056 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002057 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002058 vndk: {
2059 enabled: true,
2060 support_system_process: true,
2061 },
2062 nocrt: true,
2063 }
2064
2065 cc_library {
2066 name: "libvndk_sp_ext",
2067 vendor: true,
2068 vndk: {
2069 enabled: true,
2070 extends: "libvndk_sp",
2071 support_system_process: true,
2072 },
2073 nocrt: true,
2074 }
2075
2076 cc_library {
2077 name: "libvndk_sp_2",
2078 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002079 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002080 vndk: {
2081 enabled: true,
2082 support_system_process: true,
2083 },
2084 shared_libs: ["libvndk_sp_ext"],
2085 nocrt: true,
2086 }
2087 `)
2088
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002089 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002090 cc_library {
2091 name: "libvndk_sp",
2092 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002093 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002094 vndk: {
2095 enabled: true,
2096 },
2097 nocrt: true,
2098 }
2099
2100 cc_library {
2101 name: "libvndk_sp_ext",
2102 vendor: true,
2103 vndk: {
2104 enabled: true,
2105 extends: "libvndk_sp",
2106 },
2107 nocrt: true,
2108 }
2109
2110 cc_library {
2111 name: "libvndk_sp2",
2112 vendor_available: true,
2113 vndk: {
2114 enabled: true,
2115 },
2116 target: {
2117 vendor: {
2118 shared_libs: ["libvndk_sp_ext"],
2119 },
2120 },
2121 nocrt: true,
2122 }
2123 `)
2124}
2125
Justin Yun5f7f7e82019-11-18 19:52:14 +09002126func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002127 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002128 bp := `
2129 cc_library {
2130 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002131 llndk: {
2132 symbol_file: "libllndk.map.txt",
2133 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002134 }
2135 cc_library {
2136 name: "libvndk",
2137 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002138 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002139 vndk: {
2140 enabled: true,
2141 },
2142 nocrt: true,
2143 }
2144 cc_library {
2145 name: "libvndk_sp",
2146 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002147 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002148 vndk: {
2149 enabled: true,
2150 support_system_process: true,
2151 },
2152 nocrt: true,
2153 }
2154 cc_library {
2155 name: "libva",
2156 vendor_available: true,
2157 nocrt: true,
2158 }
2159 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002160 name: "libpa",
2161 product_available: true,
2162 nocrt: true,
2163 }
2164 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002165 name: "libboth_available",
2166 vendor_available: true,
2167 product_available: true,
2168 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002169 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002170 target: {
2171 vendor: {
2172 suffix: "-vendor",
2173 },
2174 product: {
2175 suffix: "-product",
2176 },
2177 }
2178 }
2179 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002180 name: "libproduct_va",
2181 product_specific: true,
2182 vendor_available: true,
2183 nocrt: true,
2184 }
2185 cc_library {
2186 name: "libprod",
2187 product_specific: true,
2188 shared_libs: [
2189 "libllndk",
2190 "libvndk",
2191 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002192 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002193 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002194 "libproduct_va",
2195 ],
2196 nocrt: true,
2197 }
2198 cc_library {
2199 name: "libvendor",
2200 vendor: true,
2201 shared_libs: [
2202 "libllndk",
2203 "libvndk",
2204 "libvndk_sp",
2205 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002206 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002207 "libproduct_va",
2208 ],
2209 nocrt: true,
2210 }
2211 `
2212
Paul Duffin8567f222021-03-23 00:02:06 +00002213 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002214
Jooyung Han261e1582020-10-20 18:54:21 +09002215 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2216 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002217
2218 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2219 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2220
2221 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2222 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002223
2224 ensureStringContains := func(t *testing.T, str string, substr string) {
2225 t.Helper()
2226 if !strings.Contains(str, substr) {
2227 t.Errorf("%q is not found in %v", substr, str)
2228 }
2229 }
2230 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2231 t.Helper()
2232 if strings.Contains(str, substr) {
2233 t.Errorf("%q is found in %v", substr, str)
2234 }
2235 }
2236
2237 // _static variant is used since _shared reuses *.o from the static variant
2238 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2239 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2240
2241 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2242 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2243 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2244 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2245
2246 product_cflags := product_static.Rule("cc").Args["cFlags"]
2247 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2248 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2249 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002250}
2251
2252func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002253 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002254 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002255 cc_library {
2256 name: "libprod",
2257 product_specific: true,
2258 shared_libs: [
2259 "libvendor",
2260 ],
2261 nocrt: true,
2262 }
2263 cc_library {
2264 name: "libvendor",
2265 vendor: true,
2266 nocrt: true,
2267 }
2268 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002269 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002270 cc_library {
2271 name: "libprod",
2272 product_specific: true,
2273 shared_libs: [
2274 "libsystem",
2275 ],
2276 nocrt: true,
2277 }
2278 cc_library {
2279 name: "libsystem",
2280 nocrt: true,
2281 }
2282 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002283 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002284 cc_library {
2285 name: "libprod",
2286 product_specific: true,
2287 shared_libs: [
2288 "libva",
2289 ],
2290 nocrt: true,
2291 }
2292 cc_library {
2293 name: "libva",
2294 vendor_available: true,
2295 nocrt: true,
2296 }
2297 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002298 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002299 cc_library {
2300 name: "libprod",
2301 product_specific: true,
2302 shared_libs: [
2303 "libvndk_private",
2304 ],
2305 nocrt: true,
2306 }
2307 cc_library {
2308 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002309 vendor_available: true,
2310 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002311 vndk: {
2312 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002313 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002314 },
2315 nocrt: true,
2316 }
2317 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002318 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002319 cc_library {
2320 name: "libprod",
2321 product_specific: true,
2322 shared_libs: [
2323 "libsystem_ext",
2324 ],
2325 nocrt: true,
2326 }
2327 cc_library {
2328 name: "libsystem_ext",
2329 system_ext_specific: true,
2330 nocrt: true,
2331 }
2332 `)
2333 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2334 cc_library {
2335 name: "libsystem",
2336 shared_libs: [
2337 "libproduct_va",
2338 ],
2339 nocrt: true,
2340 }
2341 cc_library {
2342 name: "libproduct_va",
2343 product_specific: true,
2344 vendor_available: true,
2345 nocrt: true,
2346 }
2347 `)
2348}
2349
Jooyung Han38002912019-05-16 04:01:54 +09002350func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002351 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002352 bp := `
2353 cc_library {
2354 name: "libvndk",
2355 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002356 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002357 vndk: {
2358 enabled: true,
2359 },
2360 }
2361 cc_library {
2362 name: "libvndksp",
2363 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002364 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002365 vndk: {
2366 enabled: true,
2367 support_system_process: true,
2368 },
2369 }
2370 cc_library {
2371 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002372 vendor_available: true,
2373 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002374 vndk: {
2375 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002376 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002377 },
2378 }
2379 cc_library {
2380 name: "libvendor",
2381 vendor: true,
2382 }
2383 cc_library {
2384 name: "libvndkext",
2385 vendor: true,
2386 vndk: {
2387 enabled: true,
2388 extends: "libvndk",
2389 },
2390 }
2391 vndk_prebuilt_shared {
2392 name: "prevndk",
2393 version: "27",
2394 target_arch: "arm",
2395 binder32bit: true,
2396 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002397 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002398 vndk: {
2399 enabled: true,
2400 },
2401 arch: {
2402 arm: {
2403 srcs: ["liba.so"],
2404 },
2405 },
2406 }
2407 cc_library {
2408 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002409 llndk: {
2410 symbol_file: "libllndk.map.txt",
2411 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002412 }
2413 cc_library {
2414 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002415 llndk: {
2416 symbol_file: "libllndkprivate.map.txt",
2417 private: true,
2418 }
Colin Cross78212242021-01-06 14:51:30 -08002419 }
2420
2421 llndk_libraries_txt {
2422 name: "llndk.libraries.txt",
2423 }
2424 vndkcore_libraries_txt {
2425 name: "vndkcore.libraries.txt",
2426 }
2427 vndksp_libraries_txt {
2428 name: "vndksp.libraries.txt",
2429 }
2430 vndkprivate_libraries_txt {
2431 name: "vndkprivate.libraries.txt",
2432 }
2433 vndkcorevariant_libraries_txt {
2434 name: "vndkcorevariant.libraries.txt",
2435 insert_vndk_version: false,
2436 }
2437 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002438
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002439 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002440 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002441 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002442 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002443 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002444
Colin Cross78212242021-01-06 14:51:30 -08002445 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2446 []string{"libvndk.so", "libvndkprivate.so"})
2447 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2448 []string{"libc++.so", "libvndksp.so"})
2449 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2450 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2451 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2452 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002453
Colin Crossfb0c16e2019-11-20 17:12:35 -08002454 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002455
Jooyung Han38002912019-05-16 04:01:54 +09002456 tests := []struct {
2457 variant string
2458 name string
2459 expected string
2460 }{
2461 {vendorVariant, "libvndk", "native:vndk"},
2462 {vendorVariant, "libvndksp", "native:vndk"},
2463 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2464 {vendorVariant, "libvendor", "native:vendor"},
2465 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002466 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002467 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002468 {coreVariant, "libvndk", "native:platform"},
2469 {coreVariant, "libvndkprivate", "native:platform"},
2470 {coreVariant, "libllndk", "native:platform"},
2471 }
2472 for _, test := range tests {
2473 t.Run(test.name, func(t *testing.T) {
2474 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2475 assertString(t, module.makeLinkType, test.expected)
2476 })
2477 }
2478}
2479
Jeff Gaston294356f2017-09-27 17:05:30 -07002480var staticLinkDepOrderTestCases = []struct {
2481 // This is a string representation of a map[moduleName][]moduleDependency .
2482 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002483 inStatic string
2484
2485 // This is a string representation of a map[moduleName][]moduleDependency .
2486 // It models the dependencies declared in an Android.bp file.
2487 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002488
2489 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2490 // The keys of allOrdered specify which modules we would like to check.
2491 // The values of allOrdered specify the expected result (of the transitive closure of all
2492 // dependencies) for each module to test
2493 allOrdered string
2494
2495 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2496 // The keys of outOrdered specify which modules we would like to check.
2497 // The values of outOrdered specify the expected result (of the ordered linker command line)
2498 // for each module to test.
2499 outOrdered string
2500}{
2501 // Simple tests
2502 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002503 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002504 outOrdered: "",
2505 },
2506 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002507 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002508 outOrdered: "a:",
2509 },
2510 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002512 outOrdered: "a:b; b:",
2513 },
2514 // Tests of reordering
2515 {
2516 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002517 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002518 outOrdered: "a:b,c,d; b:d; c:d; d:",
2519 },
2520 {
2521 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002522 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002523 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2524 },
2525 {
2526 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002527 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002528 outOrdered: "a:d,b,e,c; d:b; e:c",
2529 },
2530 {
2531 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002532 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002533 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2534 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2535 },
2536 {
2537 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002538 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 -07002539 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2540 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2541 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002542 // shared dependencies
2543 {
2544 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2545 // So, we don't actually have to check that a shared dependency of c will change the order
2546 // of a library that depends statically on b and on c. We only need to check that if c has
2547 // a shared dependency on b, that that shows up in allOrdered.
2548 inShared: "c:b",
2549 allOrdered: "c:b",
2550 outOrdered: "c:",
2551 },
2552 {
2553 // This test doesn't actually include any shared dependencies but it's a reminder of what
2554 // the second phase of the above test would look like
2555 inStatic: "a:b,c; c:b",
2556 allOrdered: "a:c,b; c:b",
2557 outOrdered: "a:c,b; c:b",
2558 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002559 // tiebreakers for when two modules specifying different orderings and there is no dependency
2560 // to dictate an order
2561 {
2562 // 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 -08002563 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002564 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2565 },
2566 {
2567 // 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 -08002568 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 -07002569 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2570 },
2571 // Tests involving duplicate dependencies
2572 {
2573 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002574 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002575 outOrdered: "a:c,b",
2576 },
2577 {
2578 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002579 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002580 outOrdered: "a:d,c,b",
2581 },
2582 // Tests to confirm the nonexistence of infinite loops.
2583 // These cases should never happen, so as long as the test terminates and the
2584 // result is deterministic then that should be fine.
2585 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002586 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002587 outOrdered: "a:a",
2588 },
2589 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002590 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002591 allOrdered: "a:b,c; b:c,a; c:a,b",
2592 outOrdered: "a:b; b:c; c:a",
2593 },
2594 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002595 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002596 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2597 outOrdered: "a:c,b; b:a,c; c:b,a",
2598 },
2599}
2600
2601// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2602func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2603 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2604 strippedText := strings.Replace(text, " ", "", -1)
2605 if len(strippedText) < 1 {
2606 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2607 }
2608 allDeps = make(map[android.Path][]android.Path, 0)
2609
2610 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2611 moduleTexts := strings.Split(strippedText, ";")
2612
2613 outputForModuleName := func(moduleName string) android.Path {
2614 return android.PathForTesting(moduleName)
2615 }
2616
2617 for _, moduleText := range moduleTexts {
2618 // convert from "a:b,c" to ["a", "b,c"]
2619 components := strings.Split(moduleText, ":")
2620 if len(components) != 2 {
2621 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2622 }
2623 moduleName := components[0]
2624 moduleOutput := outputForModuleName(moduleName)
2625 modulesInOrder = append(modulesInOrder, moduleOutput)
2626
2627 depString := components[1]
2628 // convert from "b,c" to ["b", "c"]
2629 depNames := strings.Split(depString, ",")
2630 if len(depString) < 1 {
2631 depNames = []string{}
2632 }
2633 var deps []android.Path
2634 for _, depName := range depNames {
2635 deps = append(deps, outputForModuleName(depName))
2636 }
2637 allDeps[moduleOutput] = deps
2638 }
2639 return modulesInOrder, allDeps
2640}
2641
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002642func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002643 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002644 ctx := testCc(t, `
2645 cc_library {
2646 name: "a",
2647 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002648 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002649 }
2650 cc_library {
2651 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002652 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002653 }
2654 cc_library {
2655 name: "c",
2656 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002657 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002658 }
2659 cc_library {
2660 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002661 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002662 }
2663
2664 `)
2665
Colin Cross7113d202019-11-20 16:39:12 -08002666 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002667 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002668 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2669 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002670 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002671
2672 if !reflect.DeepEqual(actual, expected) {
2673 t.Errorf("staticDeps orderings were not propagated correctly"+
2674 "\nactual: %v"+
2675 "\nexpected: %v",
2676 actual,
2677 expected,
2678 )
2679 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002680}
Jeff Gaston294356f2017-09-27 17:05:30 -07002681
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002682func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002683 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002684 ctx := testCc(t, `
2685 cc_library {
2686 name: "a",
2687 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002688 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002689 }
2690 cc_library {
2691 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002692 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002693 }
2694 cc_library {
2695 name: "c",
2696 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002697 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002698 }
2699
2700 `)
2701
Colin Cross7113d202019-11-20 16:39:12 -08002702 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002703 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002704 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2705 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002706 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002707
2708 if !reflect.DeepEqual(actual, expected) {
2709 t.Errorf("staticDeps orderings did not account for shared libs"+
2710 "\nactual: %v"+
2711 "\nexpected: %v",
2712 actual,
2713 expected,
2714 )
2715 }
2716}
2717
Jooyung Hanb04a4992020-03-13 18:57:35 +09002718func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002719 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002720 if !reflect.DeepEqual(actual, expected) {
2721 t.Errorf(message+
2722 "\nactual: %v"+
2723 "\nexpected: %v",
2724 actual,
2725 expected,
2726 )
2727 }
2728}
2729
Jooyung Han61b66e92020-03-21 14:21:46 +00002730func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002731 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002732 result := prepareForCcTest.RunTestWithBp(t, `
2733 cc_library {
2734 name: "libllndk",
2735 stubs: { versions: ["1", "2"] },
2736 llndk: {
2737 symbol_file: "libllndk.map.txt",
2738 },
2739 export_include_dirs: ["include"],
2740 }
2741
2742 cc_prebuilt_library_shared {
2743 name: "libllndkprebuilt",
2744 stubs: { versions: ["1", "2"] },
2745 llndk: {
2746 symbol_file: "libllndkprebuilt.map.txt",
2747 },
2748 }
2749
2750 cc_library {
2751 name: "libllndk_with_external_headers",
2752 stubs: { versions: ["1", "2"] },
2753 llndk: {
2754 symbol_file: "libllndk.map.txt",
2755 export_llndk_headers: ["libexternal_llndk_headers"],
2756 },
2757 header_libs: ["libexternal_headers"],
2758 export_header_lib_headers: ["libexternal_headers"],
2759 }
2760 cc_library_headers {
2761 name: "libexternal_headers",
2762 export_include_dirs: ["include"],
2763 vendor_available: true,
2764 }
2765 cc_library_headers {
2766 name: "libexternal_llndk_headers",
2767 export_include_dirs: ["include_llndk"],
2768 llndk: {
2769 symbol_file: "libllndk.map.txt",
2770 },
2771 vendor_available: true,
2772 }
2773
2774 cc_library {
2775 name: "libllndk_with_override_headers",
2776 stubs: { versions: ["1", "2"] },
2777 llndk: {
2778 symbol_file: "libllndk.map.txt",
2779 override_export_include_dirs: ["include_llndk"],
2780 },
2781 export_include_dirs: ["include"],
2782 }
2783 `)
2784 actual := result.ModuleVariantsForTests("libllndk")
2785 for i := 0; i < len(actual); i++ {
2786 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2787 actual = append(actual[:i], actual[i+1:]...)
2788 i--
2789 }
2790 }
2791 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002792 "android_vendor.29_arm64_armv8-a_shared_current",
2793 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002794 "android_vendor.29_arm_armv7-a-neon_shared_current",
2795 "android_vendor.29_arm_armv7-a-neon_shared",
2796 }
2797 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2798
2799 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2800 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2801
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002802 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2803 t.Helper()
2804 m := result.ModuleForTests(module, variant).Module()
2805 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2806 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2807 expectedDirs, f.IncludeDirs)
2808 }
2809
2810 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2811 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2812 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2813 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2814 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2815 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2816}
2817
Jiyong Parka46a4d52017-12-14 19:54:34 +09002818func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002819 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002820 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002821 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002822 name: "libllndk_headers",
2823 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002824 llndk: {
2825 llndk_headers: true,
2826 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002827 }
2828 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002829 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002830 llndk: {
2831 symbol_file: "libllndk.map.txt",
2832 export_llndk_headers: ["libllndk_headers"],
2833 }
Colin Cross0477b422020-10-13 18:43:54 -07002834 }
2835
2836 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002837 name: "libvendor",
2838 shared_libs: ["libllndk"],
2839 vendor: true,
2840 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002841 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002842 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002843 }
2844 `)
2845
2846 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002847 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002848 cflags := cc.Args["cFlags"]
2849 if !strings.Contains(cflags, "-Imy_include") {
2850 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2851 }
2852}
2853
Logan Chien43d34c32017-12-20 01:17:32 +08002854func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2855 actual := module.Properties.AndroidMkRuntimeLibs
2856 if !reflect.DeepEqual(actual, expected) {
2857 t.Errorf("incorrect runtime_libs for shared libs"+
2858 "\nactual: %v"+
2859 "\nexpected: %v",
2860 actual,
2861 expected,
2862 )
2863 }
2864}
2865
2866const runtimeLibAndroidBp = `
2867 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002868 name: "liball_available",
2869 vendor_available: true,
2870 product_available: true,
2871 no_libcrt : true,
2872 nocrt : true,
2873 system_shared_libs : [],
2874 }
2875 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002876 name: "libvendor_available1",
2877 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002878 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002879 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002880 nocrt : true,
2881 system_shared_libs : [],
2882 }
2883 cc_library {
2884 name: "libvendor_available2",
2885 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002886 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002887 target: {
2888 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002889 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002890 }
2891 },
Yi Konge7fe9912019-06-02 00:53:50 -07002892 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002893 nocrt : true,
2894 system_shared_libs : [],
2895 }
2896 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002897 name: "libproduct_vendor",
2898 product_specific: true,
2899 vendor_available: true,
2900 no_libcrt : true,
2901 nocrt : true,
2902 system_shared_libs : [],
2903 }
2904 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002905 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002906 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002907 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002908 nocrt : true,
2909 system_shared_libs : [],
2910 }
2911 cc_library {
2912 name: "libvendor1",
2913 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002914 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002915 nocrt : true,
2916 system_shared_libs : [],
2917 }
2918 cc_library {
2919 name: "libvendor2",
2920 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002921 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002922 no_libcrt : true,
2923 nocrt : true,
2924 system_shared_libs : [],
2925 }
2926 cc_library {
2927 name: "libproduct_available1",
2928 product_available: true,
2929 runtime_libs: ["liball_available"],
2930 no_libcrt : true,
2931 nocrt : true,
2932 system_shared_libs : [],
2933 }
2934 cc_library {
2935 name: "libproduct1",
2936 product_specific: true,
2937 no_libcrt : true,
2938 nocrt : true,
2939 system_shared_libs : [],
2940 }
2941 cc_library {
2942 name: "libproduct2",
2943 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002944 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002945 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002946 nocrt : true,
2947 system_shared_libs : [],
2948 }
2949`
2950
2951func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002952 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002953 ctx := testCc(t, runtimeLibAndroidBp)
2954
2955 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002956 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002957
Justin Yun8a2600c2020-12-07 12:44:03 +09002958 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2959 checkRuntimeLibs(t, []string{"liball_available"}, module)
2960
2961 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2962 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002963
2964 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002965 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002966
2967 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2968 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002969 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002970
Justin Yun8a2600c2020-12-07 12:44:03 +09002971 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2972 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002973
2974 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002975 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002976
2977 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2978 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002979 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002980
2981 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2982 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2983
2984 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002985 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002986}
2987
2988func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002989 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002990 ctx := testCc(t, runtimeLibAndroidBp)
2991
Colin Cross7113d202019-11-20 16:39:12 -08002992 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002993 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2994 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002995
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002996 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002997 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002998 checkRuntimeLibs(t, nil, module)
2999}
3000
3001func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003002 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003003 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3004
3005 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3006
Colin Cross7113d202019-11-20 16:39:12 -08003007 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003008
Justin Yun8a2600c2020-12-07 12:44:03 +09003009 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3010 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003011
3012 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003013 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003014
3015 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003016 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003017}
3018
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003019func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003020 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003021 actual := module.Properties.AndroidMkStaticLibs
3022 if !reflect.DeepEqual(actual, expected) {
3023 t.Errorf("incorrect static_libs"+
3024 "\nactual: %v"+
3025 "\nexpected: %v",
3026 actual,
3027 expected,
3028 )
3029 }
3030}
3031
3032const staticLibAndroidBp = `
3033 cc_library {
3034 name: "lib1",
3035 }
3036 cc_library {
3037 name: "lib2",
3038 static_libs: ["lib1"],
3039 }
3040`
3041
3042func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003043 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003044 ctx := testCc(t, staticLibAndroidBp)
3045
3046 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003047 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003048 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003049 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003050
3051 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003052 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003053 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3054 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003055 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003056}
3057
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003058func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3059 bp := `
3060 cc_library {
3061 name: "static_dep",
3062 }
3063 cc_library {
3064 name: "whole_static_dep",
3065 }
3066 cc_library {
3067 name: "shared_dep",
3068 }
3069 cc_library {
3070 name: "lib",
3071 bazel_module: { label: "//:lib" },
3072 static_libs: ["static_dep"],
3073 whole_static_libs: ["whole_static_dep"],
3074 shared_libs: ["shared_dep"],
3075 }
3076 cc_test {
3077 name: "test",
3078 bazel_module: { label: "//:test" },
3079 static_libs: ["static_dep"],
3080 whole_static_libs: ["whole_static_dep"],
3081 shared_libs: ["shared_dep"],
3082 gtest: false,
3083 }
3084 cc_binary {
3085 name: "binary",
3086 bazel_module: { label: "//:binary" },
3087 static_libs: ["static_dep"],
3088 whole_static_libs: ["whole_static_dep"],
3089 shared_libs: ["shared_dep"],
3090 }
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003091 cc_library_headers {
3092 name: "lib_headers",
3093 bazel_module: { label: "//:lib_headers" },
3094 static_libs: ["static_dep"],
3095 whole_static_libs: ["whole_static_dep"],
3096 shared_libs: ["shared_dep"],
3097 }
3098 cc_prebuilt_library {
3099 name: "lib_prebuilt",
3100 bazel_module: { label: "//:lib_prebuilt" },
3101 static_libs: ["static_dep"],
3102 whole_static_libs: ["whole_static_dep"],
3103 shared_libs: ["shared_dep"],
3104 }
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003105 `
3106
3107 testCases := []struct {
3108 name string
3109 moduleName string
3110 variant string
3111 androidMkInfo cquery.CcAndroidMkInfo
3112 }{
3113 {
3114 name: "shared lib",
3115 moduleName: "lib",
3116 variant: "android_arm64_armv8-a_shared",
3117 androidMkInfo: cquery.CcAndroidMkInfo{
3118 LocalStaticLibs: []string{"static_dep"},
3119 LocalWholeStaticLibs: []string{"whole_static_dep"},
3120 LocalSharedLibs: []string{"shared_dep"},
3121 },
3122 },
3123 {
3124 name: "static lib",
3125 moduleName: "lib",
3126 variant: "android_arm64_armv8-a_static",
3127 androidMkInfo: cquery.CcAndroidMkInfo{
3128 LocalStaticLibs: []string{"static_dep"},
3129 LocalWholeStaticLibs: []string{"whole_static_dep"},
3130 LocalSharedLibs: []string{"shared_dep"},
3131 },
3132 },
3133 {
3134 name: "cc_test arm64",
3135 moduleName: "test",
3136 variant: "android_arm64_armv8-a",
3137 androidMkInfo: cquery.CcAndroidMkInfo{
3138 LocalStaticLibs: []string{"static_dep"},
3139 LocalWholeStaticLibs: []string{"whole_static_dep"},
3140 LocalSharedLibs: []string{"shared_dep"},
3141 },
3142 },
3143 {
3144 name: "cc_test arm",
3145 moduleName: "test",
3146 variant: "android_arm_armv7-a-neon",
3147 androidMkInfo: cquery.CcAndroidMkInfo{
3148 LocalStaticLibs: []string{"static_dep"},
3149 LocalWholeStaticLibs: []string{"whole_static_dep"},
3150 LocalSharedLibs: []string{"shared_dep"},
3151 },
3152 },
3153 {
3154 name: "cc_binary",
3155 moduleName: "binary",
3156 variant: "android_arm64_armv8-a",
3157 androidMkInfo: cquery.CcAndroidMkInfo{
3158 LocalStaticLibs: []string{"static_dep"},
3159 LocalWholeStaticLibs: []string{"whole_static_dep"},
3160 LocalSharedLibs: []string{"shared_dep"},
3161 },
3162 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003163 {
3164 name: "cc_library_headers",
3165 moduleName: "lib_headers",
3166 variant: "android_arm64_armv8-a",
3167 androidMkInfo: cquery.CcAndroidMkInfo{
3168 LocalStaticLibs: []string{"static_dep"},
3169 LocalWholeStaticLibs: []string{"whole_static_dep"},
3170 LocalSharedLibs: []string{"shared_dep"},
3171 },
3172 },
3173 {
3174 name: "prebuilt lib static",
3175 moduleName: "lib_prebuilt",
3176 variant: "android_arm64_armv8-a_static",
3177 androidMkInfo: cquery.CcAndroidMkInfo{
3178 LocalStaticLibs: []string{"static_dep"},
3179 LocalWholeStaticLibs: []string{"whole_static_dep"},
3180 LocalSharedLibs: []string{"shared_dep"},
3181 },
3182 },
3183 {
3184 name: "prebuilt lib shared",
3185 moduleName: "lib_prebuilt",
3186 variant: "android_arm64_armv8-a_shared",
3187 androidMkInfo: cquery.CcAndroidMkInfo{
3188 LocalStaticLibs: []string{"static_dep"},
3189 LocalWholeStaticLibs: []string{"whole_static_dep"},
3190 LocalSharedLibs: []string{"shared_dep"},
3191 },
3192 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003193 }
3194
3195 outputBaseDir := "out/bazel"
3196 for _, tc := range testCases {
3197 t.Run(tc.name, func(t *testing.T) {
3198 result := android.GroupFixturePreparers(
3199 prepareForCcTest,
3200 android.FixtureModifyConfig(func(config android.Config) {
3201 config.BazelContext = android.MockBazelContext{
3202 OutputBaseDir: outputBaseDir,
3203 LabelToCcInfo: map[string]cquery.CcInfo{
3204 "//:lib": cquery.CcInfo{
3205 CcAndroidMkInfo: tc.androidMkInfo,
3206 RootDynamicLibraries: []string{""},
3207 },
3208 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3209 CcAndroidMkInfo: tc.androidMkInfo,
3210 RootStaticArchives: []string{""},
3211 },
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003212 "//:lib_headers": cquery.CcInfo{
3213 CcAndroidMkInfo: tc.androidMkInfo,
3214 OutputFiles: []string{""},
3215 },
3216 "//:lib_prebuilt": cquery.CcInfo{
3217 CcAndroidMkInfo: tc.androidMkInfo,
3218 },
3219 "//:lib_prebuilt_bp2build_cc_library_static": cquery.CcInfo{
3220 CcAndroidMkInfo: tc.androidMkInfo,
3221 },
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003222 },
3223 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3224 "//:test": cquery.CcUnstrippedInfo{
3225 CcAndroidMkInfo: tc.androidMkInfo,
3226 },
3227 "//:binary": cquery.CcUnstrippedInfo{
3228 CcAndroidMkInfo: tc.androidMkInfo,
3229 },
3230 },
3231 }
3232 }),
3233 ).RunTestWithBp(t, bp)
3234 ctx := result.TestContext
3235
3236 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3237 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003238 if !reflect.DeepEqual(module.Properties.AndroidMkStaticLibs, tc.androidMkInfo.LocalStaticLibs) {
3239 t.Errorf("incorrect static_libs"+
3240 "\nactual: %v"+
3241 "\nexpected: %v",
3242 module.Properties.AndroidMkStaticLibs,
3243 tc.androidMkInfo.LocalStaticLibs,
3244 )
3245 }
3246 staticDepsDiffer, missingStaticDeps, additionalStaticDeps := android.ListSetDifference(
3247 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3248 tc.androidMkInfo.LocalStaticLibs,
3249 )
3250 if staticDepsDiffer {
3251 t.Errorf(
3252 "expected LOCAL_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3253 tc.androidMkInfo.LocalStaticLibs,
3254 entries.EntryMap["LOCAL_STATIC_LIBRARIES"],
3255 missingStaticDeps,
3256 additionalStaticDeps,
3257 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003258 }
3259
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003260 if !reflect.DeepEqual(module.Properties.AndroidMkWholeStaticLibs, tc.androidMkInfo.LocalWholeStaticLibs) {
3261 t.Errorf("expected module.Properties.AndroidMkWholeStaticLibs to be %q, but was %q",
3262 tc.androidMkInfo.LocalWholeStaticLibs,
3263 module.Properties.AndroidMkWholeStaticLibs,
3264 )
3265 }
3266 wholeStaticDepsDiffer, missingWholeStaticDeps, additionalWholeStaticDeps := android.ListSetDifference(
3267 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3268 tc.androidMkInfo.LocalWholeStaticLibs,
3269 )
3270 if wholeStaticDepsDiffer {
3271 t.Errorf(
3272 "expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q but was %q; missing: %q; extra %q",
3273 tc.androidMkInfo.LocalWholeStaticLibs,
3274 entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"],
3275 missingWholeStaticDeps,
3276 additionalWholeStaticDeps,
3277 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003278 }
3279
Sam Delmerico5fb794a2023-01-27 16:01:37 -05003280 if !reflect.DeepEqual(module.Properties.AndroidMkSharedLibs, tc.androidMkInfo.LocalSharedLibs) {
3281 t.Errorf("incorrect shared_libs"+
3282 "\nactual: %v"+
3283 "\nexpected: %v",
3284 module.Properties.AndroidMkSharedLibs,
3285 tc.androidMkInfo.LocalSharedLibs,
3286 )
3287 }
3288 sharedDepsDiffer, missingSharedDeps, additionalSharedDeps := android.ListSetDifference(
3289 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3290 tc.androidMkInfo.LocalSharedLibs,
3291 )
3292 if sharedDepsDiffer {
3293 t.Errorf(
3294 "expected LOCAL_SHARED_LIBRARIES to be %q but was %q; missing %q; extra %q",
3295 tc.androidMkInfo.LocalSharedLibs,
3296 entries.EntryMap["LOCAL_SHARED_LIBRARIES"],
3297 missingSharedDeps,
3298 additionalSharedDeps,
3299 )
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003300 }
3301 })
3302 }
3303}
3304
Jiyong Parkd08b6972017-09-26 10:50:54 +09003305var compilerFlagsTestCases = []struct {
3306 in string
3307 out bool
3308}{
3309 {
3310 in: "a",
3311 out: false,
3312 },
3313 {
3314 in: "-a",
3315 out: true,
3316 },
3317 {
3318 in: "-Ipath/to/something",
3319 out: false,
3320 },
3321 {
3322 in: "-isystempath/to/something",
3323 out: false,
3324 },
3325 {
3326 in: "--coverage",
3327 out: false,
3328 },
3329 {
3330 in: "-include a/b",
3331 out: true,
3332 },
3333 {
3334 in: "-include a/b c/d",
3335 out: false,
3336 },
3337 {
3338 in: "-DMACRO",
3339 out: true,
3340 },
3341 {
3342 in: "-DMAC RO",
3343 out: false,
3344 },
3345 {
3346 in: "-a -b",
3347 out: false,
3348 },
3349 {
3350 in: "-DMACRO=definition",
3351 out: true,
3352 },
3353 {
3354 in: "-DMACRO=defi nition",
3355 out: true, // TODO(jiyong): this should be false
3356 },
3357 {
3358 in: "-DMACRO(x)=x + 1",
3359 out: true,
3360 },
3361 {
3362 in: "-DMACRO=\"defi nition\"",
3363 out: true,
3364 },
3365}
3366
3367type mockContext struct {
3368 BaseModuleContext
3369 result bool
3370}
3371
3372func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3373 // CheckBadCompilerFlags calls this function when the flag should be rejected
3374 ctx.result = false
3375}
3376
3377func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003378 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003379 for _, testCase := range compilerFlagsTestCases {
3380 ctx := &mockContext{result: true}
3381 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3382 if ctx.result != testCase.out {
3383 t.Errorf("incorrect output:")
3384 t.Errorf(" input: %#v", testCase.in)
3385 t.Errorf(" expected: %#v", testCase.out)
3386 t.Errorf(" got: %#v", ctx.result)
3387 }
3388 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003389}
Jiyong Park374510b2018-03-19 18:23:01 +09003390
Jiyong Park37b25202018-07-11 10:49:27 +09003391func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003392 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003393 ctx := testCc(t, `
3394 cc_library_shared {
3395 name: "librecovery",
3396 recovery: true,
3397 }
3398 cc_library_shared {
3399 name: "librecovery32",
3400 recovery: true,
3401 compile_multilib:"32",
3402 }
Jiyong Park5baac542018-08-28 09:55:37 +09003403 cc_library_shared {
3404 name: "libHalInRecovery",
3405 recovery_available: true,
3406 vendor: true,
3407 }
Jiyong Park37b25202018-07-11 10:49:27 +09003408 `)
3409
3410 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003411 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003412 if len(variants) != 1 || !android.InList(arm64, variants) {
3413 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3414 }
3415
3416 variants = ctx.ModuleVariantsForTests("librecovery32")
3417 if android.InList(arm64, variants) {
3418 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3419 }
Jiyong Park5baac542018-08-28 09:55:37 +09003420
3421 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3422 if !recoveryModule.Platform() {
3423 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3424 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003425}
Jiyong Park5baac542018-08-28 09:55:37 +09003426
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003427func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003428 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003429 bp := `
3430 cc_prebuilt_test_library_shared {
3431 name: "test_lib",
3432 relative_install_path: "foo/bar/baz",
3433 srcs: ["srcpath/dontusethispath/baz.so"],
3434 }
3435
3436 cc_test {
3437 name: "main_test",
3438 data_libs: ["test_lib"],
3439 gtest: false,
3440 }
3441 `
3442
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003443 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003444 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003445 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003446 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3447
3448 ctx := testCcWithConfig(t, config)
3449 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3450 testBinary := module.(*Module).linker.(*testBinary)
3451 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3452 if err != nil {
3453 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3454 }
3455 if len(outputFiles) != 1 {
3456 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3457 }
3458 if len(testBinary.dataPaths()) != 1 {
3459 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3460 }
3461
3462 outputPath := outputFiles[0].String()
3463
3464 if !strings.HasSuffix(outputPath, "/main_test") {
3465 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3466 }
Colin Crossaa255532020-07-03 13:18:24 -07003467 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003468 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3469 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3470 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3471 }
3472}
3473
Jiyong Park7ed9de32018-10-15 22:25:07 +09003474func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003475 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003476 ctx := testCc(t, `
3477 cc_library_shared {
3478 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003479 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003480 stubs: {
3481 symbol_file: "foo.map.txt",
3482 versions: ["1", "2", "3"],
3483 },
3484 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003485
Jiyong Park7ed9de32018-10-15 22:25:07 +09003486 cc_library_shared {
3487 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003488 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003489 shared_libs: ["libFoo#1"],
3490 }`)
3491
3492 variants := ctx.ModuleVariantsForTests("libFoo")
3493 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003494 "android_arm64_armv8-a_shared",
3495 "android_arm64_armv8-a_shared_1",
3496 "android_arm64_armv8-a_shared_2",
3497 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003498 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003499 "android_arm_armv7-a-neon_shared",
3500 "android_arm_armv7-a-neon_shared_1",
3501 "android_arm_armv7-a-neon_shared_2",
3502 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003503 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003504 }
3505 variantsMismatch := false
3506 if len(variants) != len(expectedVariants) {
3507 variantsMismatch = true
3508 } else {
3509 for _, v := range expectedVariants {
3510 if !inList(v, variants) {
3511 variantsMismatch = false
3512 }
3513 }
3514 }
3515 if variantsMismatch {
3516 t.Errorf("variants of libFoo expected:\n")
3517 for _, v := range expectedVariants {
3518 t.Errorf("%q\n", v)
3519 }
3520 t.Errorf(", but got:\n")
3521 for _, v := range variants {
3522 t.Errorf("%q\n", v)
3523 }
3524 }
3525
Colin Cross7113d202019-11-20 16:39:12 -08003526 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003527 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003528 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003529 if !strings.Contains(libFlags, libFoo1StubPath) {
3530 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3531 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003532
Colin Cross7113d202019-11-20 16:39:12 -08003533 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003534 cFlags := libBarCompileRule.Args["cFlags"]
3535 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3536 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3537 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3538 }
Jiyong Park37b25202018-07-11 10:49:27 +09003539}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003540
Jooyung Hanb04a4992020-03-13 18:57:35 +09003541func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003542 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003543 for _, tc := range []struct{ moduleName, expected string }{
3544 {"libc", "__LIBC_API__"},
3545 {"libfoo", "__LIBFOO_API__"},
3546 {"libfoo@1", "__LIBFOO_1_API__"},
3547 {"libfoo-v1", "__LIBFOO_V1_API__"},
3548 {"libfoo.v1", "__LIBFOO_V1_API__"},
3549 } {
3550 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3551 }
3552}
3553
Liz Kammer83cf81b2022-09-22 08:24:20 -04003554func pathsToBase(paths android.Paths) []string {
3555 var ret []string
3556 for _, p := range paths {
3557 ret = append(ret, p.Base())
3558 }
3559 return ret
3560}
3561
3562func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003563 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003564 ctx := testCc(t, `
3565 cc_library_static {
3566 name: "foo",
3567 srcs: ["foo.c"],
3568 }
3569
3570 cc_library_static {
3571 name: "bar",
3572 srcs: ["bar.c"],
3573 }
3574
3575 cc_library_shared {
3576 name: "qux",
3577 srcs: ["qux.c"],
3578 }
3579
3580 cc_library_static {
3581 name: "baz",
3582 srcs: ["baz.c"],
3583 static_libs: ["foo"],
3584 shared_libs: ["qux"],
3585 whole_static_libs: ["bar"],
3586 }`)
3587
3588 variant := "android_arm64_armv8-a_static"
3589 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3590
3591 // For static libraries, the object files of a whole static dep are included in the archive
3592 // directly
3593 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3594 t.Errorf("Expected input objects %q, got %q", w, g)
3595 }
3596
3597 // non whole static dependencies are not linked into the archive
3598 if len(arRule.Implicits) > 0 {
3599 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3600 }
3601}
3602
3603func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003604 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003605 ctx := testCc(t, `
3606 cc_library_static {
3607 name: "foo",
3608 srcs: ["foo.c"],
3609 }
3610
3611 cc_library_static {
3612 name: "bar",
3613 srcs: ["bar.c"],
3614 }
3615
3616 cc_library_shared {
3617 name: "qux",
3618 srcs: ["qux.c"],
3619 }
3620
3621 cc_library_shared {
3622 name: "baz",
3623 srcs: ["baz.c"],
3624 static_libs: ["foo"],
3625 shared_libs: ["qux"],
3626 whole_static_libs: ["bar"],
3627 }`)
3628
3629 variant := "android_arm64_armv8-a_shared"
3630 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3631 libFlags := linkRule.Args["libFlags"]
3632 // When dynamically linking, we expect static dependencies to be found on the command line
3633 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3634 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3635 }
3636 // When dynamically linking, we expect whole static dependencies to be found on the command line
3637 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3638 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3639 }
3640
3641 // When dynamically linking, we expect shared dependencies to be found on the command line
3642 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3643 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3644 }
3645
3646 // We should only have the objects from the shared library srcs, not the whole static dependencies
3647 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3648 t.Errorf("Expected input objects %q, got %q", w, g)
3649 }
3650}
3651
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003652func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003653 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003654 ctx := testCc(t, `
3655 cc_binary {
3656 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003657 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003658 static_executable: true,
3659 }`)
3660
Colin Cross7113d202019-11-20 16:39:12 -08003661 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003662 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3663 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003664 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003665 for _, lib := range systemStaticLibs {
3666 if !strings.Contains(libFlags, lib) {
3667 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3668 }
3669 }
3670 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3671 for _, lib := range systemSharedLibs {
3672 if strings.Contains(libFlags, lib) {
3673 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3674 }
3675 }
3676}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003677
3678func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003679 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003680 ctx := testCc(t, `
3681 cc_binary {
3682 name: "mybin",
3683 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003684 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003685 static_executable: true,
3686 stl: "none",
3687 }
3688
3689 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003690 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003691 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003692 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003693 stl: "none",
3694 }
3695
3696 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003697 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003698 srcs: ["foo.c"],
3699 stl: "none",
3700 stubs: {
3701 versions: ["1"],
3702 },
3703 }`)
3704
Colin Cross0de8a1e2020-09-18 14:15:30 -07003705 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3706 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003707 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003708
3709 if !reflect.DeepEqual(actual, expected) {
3710 t.Errorf("staticDeps orderings were not propagated correctly"+
3711 "\nactual: %v"+
3712 "\nexpected: %v",
3713 actual,
3714 expected,
3715 )
3716 }
3717}
Jooyung Han38002912019-05-16 04:01:54 +09003718
Jooyung Hand48f3c32019-08-23 11:18:57 +09003719func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003720 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003721 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3722 cc_library {
3723 name: "libA",
3724 srcs: ["foo.c"],
3725 shared_libs: ["libB"],
3726 stl: "none",
3727 }
3728
3729 cc_library {
3730 name: "libB",
3731 srcs: ["foo.c"],
3732 enabled: false,
3733 stl: "none",
3734 }
3735 `)
3736}
3737
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003738func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3739 bp := `
3740 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003741 name: "test_afl_fuzz_target",
3742 srcs: ["foo.c"],
3743 host_supported: true,
3744 static_libs: [
3745 "afl_fuzz_static_lib",
3746 ],
3747 shared_libs: [
3748 "afl_fuzz_shared_lib",
3749 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003750 fuzzing_frameworks: {
3751 afl: true,
3752 libfuzzer: false,
3753 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003754 }
3755 cc_library {
3756 name: "afl_fuzz_static_lib",
3757 host_supported: true,
3758 srcs: ["static_file.c"],
3759 }
3760 cc_library {
3761 name: "libfuzzer_only_static_lib",
3762 host_supported: true,
3763 srcs: ["static_file.c"],
3764 }
3765 cc_library {
3766 name: "afl_fuzz_shared_lib",
3767 host_supported: true,
3768 srcs: ["shared_file.c"],
3769 static_libs: [
3770 "second_static_lib",
3771 ],
3772 }
3773 cc_library_headers {
3774 name: "libafl_headers",
3775 vendor_available: true,
3776 host_supported: true,
3777 export_include_dirs: [
3778 "include",
3779 "instrumentation",
3780 ],
3781 }
3782 cc_object {
3783 name: "afl-compiler-rt",
3784 vendor_available: true,
3785 host_supported: true,
3786 cflags: [
3787 "-fPIC",
3788 ],
3789 srcs: [
3790 "instrumentation/afl-compiler-rt.o.c",
3791 ],
3792 }
3793 cc_library {
3794 name: "second_static_lib",
3795 host_supported: true,
3796 srcs: ["second_file.c"],
3797 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003798 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003799 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003800 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003801 srcs: [
3802 "aflpp_driver.c",
3803 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003804 }`
3805
3806 testEnv := map[string]string{
3807 "FUZZ_FRAMEWORK": "AFL",
3808 }
3809
3810 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003811
3812 checkPcGuardFlag := func(
3813 modName string, variantName string, shouldHave bool) {
3814 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3815
3816 cFlags, ok := cc.Args["cFlags"]
3817 if !ok {
3818 t.Errorf("Could not find cFlags for module %s and variant %s",
3819 modName, variantName)
3820 }
3821
3822 if strings.Contains(
3823 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3824 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3825 "Test failed for module %s and variant %s",
3826 !shouldHave, shouldHave, modName, variantName)
3827 }
3828 }
3829
Cory Barkera1da26f2022-06-07 20:12:06 +00003830 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003831 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003832
3833 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003834 checkPcGuardFlag(moduleName, variant+"_static", false)
3835 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003836
3837 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003838 checkPcGuardFlag(moduleName, variant+"_static", false)
3839 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003840
3841 ctx.ModuleForTests("afl_fuzz_shared_lib",
3842 "android_arm64_armv8-a_shared").Rule("cc")
3843 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003844 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3845}
3846
3847func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003848 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003849 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3850}
3851
3852func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003853 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003854 if runtime.GOOS != "linux" {
3855 t.Skip("requires linux")
3856 }
3857
3858 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003859}
3860
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003861// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3862// correctly.
3863func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003864 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003865 ctx := testCc(t, `
3866 cc_fuzz {
3867 name: "fuzz_smoke_test",
3868 srcs: ["foo.c"],
3869 }`)
3870
Paul Duffin075c4172019-12-19 19:06:13 +00003871 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003872 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3873}
3874
Jooyung Han38002912019-05-16 04:01:54 +09003875func assertString(t *testing.T, got, expected string) {
3876 t.Helper()
3877 if got != expected {
3878 t.Errorf("expected %q got %q", expected, got)
3879 }
3880}
3881
3882func assertArrayString(t *testing.T, got, expected []string) {
3883 t.Helper()
3884 if len(got) != len(expected) {
3885 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3886 return
3887 }
3888 for i := range got {
3889 if got[i] != expected[i] {
3890 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3891 i, expected[i], expected, got[i], got)
3892 return
3893 }
3894 }
3895}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003896
Jooyung Han0302a842019-10-30 18:43:49 +09003897func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3898 t.Helper()
3899 assertArrayString(t, android.SortedStringKeys(m), expected)
3900}
3901
Colin Crosse1bb5d02019-09-24 14:55:04 -07003902func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003903 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003904 ctx := testCc(t, `
3905 cc_defaults {
3906 name: "defaults",
3907 srcs: ["foo.c"],
3908 static: {
3909 srcs: ["bar.c"],
3910 },
3911 shared: {
3912 srcs: ["baz.c"],
3913 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003914 bazel_module: {
3915 bp2build_available: true,
3916 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003917 }
3918
3919 cc_library_static {
3920 name: "libstatic",
3921 defaults: ["defaults"],
3922 }
3923
3924 cc_library_shared {
3925 name: "libshared",
3926 defaults: ["defaults"],
3927 }
3928
3929 cc_library {
3930 name: "libboth",
3931 defaults: ["defaults"],
3932 }
3933
3934 cc_binary {
3935 name: "binary",
3936 defaults: ["defaults"],
3937 }`)
3938
Colin Cross7113d202019-11-20 16:39:12 -08003939 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003940 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3941 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3942 }
Colin Cross7113d202019-11-20 16:39:12 -08003943 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003944 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3945 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3946 }
Colin Cross7113d202019-11-20 16:39:12 -08003947 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003948 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3949 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3950 }
3951
Colin Cross7113d202019-11-20 16:39:12 -08003952 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003953 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3954 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3955 }
Colin Cross7113d202019-11-20 16:39:12 -08003956 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003957 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3958 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3959 }
3960}
Colin Crosseabaedd2020-02-06 17:01:55 -08003961
3962func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003963 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003964 bp := `
3965 cc_defaults {
3966 name: "libfoo_defaults",
3967 srcs: ["foo.c"],
3968 cppflags: ["-DFOO"],
3969 product_variables: {
3970 debuggable: {
3971 cppflags: ["-DBAR"],
3972 },
3973 },
3974 }
3975
3976 cc_library {
3977 name: "libfoo",
3978 defaults: ["libfoo_defaults"],
3979 }
3980 `
3981
Paul Duffin8567f222021-03-23 00:02:06 +00003982 result := android.GroupFixturePreparers(
3983 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003984 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003985
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003986 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3987 variables.Debuggable = BoolPtr(true)
3988 }),
3989 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003990
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003991 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003992 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003993}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003994
3995func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3996 t.Parallel()
3997 bp := `
3998 cc_library_static {
3999 name: "libfoo",
4000 srcs: ["foo.c"],
4001 whole_static_libs: ["libbar"],
4002 }
4003
4004 cc_library_static {
4005 name: "libbar",
4006 whole_static_libs: ["libmissing"],
4007 }
4008 `
4009
Paul Duffin8567f222021-03-23 00:02:06 +00004010 result := android.GroupFixturePreparers(
4011 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004012 android.PrepareForTestWithAllowMissingDependencies,
4013 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004014
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004015 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004016 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004017
Paul Duffine84b1332021-03-12 11:59:43 +00004018 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07004019
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004020 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00004021 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07004022}
Colin Crosse9fe2942020-11-10 18:12:15 -08004023
4024func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004025 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08004026 bp := `
4027 cc_binary {
4028 name: "bin",
4029 host_supported: true,
4030 shared_libs: ["libshared"],
4031 runtime_libs: ["libruntime"],
4032 srcs: [":gen"],
4033 }
4034
4035 cc_library_shared {
4036 name: "libshared",
4037 host_supported: true,
4038 shared_libs: ["libtransitive"],
4039 }
4040
4041 cc_library_shared {
4042 name: "libtransitive",
4043 host_supported: true,
4044 }
4045
4046 cc_library_shared {
4047 name: "libruntime",
4048 host_supported: true,
4049 }
4050
4051 cc_binary_host {
4052 name: "tool",
4053 srcs: ["foo.cpp"],
4054 }
4055
4056 genrule {
4057 name: "gen",
4058 tools: ["tool"],
4059 out: ["gen.cpp"],
4060 cmd: "$(location tool) $(out)",
4061 }
4062 `
4063
Paul Duffinc3e6ce02021-03-22 23:21:32 +00004064 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08004065 ctx := testCcWithConfig(t, config)
4066
4067 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4068 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4069 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4070 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4071 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4072
4073 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4074 t.Errorf("expected host bin dependency %q, got %q", w, g)
4075 }
4076
4077 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4078 t.Errorf("expected host bin dependency %q, got %q", w, g)
4079 }
4080
4081 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4082 t.Errorf("expected host bin dependency %q, got %q", w, g)
4083 }
4084
4085 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4086 t.Errorf("expected host bin dependency %q, got %q", w, g)
4087 }
4088
4089 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4090 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4091 }
4092
4093 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4094 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4095 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4096 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4097
4098 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4099 t.Errorf("expected device bin dependency %q, got %q", w, g)
4100 }
4101
4102 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4103 t.Errorf("expected device bin dependency %q, got %q", w, g)
4104 }
4105
4106 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4107 t.Errorf("expected device bin dependency %q, got %q", w, g)
4108 }
4109
4110 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4111 t.Errorf("expected device bin dependency %q, got %q", w, g)
4112 }
4113
4114 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4115 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4116 }
4117
4118}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004119
4120func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004121 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004122 ctx := testCc(t, `
4123 cc_library_shared {
4124 name: "libclient",
4125 srcs: ["foo.c"],
4126 shared_libs: ["libfoo#1"],
4127 }
4128
4129 cc_library_shared {
4130 name: "libfoo",
4131 srcs: ["foo.c"],
4132 shared_libs: ["libbar"],
4133 export_shared_lib_headers: ["libbar"],
4134 stubs: {
4135 symbol_file: "foo.map.txt",
4136 versions: ["1", "2", "3"],
4137 },
4138 }
4139
4140 cc_library_shared {
4141 name: "libbar",
4142 export_include_dirs: ["include/libbar"],
4143 srcs: ["foo.c"],
4144 }`)
4145
4146 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4147
4148 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4149 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4150 }
4151}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004152
4153func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004154 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09004155 ctx := testCc(t, `
4156 cc_library {
4157 name: "libfoo",
4158 srcs: ["a/Foo.aidl"],
4159 aidl: { flags: ["-Werror"], },
4160 }
4161 `)
4162
4163 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4164 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4165 aidlCommand := manifest.Commands[0].GetCommand()
4166 expectedAidlFlag := "-Werror"
4167 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4168 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4169 }
4170}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004171
Jooyung Han07f70c02021-11-06 07:08:45 +09004172func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004173 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004174 for _, tc := range []struct {
4175 name string
4176 sdkVersion string
4177 variant string
4178 expected string
4179 }{
4180 {
4181 name: "default is current",
4182 sdkVersion: "",
4183 variant: "android_arm64_armv8-a_static",
4184 expected: "platform_apis",
4185 },
4186 {
4187 name: "use sdk_version",
4188 sdkVersion: `sdk_version: "29"`,
4189 variant: "android_arm64_armv8-a_static",
4190 expected: "platform_apis",
4191 },
4192 {
4193 name: "use sdk_version(sdk variant)",
4194 sdkVersion: `sdk_version: "29"`,
4195 variant: "android_arm64_armv8-a_sdk_static",
4196 expected: "29",
4197 },
4198 {
4199 name: "use min_sdk_version",
4200 sdkVersion: `min_sdk_version: "29"`,
4201 variant: "android_arm64_armv8-a_static",
4202 expected: "29",
4203 },
4204 } {
4205 t.Run(tc.name, func(t *testing.T) {
4206 ctx := testCc(t, `
4207 cc_library {
4208 name: "libfoo",
4209 stl: "none",
4210 srcs: ["a/Foo.aidl"],
4211 `+tc.sdkVersion+`
4212 }
4213 `)
4214 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4215 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4216 aidlCommand := manifest.Commands[0].GetCommand()
4217 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4218 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4219 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4220 }
4221 })
4222 }
4223}
4224
Jiyong Parka008fb02021-03-16 17:15:53 +09004225func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004226 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004227 ctx := testCc(t, `
4228 cc_library_shared {
4229 name: "libfoo",
4230 srcs: ["foo.c"],
4231 min_sdk_version: "29",
4232 }`)
4233
4234 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4235 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4236}
4237
Vinh Tranf1924742022-06-24 16:40:11 -04004238func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004239 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004240 bp := `
4241 cc_library_shared {
4242 name: "libfoo",
4243 srcs: ["foo.c"],
4244 min_sdk_version: "S",
4245 }
4246 `
4247 result := android.GroupFixturePreparers(
4248 prepareForCcTest,
4249 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4250 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4251 }),
4252 ).RunTestWithBp(t, bp)
4253 ctx := result.TestContext
4254 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4255 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4256}
4257
Paul Duffin3cb603e2021-02-19 13:57:10 +00004258func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004259 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004260
4261 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4262 // embedded newline characters alone.
4263 trimIndentingSpaces := func(s string) string {
4264 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4265 }
4266
4267 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4268 t.Helper()
4269 expected = trimIndentingSpaces(expected)
4270 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4271 if expected != actual {
4272 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4273 }
4274 }
4275
4276 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4277
4278 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4279 t.Helper()
4280 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4281 name := module.Name()
4282
4283 for _, checker := range checkers {
4284 checker(t, name, exported)
4285 }
4286 }
4287
4288 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4289 return func(t *testing.T, name string, exported FlagExporterInfo) {
4290 t.Helper()
4291 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4292 }
4293 }
4294
4295 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4296 return func(t *testing.T, name string, exported FlagExporterInfo) {
4297 t.Helper()
4298 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4299 }
4300 }
4301
4302 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4303 return func(t *testing.T, name string, exported FlagExporterInfo) {
4304 t.Helper()
4305 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4306 }
4307 }
4308
4309 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4310 return func(t *testing.T, name string, exported FlagExporterInfo) {
4311 t.Helper()
4312 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4313 }
4314 }
4315
4316 genRuleModules := `
4317 genrule {
4318 name: "genrule_foo",
4319 cmd: "generate-foo",
4320 out: [
4321 "generated_headers/foo/generated_header.h",
4322 ],
4323 export_include_dirs: [
4324 "generated_headers",
4325 ],
4326 }
4327
4328 genrule {
4329 name: "genrule_bar",
4330 cmd: "generate-bar",
4331 out: [
4332 "generated_headers/bar/generated_header.h",
4333 ],
4334 export_include_dirs: [
4335 "generated_headers",
4336 ],
4337 }
4338 `
4339
4340 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4341 ctx := testCc(t, genRuleModules+`
4342 cc_library {
4343 name: "libfoo",
4344 srcs: ["foo.c"],
4345 export_include_dirs: ["foo/standard"],
4346 export_system_include_dirs: ["foo/system"],
4347 generated_headers: ["genrule_foo"],
4348 export_generated_headers: ["genrule_foo"],
4349 }
4350
4351 cc_library {
4352 name: "libbar",
4353 srcs: ["bar.c"],
4354 shared_libs: ["libfoo"],
4355 export_include_dirs: ["bar/standard"],
4356 export_system_include_dirs: ["bar/system"],
4357 generated_headers: ["genrule_bar"],
4358 export_generated_headers: ["genrule_bar"],
4359 }
4360 `)
4361 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4362 checkIncludeDirs(t, ctx, foo,
4363 expectedIncludeDirs(`
4364 foo/standard
4365 .intermediates/genrule_foo/gen/generated_headers
4366 `),
4367 expectedSystemIncludeDirs(`foo/system`),
4368 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4369 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4370 )
4371
4372 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4373 checkIncludeDirs(t, ctx, bar,
4374 expectedIncludeDirs(`
4375 bar/standard
4376 .intermediates/genrule_bar/gen/generated_headers
4377 `),
4378 expectedSystemIncludeDirs(`bar/system`),
4379 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4380 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4381 )
4382 })
4383
4384 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4385 ctx := testCc(t, genRuleModules+`
4386 cc_library {
4387 name: "libfoo",
4388 srcs: ["foo.c"],
4389 export_include_dirs: ["foo/standard"],
4390 export_system_include_dirs: ["foo/system"],
4391 generated_headers: ["genrule_foo"],
4392 export_generated_headers: ["genrule_foo"],
4393 }
4394
4395 cc_library {
4396 name: "libbar",
4397 srcs: ["bar.c"],
4398 whole_static_libs: ["libfoo"],
4399 export_include_dirs: ["bar/standard"],
4400 export_system_include_dirs: ["bar/system"],
4401 generated_headers: ["genrule_bar"],
4402 export_generated_headers: ["genrule_bar"],
4403 }
4404 `)
4405 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4406 checkIncludeDirs(t, ctx, foo,
4407 expectedIncludeDirs(`
4408 foo/standard
4409 .intermediates/genrule_foo/gen/generated_headers
4410 `),
4411 expectedSystemIncludeDirs(`foo/system`),
4412 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4413 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4414 )
4415
4416 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4417 checkIncludeDirs(t, ctx, bar,
4418 expectedIncludeDirs(`
4419 bar/standard
4420 foo/standard
4421 .intermediates/genrule_foo/gen/generated_headers
4422 .intermediates/genrule_bar/gen/generated_headers
4423 `),
4424 expectedSystemIncludeDirs(`
4425 bar/system
4426 foo/system
4427 `),
4428 expectedGeneratedHeaders(`
4429 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4430 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4431 `),
4432 expectedOrderOnlyDeps(`
4433 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4434 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4435 `),
4436 )
4437 })
4438
Paul Duffin3cb603e2021-02-19 13:57:10 +00004439 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4440 ctx := testCc(t, genRuleModules+`
4441 cc_library_shared {
4442 name: "libfoo",
4443 srcs: [
4444 "foo.c",
4445 "b.aidl",
4446 "a.proto",
4447 ],
4448 aidl: {
4449 export_aidl_headers: true,
4450 }
4451 }
4452 `)
4453 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4454 checkIncludeDirs(t, ctx, foo,
4455 expectedIncludeDirs(`
4456 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4457 `),
4458 expectedSystemIncludeDirs(``),
4459 expectedGeneratedHeaders(`
4460 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4461 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4462 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004463 `),
4464 expectedOrderOnlyDeps(`
4465 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4466 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4467 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004468 `),
4469 )
4470 })
4471
Paul Duffin3cb603e2021-02-19 13:57:10 +00004472 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4473 ctx := testCc(t, genRuleModules+`
4474 cc_library_shared {
4475 name: "libfoo",
4476 srcs: [
4477 "foo.c",
4478 "b.aidl",
4479 "a.proto",
4480 ],
4481 proto: {
4482 export_proto_headers: true,
4483 }
4484 }
4485 `)
4486 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4487 checkIncludeDirs(t, ctx, foo,
4488 expectedIncludeDirs(`
4489 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4490 `),
4491 expectedSystemIncludeDirs(``),
4492 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004493 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4494 `),
4495 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004496 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4497 `),
4498 )
4499 })
4500
Paul Duffin33056e82021-02-19 13:49:08 +00004501 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004502 ctx := testCc(t, genRuleModules+`
4503 cc_library_shared {
4504 name: "libfoo",
4505 srcs: [
4506 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004507 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004508 "b.aidl",
4509 "a.proto",
4510 ],
4511 }
4512 `)
4513 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4514 checkIncludeDirs(t, ctx, foo,
4515 expectedIncludeDirs(`
4516 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4517 `),
4518 expectedSystemIncludeDirs(``),
4519 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004520 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004521 `),
4522 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004523 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4524 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004525 `),
4526 )
4527 })
4528}
Colin Crossae628182021-06-14 16:52:28 -07004529
4530func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004531 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004532 baseExpectedFlags := []string{
4533 "${config.ArmThumbCflags}",
4534 "${config.ArmCflags}",
4535 "${config.CommonGlobalCflags}",
4536 "${config.DeviceGlobalCflags}",
4537 "${config.ExternalCflags}",
4538 "${config.ArmToolchainCflags}",
4539 "${config.ArmArmv7ANeonCflags}",
4540 "${config.ArmGenericCflags}",
4541 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004542 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004543 }
4544
4545 expectedIncludes := []string{
4546 "external/foo/android_arm_export_include_dirs",
4547 "external/foo/lib32_export_include_dirs",
4548 "external/foo/arm_export_include_dirs",
4549 "external/foo/android_export_include_dirs",
4550 "external/foo/linux_export_include_dirs",
4551 "external/foo/export_include_dirs",
4552 "external/foo/android_arm_local_include_dirs",
4553 "external/foo/lib32_local_include_dirs",
4554 "external/foo/arm_local_include_dirs",
4555 "external/foo/android_local_include_dirs",
4556 "external/foo/linux_local_include_dirs",
4557 "external/foo/local_include_dirs",
4558 "external/foo",
4559 "external/foo/libheader1",
4560 "external/foo/libheader2",
4561 "external/foo/libwhole1",
4562 "external/foo/libwhole2",
4563 "external/foo/libstatic1",
4564 "external/foo/libstatic2",
4565 "external/foo/libshared1",
4566 "external/foo/libshared2",
4567 "external/foo/liblinux",
4568 "external/foo/libandroid",
4569 "external/foo/libarm",
4570 "external/foo/lib32",
4571 "external/foo/libandroid_arm",
4572 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004573 }
4574
4575 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4576 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4577
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004578 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004579 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004580 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004581
4582 lastIncludes := []string{
4583 "out/soong/ndk/sysroot/usr/include",
4584 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4585 }
4586
4587 combineSlices := func(slices ...[]string) []string {
4588 var ret []string
4589 for _, s := range slices {
4590 ret = append(ret, s...)
4591 }
4592 return ret
4593 }
4594
4595 testCases := []struct {
4596 name string
4597 src string
4598 expected []string
4599 }{
4600 {
4601 name: "c",
4602 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004603 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004604 },
4605 {
4606 name: "cc",
4607 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004608 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004609 },
4610 {
4611 name: "assemble",
4612 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004613 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004614 },
4615 }
4616
4617 for _, tc := range testCases {
4618 t.Run(tc.name, func(t *testing.T) {
4619 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004620 cc_library {
4621 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004622 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004623 cflags: ["-std=candcpp"],
4624 conlyflags: ["-std=conly"],
4625 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004626 local_include_dirs: ["local_include_dirs"],
4627 export_include_dirs: ["export_include_dirs"],
4628 export_system_include_dirs: ["export_system_include_dirs"],
4629 static_libs: ["libstatic1", "libstatic2"],
4630 whole_static_libs: ["libwhole1", "libwhole2"],
4631 shared_libs: ["libshared1", "libshared2"],
4632 header_libs: ["libheader1", "libheader2"],
4633 target: {
4634 android: {
4635 shared_libs: ["libandroid"],
4636 local_include_dirs: ["android_local_include_dirs"],
4637 export_include_dirs: ["android_export_include_dirs"],
4638 },
4639 android_arm: {
4640 shared_libs: ["libandroid_arm"],
4641 local_include_dirs: ["android_arm_local_include_dirs"],
4642 export_include_dirs: ["android_arm_export_include_dirs"],
4643 },
4644 linux: {
4645 shared_libs: ["liblinux"],
4646 local_include_dirs: ["linux_local_include_dirs"],
4647 export_include_dirs: ["linux_export_include_dirs"],
4648 },
4649 },
4650 multilib: {
4651 lib32: {
4652 shared_libs: ["lib32"],
4653 local_include_dirs: ["lib32_local_include_dirs"],
4654 export_include_dirs: ["lib32_export_include_dirs"],
4655 },
4656 },
4657 arch: {
4658 arm: {
4659 shared_libs: ["libarm"],
4660 local_include_dirs: ["arm_local_include_dirs"],
4661 export_include_dirs: ["arm_export_include_dirs"],
4662 },
4663 },
4664 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004665 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004666 }
4667
4668 cc_library_headers {
4669 name: "libheader1",
4670 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004671 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004672 stl: "none",
4673 }
4674
4675 cc_library_headers {
4676 name: "libheader2",
4677 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004678 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004679 stl: "none",
4680 }
Liz Kammer08572c62021-09-30 10:11:04 -04004681 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004682
Liz Kammer08572c62021-09-30 10:11:04 -04004683 libs := []string{
4684 "libstatic1",
4685 "libstatic2",
4686 "libwhole1",
4687 "libwhole2",
4688 "libshared1",
4689 "libshared2",
4690 "libandroid",
4691 "libandroid_arm",
4692 "liblinux",
4693 "lib32",
4694 "libarm",
4695 }
Colin Crossae628182021-06-14 16:52:28 -07004696
Liz Kammer08572c62021-09-30 10:11:04 -04004697 for _, lib := range libs {
4698 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004699 cc_library {
4700 name: "%s",
4701 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004702 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004703 stl: "none",
4704 }
4705 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004706 }
4707
4708 ctx := android.GroupFixturePreparers(
4709 PrepareForIntegrationTestWithCc,
4710 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4711 ).RunTest(t)
4712 // Use the arm variant instead of the arm64 variant so that it gets headers from
4713 // ndk_libandroid_support to test LateStaticLibs.
4714 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4715
4716 var includes []string
4717 flags := strings.Split(cflags, " ")
4718 for _, flag := range flags {
4719 if strings.HasPrefix(flag, "-I") {
4720 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4721 } else if flag == "-isystem" {
4722 // skip isystem, include next
4723 } else if len(flag) > 0 {
4724 includes = append(includes, flag)
4725 }
4726 }
4727
4728 android.AssertArrayString(t, "includes", tc.expected, includes)
4729 })
Colin Crossae628182021-06-14 16:52:28 -07004730 }
4731
Colin Crossae628182021-06-14 16:52:28 -07004732}
Alixb5f6d9e2022-04-20 23:00:58 +00004733
zijunzhao933e3802023-01-12 07:26:20 +00004734func TestAddnoOverride64GlobalCflags(t *testing.T) {
4735 t.Parallel()
4736 ctx := testCc(t, `
4737 cc_library_shared {
4738 name: "libclient",
4739 srcs: ["foo.c"],
4740 shared_libs: ["libfoo#1"],
4741 }
4742
4743 cc_library_shared {
4744 name: "libfoo",
4745 srcs: ["foo.c"],
4746 shared_libs: ["libbar"],
4747 export_shared_lib_headers: ["libbar"],
4748 stubs: {
4749 symbol_file: "foo.map.txt",
4750 versions: ["1", "2", "3"],
4751 },
4752 }
4753
4754 cc_library_shared {
4755 name: "libbar",
4756 export_include_dirs: ["include/libbar"],
4757 srcs: ["foo.c"],
4758 }`)
4759
4760 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4761
4762 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4763 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4764 }
4765}
4766
Alixb5f6d9e2022-04-20 23:00:58 +00004767func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004768 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004769 tests := []struct {
4770 name string
4771 clang bool
4772 BuildBrokenClangProperty bool
4773 err string
4774 }{
4775 {
4776 name: "error when clang is set to false",
4777 clang: false,
4778 err: "is no longer supported",
4779 },
4780 {
4781 name: "error when clang is set to true",
4782 clang: true,
4783 err: "property is deprecated, see Changes.md",
4784 },
4785 {
4786 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4787 clang: true,
4788 BuildBrokenClangProperty: true,
4789 },
4790 }
4791
4792 for _, test := range tests {
4793 t.Run(test.name, func(t *testing.T) {
4794 bp := fmt.Sprintf(`
4795 cc_library {
4796 name: "foo",
4797 clang: %t,
4798 }`, test.clang)
4799
4800 if test.err == "" {
4801 android.GroupFixturePreparers(
4802 prepareForCcTest,
4803 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4804 if test.BuildBrokenClangProperty {
4805 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4806 }
4807 }),
4808 ).RunTestWithBp(t, bp)
4809 } else {
4810 prepareForCcTest.
4811 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4812 RunTestWithBp(t, bp)
4813 }
4814 })
4815 }
4816}
Alix Espinoef47e542022-09-14 19:10:51 +00004817
4818func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004819 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004820 tests := []struct {
4821 name string
4822 clangAsFlags []string
4823 BuildBrokenClangAsFlags bool
4824 err string
4825 }{
4826 {
4827 name: "error when clang_asflags is set",
4828 clangAsFlags: []string{"-a", "-b"},
4829 err: "clang_asflags: property is deprecated",
4830 },
4831 {
4832 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4833 clangAsFlags: []string{"-a", "-b"},
4834 BuildBrokenClangAsFlags: true,
4835 },
4836 }
4837
4838 for _, test := range tests {
4839 t.Run(test.name, func(t *testing.T) {
4840 bp := fmt.Sprintf(`
4841 cc_library {
4842 name: "foo",
4843 clang_asflags: %s,
4844 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4845
4846 if test.err == "" {
4847 android.GroupFixturePreparers(
4848 prepareForCcTest,
4849 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4850 if test.BuildBrokenClangAsFlags {
4851 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4852 }
4853 }),
4854 ).RunTestWithBp(t, bp)
4855 } else {
4856 prepareForCcTest.
4857 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4858 RunTestWithBp(t, bp)
4859 }
4860 })
4861 }
4862}
4863
4864func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004865 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004866 tests := []struct {
4867 name string
4868 clangCFlags []string
4869 BuildBrokenClangCFlags bool
4870 err string
4871 }{
4872 {
4873 name: "error when clang_cflags is set",
4874 clangCFlags: []string{"-a", "-b"},
4875 err: "clang_cflags: property is deprecated",
4876 },
4877 {
4878 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4879 clangCFlags: []string{"-a", "-b"},
4880 BuildBrokenClangCFlags: true,
4881 },
4882 }
4883
4884 for _, test := range tests {
4885 t.Run(test.name, func(t *testing.T) {
4886 bp := fmt.Sprintf(`
4887 cc_library {
4888 name: "foo",
4889 clang_cflags: %s,
4890 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4891
4892 if test.err == "" {
4893 android.GroupFixturePreparers(
4894 prepareForCcTest,
4895 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4896 if test.BuildBrokenClangCFlags {
4897 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4898 }
4899 }),
4900 ).RunTestWithBp(t, bp)
4901 } else {
4902 prepareForCcTest.
4903 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4904 RunTestWithBp(t, bp)
4905 }
4906 })
4907 }
4908}