blob: 39cc0730cf084947d0220f87074932e3c834f188 [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
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003032func checkWholeStaticLibs(t *testing.T, expected []string, module *Module) {
3033 t.Helper()
3034 actual := module.Properties.AndroidMkWholeStaticLibs
3035 if !reflect.DeepEqual(actual, expected) {
3036 t.Errorf("incorrect whole_static_libs"+
3037 "\nactual: %v"+
3038 "\nexpected: %v",
3039 actual,
3040 expected,
3041 )
3042 }
3043}
3044
3045func checkSharedLibs(t *testing.T, expected []string, module *Module) {
3046 t.Helper()
3047 actual := module.Properties.AndroidMkSharedLibs
3048 if !reflect.DeepEqual(actual, expected) {
3049 t.Errorf("incorrect shared_libs"+
3050 "\nactual: %v"+
3051 "\nexpected: %v",
3052 actual,
3053 expected,
3054 )
3055 }
3056}
3057
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003058const staticLibAndroidBp = `
3059 cc_library {
3060 name: "lib1",
3061 }
3062 cc_library {
3063 name: "lib2",
3064 static_libs: ["lib1"],
3065 }
3066`
3067
3068func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003069 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003070 ctx := testCc(t, staticLibAndroidBp)
3071
3072 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003073 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003074 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003075 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003076
3077 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003078 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003079 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3080 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003081 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003082}
3083
Sam Delmerico4e115cc2023-01-19 15:36:52 -05003084func TestLibDepAndroidMkExportInMixedBuilds(t *testing.T) {
3085 bp := `
3086 cc_library {
3087 name: "static_dep",
3088 }
3089 cc_library {
3090 name: "whole_static_dep",
3091 }
3092 cc_library {
3093 name: "shared_dep",
3094 }
3095 cc_library {
3096 name: "lib",
3097 bazel_module: { label: "//:lib" },
3098 static_libs: ["static_dep"],
3099 whole_static_libs: ["whole_static_dep"],
3100 shared_libs: ["shared_dep"],
3101 }
3102 cc_test {
3103 name: "test",
3104 bazel_module: { label: "//:test" },
3105 static_libs: ["static_dep"],
3106 whole_static_libs: ["whole_static_dep"],
3107 shared_libs: ["shared_dep"],
3108 gtest: false,
3109 }
3110 cc_binary {
3111 name: "binary",
3112 bazel_module: { label: "//:binary" },
3113 static_libs: ["static_dep"],
3114 whole_static_libs: ["whole_static_dep"],
3115 shared_libs: ["shared_dep"],
3116 }
3117 `
3118
3119 testCases := []struct {
3120 name string
3121 moduleName string
3122 variant string
3123 androidMkInfo cquery.CcAndroidMkInfo
3124 }{
3125 {
3126 name: "shared lib",
3127 moduleName: "lib",
3128 variant: "android_arm64_armv8-a_shared",
3129 androidMkInfo: cquery.CcAndroidMkInfo{
3130 LocalStaticLibs: []string{"static_dep"},
3131 LocalWholeStaticLibs: []string{"whole_static_dep"},
3132 LocalSharedLibs: []string{"shared_dep"},
3133 },
3134 },
3135 {
3136 name: "static lib",
3137 moduleName: "lib",
3138 variant: "android_arm64_armv8-a_static",
3139 androidMkInfo: cquery.CcAndroidMkInfo{
3140 LocalStaticLibs: []string{"static_dep"},
3141 LocalWholeStaticLibs: []string{"whole_static_dep"},
3142 LocalSharedLibs: []string{"shared_dep"},
3143 },
3144 },
3145 {
3146 name: "cc_test arm64",
3147 moduleName: "test",
3148 variant: "android_arm64_armv8-a",
3149 androidMkInfo: cquery.CcAndroidMkInfo{
3150 LocalStaticLibs: []string{"static_dep"},
3151 LocalWholeStaticLibs: []string{"whole_static_dep"},
3152 LocalSharedLibs: []string{"shared_dep"},
3153 },
3154 },
3155 {
3156 name: "cc_test arm",
3157 moduleName: "test",
3158 variant: "android_arm_armv7-a-neon",
3159 androidMkInfo: cquery.CcAndroidMkInfo{
3160 LocalStaticLibs: []string{"static_dep"},
3161 LocalWholeStaticLibs: []string{"whole_static_dep"},
3162 LocalSharedLibs: []string{"shared_dep"},
3163 },
3164 },
3165 {
3166 name: "cc_binary",
3167 moduleName: "binary",
3168 variant: "android_arm64_armv8-a",
3169 androidMkInfo: cquery.CcAndroidMkInfo{
3170 LocalStaticLibs: []string{"static_dep"},
3171 LocalWholeStaticLibs: []string{"whole_static_dep"},
3172 LocalSharedLibs: []string{"shared_dep"},
3173 },
3174 },
3175 }
3176
3177 outputBaseDir := "out/bazel"
3178 for _, tc := range testCases {
3179 t.Run(tc.name, func(t *testing.T) {
3180 result := android.GroupFixturePreparers(
3181 prepareForCcTest,
3182 android.FixtureModifyConfig(func(config android.Config) {
3183 config.BazelContext = android.MockBazelContext{
3184 OutputBaseDir: outputBaseDir,
3185 LabelToCcInfo: map[string]cquery.CcInfo{
3186 "//:lib": cquery.CcInfo{
3187 CcAndroidMkInfo: tc.androidMkInfo,
3188 RootDynamicLibraries: []string{""},
3189 },
3190 "//:lib_bp2build_cc_library_static": cquery.CcInfo{
3191 CcAndroidMkInfo: tc.androidMkInfo,
3192 RootStaticArchives: []string{""},
3193 },
3194 },
3195 LabelToCcBinary: map[string]cquery.CcUnstrippedInfo{
3196 "//:test": cquery.CcUnstrippedInfo{
3197 CcAndroidMkInfo: tc.androidMkInfo,
3198 },
3199 "//:binary": cquery.CcUnstrippedInfo{
3200 CcAndroidMkInfo: tc.androidMkInfo,
3201 },
3202 },
3203 }
3204 }),
3205 ).RunTestWithBp(t, bp)
3206 ctx := result.TestContext
3207
3208 module := ctx.ModuleForTests(tc.moduleName, tc.variant).Module().(*Module)
3209 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
3210 checkStaticLibs(t, tc.androidMkInfo.LocalStaticLibs, module)
3211 missingStaticDeps := android.ListDifference(entries.EntryMap["LOCAL_STATIC_LIBRARIES"], tc.androidMkInfo.LocalStaticLibs)
3212 if len(missingStaticDeps) > 0 {
3213 t.Errorf("expected LOCAL_STATIC_LIBRARIES to be %q"+
3214 " but was %q; difference: %q", tc.androidMkInfo.LocalStaticLibs, entries.EntryMap["LOCAL_STATIC_LIBRARIES"], missingStaticDeps)
3215 }
3216
3217 checkWholeStaticLibs(t, tc.androidMkInfo.LocalWholeStaticLibs, module)
3218 missingWholeStaticDeps := android.ListDifference(entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"], tc.androidMkInfo.LocalWholeStaticLibs)
3219 if len(missingWholeStaticDeps) > 0 {
3220 t.Errorf("expected LOCAL_WHOLE_STATIC_LIBRARIES to be %q"+
3221 " but was %q; difference: %q", tc.androidMkInfo.LocalWholeStaticLibs, entries.EntryMap["LOCAL_WHOLE_STATIC_LIBRARIES"], missingWholeStaticDeps)
3222 }
3223
3224 checkSharedLibs(t, tc.androidMkInfo.LocalSharedLibs, module)
3225 missingSharedDeps := android.ListDifference(entries.EntryMap["LOCAL_SHARED_LIBRARIES"], tc.androidMkInfo.LocalSharedLibs)
3226 if len(missingSharedDeps) > 0 {
3227 t.Errorf("expected LOCAL_SHARED_LIBRARIES to be %q"+
3228 " but was %q; difference: %q", tc.androidMkInfo.LocalSharedLibs, entries.EntryMap["LOCAL_SHARED_LIBRARIES"], missingSharedDeps)
3229 }
3230 })
3231 }
3232}
3233
Jiyong Parkd08b6972017-09-26 10:50:54 +09003234var compilerFlagsTestCases = []struct {
3235 in string
3236 out bool
3237}{
3238 {
3239 in: "a",
3240 out: false,
3241 },
3242 {
3243 in: "-a",
3244 out: true,
3245 },
3246 {
3247 in: "-Ipath/to/something",
3248 out: false,
3249 },
3250 {
3251 in: "-isystempath/to/something",
3252 out: false,
3253 },
3254 {
3255 in: "--coverage",
3256 out: false,
3257 },
3258 {
3259 in: "-include a/b",
3260 out: true,
3261 },
3262 {
3263 in: "-include a/b c/d",
3264 out: false,
3265 },
3266 {
3267 in: "-DMACRO",
3268 out: true,
3269 },
3270 {
3271 in: "-DMAC RO",
3272 out: false,
3273 },
3274 {
3275 in: "-a -b",
3276 out: false,
3277 },
3278 {
3279 in: "-DMACRO=definition",
3280 out: true,
3281 },
3282 {
3283 in: "-DMACRO=defi nition",
3284 out: true, // TODO(jiyong): this should be false
3285 },
3286 {
3287 in: "-DMACRO(x)=x + 1",
3288 out: true,
3289 },
3290 {
3291 in: "-DMACRO=\"defi nition\"",
3292 out: true,
3293 },
3294}
3295
3296type mockContext struct {
3297 BaseModuleContext
3298 result bool
3299}
3300
3301func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3302 // CheckBadCompilerFlags calls this function when the flag should be rejected
3303 ctx.result = false
3304}
3305
3306func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003307 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003308 for _, testCase := range compilerFlagsTestCases {
3309 ctx := &mockContext{result: true}
3310 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3311 if ctx.result != testCase.out {
3312 t.Errorf("incorrect output:")
3313 t.Errorf(" input: %#v", testCase.in)
3314 t.Errorf(" expected: %#v", testCase.out)
3315 t.Errorf(" got: %#v", ctx.result)
3316 }
3317 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003318}
Jiyong Park374510b2018-03-19 18:23:01 +09003319
Jiyong Park37b25202018-07-11 10:49:27 +09003320func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003321 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003322 ctx := testCc(t, `
3323 cc_library_shared {
3324 name: "librecovery",
3325 recovery: true,
3326 }
3327 cc_library_shared {
3328 name: "librecovery32",
3329 recovery: true,
3330 compile_multilib:"32",
3331 }
Jiyong Park5baac542018-08-28 09:55:37 +09003332 cc_library_shared {
3333 name: "libHalInRecovery",
3334 recovery_available: true,
3335 vendor: true,
3336 }
Jiyong Park37b25202018-07-11 10:49:27 +09003337 `)
3338
3339 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003340 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003341 if len(variants) != 1 || !android.InList(arm64, variants) {
3342 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3343 }
3344
3345 variants = ctx.ModuleVariantsForTests("librecovery32")
3346 if android.InList(arm64, variants) {
3347 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3348 }
Jiyong Park5baac542018-08-28 09:55:37 +09003349
3350 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3351 if !recoveryModule.Platform() {
3352 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3353 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003354}
Jiyong Park5baac542018-08-28 09:55:37 +09003355
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003356func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003357 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003358 bp := `
3359 cc_prebuilt_test_library_shared {
3360 name: "test_lib",
3361 relative_install_path: "foo/bar/baz",
3362 srcs: ["srcpath/dontusethispath/baz.so"],
3363 }
3364
3365 cc_test {
3366 name: "main_test",
3367 data_libs: ["test_lib"],
3368 gtest: false,
3369 }
3370 `
3371
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003372 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003373 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003374 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003375 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3376
3377 ctx := testCcWithConfig(t, config)
3378 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3379 testBinary := module.(*Module).linker.(*testBinary)
3380 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3381 if err != nil {
3382 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3383 }
3384 if len(outputFiles) != 1 {
3385 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3386 }
3387 if len(testBinary.dataPaths()) != 1 {
3388 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3389 }
3390
3391 outputPath := outputFiles[0].String()
3392
3393 if !strings.HasSuffix(outputPath, "/main_test") {
3394 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3395 }
Colin Crossaa255532020-07-03 13:18:24 -07003396 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003397 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3398 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3399 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3400 }
3401}
3402
Jiyong Park7ed9de32018-10-15 22:25:07 +09003403func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003404 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003405 ctx := testCc(t, `
3406 cc_library_shared {
3407 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003408 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003409 stubs: {
3410 symbol_file: "foo.map.txt",
3411 versions: ["1", "2", "3"],
3412 },
3413 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003414
Jiyong Park7ed9de32018-10-15 22:25:07 +09003415 cc_library_shared {
3416 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003417 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003418 shared_libs: ["libFoo#1"],
3419 }`)
3420
3421 variants := ctx.ModuleVariantsForTests("libFoo")
3422 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003423 "android_arm64_armv8-a_shared",
3424 "android_arm64_armv8-a_shared_1",
3425 "android_arm64_armv8-a_shared_2",
3426 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003427 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003428 "android_arm_armv7-a-neon_shared",
3429 "android_arm_armv7-a-neon_shared_1",
3430 "android_arm_armv7-a-neon_shared_2",
3431 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003432 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003433 }
3434 variantsMismatch := false
3435 if len(variants) != len(expectedVariants) {
3436 variantsMismatch = true
3437 } else {
3438 for _, v := range expectedVariants {
3439 if !inList(v, variants) {
3440 variantsMismatch = false
3441 }
3442 }
3443 }
3444 if variantsMismatch {
3445 t.Errorf("variants of libFoo expected:\n")
3446 for _, v := range expectedVariants {
3447 t.Errorf("%q\n", v)
3448 }
3449 t.Errorf(", but got:\n")
3450 for _, v := range variants {
3451 t.Errorf("%q\n", v)
3452 }
3453 }
3454
Colin Cross7113d202019-11-20 16:39:12 -08003455 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003456 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003457 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003458 if !strings.Contains(libFlags, libFoo1StubPath) {
3459 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3460 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003461
Colin Cross7113d202019-11-20 16:39:12 -08003462 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003463 cFlags := libBarCompileRule.Args["cFlags"]
3464 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3465 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3466 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3467 }
Jiyong Park37b25202018-07-11 10:49:27 +09003468}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003469
Jooyung Hanb04a4992020-03-13 18:57:35 +09003470func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003471 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003472 for _, tc := range []struct{ moduleName, expected string }{
3473 {"libc", "__LIBC_API__"},
3474 {"libfoo", "__LIBFOO_API__"},
3475 {"libfoo@1", "__LIBFOO_1_API__"},
3476 {"libfoo-v1", "__LIBFOO_V1_API__"},
3477 {"libfoo.v1", "__LIBFOO_V1_API__"},
3478 } {
3479 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3480 }
3481}
3482
Liz Kammer83cf81b2022-09-22 08:24:20 -04003483func pathsToBase(paths android.Paths) []string {
3484 var ret []string
3485 for _, p := range paths {
3486 ret = append(ret, p.Base())
3487 }
3488 return ret
3489}
3490
3491func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003492 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003493 ctx := testCc(t, `
3494 cc_library_static {
3495 name: "foo",
3496 srcs: ["foo.c"],
3497 }
3498
3499 cc_library_static {
3500 name: "bar",
3501 srcs: ["bar.c"],
3502 }
3503
3504 cc_library_shared {
3505 name: "qux",
3506 srcs: ["qux.c"],
3507 }
3508
3509 cc_library_static {
3510 name: "baz",
3511 srcs: ["baz.c"],
3512 static_libs: ["foo"],
3513 shared_libs: ["qux"],
3514 whole_static_libs: ["bar"],
3515 }`)
3516
3517 variant := "android_arm64_armv8-a_static"
3518 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3519
3520 // For static libraries, the object files of a whole static dep are included in the archive
3521 // directly
3522 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3523 t.Errorf("Expected input objects %q, got %q", w, g)
3524 }
3525
3526 // non whole static dependencies are not linked into the archive
3527 if len(arRule.Implicits) > 0 {
3528 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3529 }
3530}
3531
3532func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003533 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003534 ctx := testCc(t, `
3535 cc_library_static {
3536 name: "foo",
3537 srcs: ["foo.c"],
3538 }
3539
3540 cc_library_static {
3541 name: "bar",
3542 srcs: ["bar.c"],
3543 }
3544
3545 cc_library_shared {
3546 name: "qux",
3547 srcs: ["qux.c"],
3548 }
3549
3550 cc_library_shared {
3551 name: "baz",
3552 srcs: ["baz.c"],
3553 static_libs: ["foo"],
3554 shared_libs: ["qux"],
3555 whole_static_libs: ["bar"],
3556 }`)
3557
3558 variant := "android_arm64_armv8-a_shared"
3559 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3560 libFlags := linkRule.Args["libFlags"]
3561 // When dynamically linking, we expect static dependencies to be found on the command line
3562 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3563 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3564 }
3565 // When dynamically linking, we expect whole static dependencies to be found on the command line
3566 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3567 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3568 }
3569
3570 // When dynamically linking, we expect shared dependencies to be found on the command line
3571 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3572 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3573 }
3574
3575 // We should only have the objects from the shared library srcs, not the whole static dependencies
3576 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3577 t.Errorf("Expected input objects %q, got %q", w, g)
3578 }
3579}
3580
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003581func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003582 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003583 ctx := testCc(t, `
3584 cc_binary {
3585 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003586 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003587 static_executable: true,
3588 }`)
3589
Colin Cross7113d202019-11-20 16:39:12 -08003590 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003591 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3592 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003593 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003594 for _, lib := range systemStaticLibs {
3595 if !strings.Contains(libFlags, lib) {
3596 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3597 }
3598 }
3599 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3600 for _, lib := range systemSharedLibs {
3601 if strings.Contains(libFlags, lib) {
3602 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3603 }
3604 }
3605}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003606
3607func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003608 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003609 ctx := testCc(t, `
3610 cc_binary {
3611 name: "mybin",
3612 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003613 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003614 static_executable: true,
3615 stl: "none",
3616 }
3617
3618 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003619 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003620 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003621 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003622 stl: "none",
3623 }
3624
3625 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003626 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003627 srcs: ["foo.c"],
3628 stl: "none",
3629 stubs: {
3630 versions: ["1"],
3631 },
3632 }`)
3633
Colin Cross0de8a1e2020-09-18 14:15:30 -07003634 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3635 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003636 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003637
3638 if !reflect.DeepEqual(actual, expected) {
3639 t.Errorf("staticDeps orderings were not propagated correctly"+
3640 "\nactual: %v"+
3641 "\nexpected: %v",
3642 actual,
3643 expected,
3644 )
3645 }
3646}
Jooyung Han38002912019-05-16 04:01:54 +09003647
Jooyung Hand48f3c32019-08-23 11:18:57 +09003648func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003649 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003650 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3651 cc_library {
3652 name: "libA",
3653 srcs: ["foo.c"],
3654 shared_libs: ["libB"],
3655 stl: "none",
3656 }
3657
3658 cc_library {
3659 name: "libB",
3660 srcs: ["foo.c"],
3661 enabled: false,
3662 stl: "none",
3663 }
3664 `)
3665}
3666
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003667func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3668 bp := `
3669 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003670 name: "test_afl_fuzz_target",
3671 srcs: ["foo.c"],
3672 host_supported: true,
3673 static_libs: [
3674 "afl_fuzz_static_lib",
3675 ],
3676 shared_libs: [
3677 "afl_fuzz_shared_lib",
3678 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003679 fuzzing_frameworks: {
3680 afl: true,
3681 libfuzzer: false,
3682 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003683 }
3684 cc_library {
3685 name: "afl_fuzz_static_lib",
3686 host_supported: true,
3687 srcs: ["static_file.c"],
3688 }
3689 cc_library {
3690 name: "libfuzzer_only_static_lib",
3691 host_supported: true,
3692 srcs: ["static_file.c"],
3693 }
3694 cc_library {
3695 name: "afl_fuzz_shared_lib",
3696 host_supported: true,
3697 srcs: ["shared_file.c"],
3698 static_libs: [
3699 "second_static_lib",
3700 ],
3701 }
3702 cc_library_headers {
3703 name: "libafl_headers",
3704 vendor_available: true,
3705 host_supported: true,
3706 export_include_dirs: [
3707 "include",
3708 "instrumentation",
3709 ],
3710 }
3711 cc_object {
3712 name: "afl-compiler-rt",
3713 vendor_available: true,
3714 host_supported: true,
3715 cflags: [
3716 "-fPIC",
3717 ],
3718 srcs: [
3719 "instrumentation/afl-compiler-rt.o.c",
3720 ],
3721 }
3722 cc_library {
3723 name: "second_static_lib",
3724 host_supported: true,
3725 srcs: ["second_file.c"],
3726 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003727 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003728 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003729 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003730 srcs: [
3731 "aflpp_driver.c",
3732 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003733 }`
3734
3735 testEnv := map[string]string{
3736 "FUZZ_FRAMEWORK": "AFL",
3737 }
3738
3739 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003740
3741 checkPcGuardFlag := func(
3742 modName string, variantName string, shouldHave bool) {
3743 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3744
3745 cFlags, ok := cc.Args["cFlags"]
3746 if !ok {
3747 t.Errorf("Could not find cFlags for module %s and variant %s",
3748 modName, variantName)
3749 }
3750
3751 if strings.Contains(
3752 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3753 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3754 "Test failed for module %s and variant %s",
3755 !shouldHave, shouldHave, modName, variantName)
3756 }
3757 }
3758
Cory Barkera1da26f2022-06-07 20:12:06 +00003759 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003760 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003761
3762 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003763 checkPcGuardFlag(moduleName, variant+"_static", false)
3764 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003765
3766 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003767 checkPcGuardFlag(moduleName, variant+"_static", false)
3768 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003769
3770 ctx.ModuleForTests("afl_fuzz_shared_lib",
3771 "android_arm64_armv8-a_shared").Rule("cc")
3772 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003773 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3774}
3775
3776func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003777 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003778 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3779}
3780
3781func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003782 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003783 if runtime.GOOS != "linux" {
3784 t.Skip("requires linux")
3785 }
3786
3787 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003788}
3789
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003790// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3791// correctly.
3792func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003793 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003794 ctx := testCc(t, `
3795 cc_fuzz {
3796 name: "fuzz_smoke_test",
3797 srcs: ["foo.c"],
3798 }`)
3799
Paul Duffin075c4172019-12-19 19:06:13 +00003800 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003801 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3802}
3803
Jooyung Han38002912019-05-16 04:01:54 +09003804func assertString(t *testing.T, got, expected string) {
3805 t.Helper()
3806 if got != expected {
3807 t.Errorf("expected %q got %q", expected, got)
3808 }
3809}
3810
3811func assertArrayString(t *testing.T, got, expected []string) {
3812 t.Helper()
3813 if len(got) != len(expected) {
3814 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3815 return
3816 }
3817 for i := range got {
3818 if got[i] != expected[i] {
3819 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3820 i, expected[i], expected, got[i], got)
3821 return
3822 }
3823 }
3824}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003825
Jooyung Han0302a842019-10-30 18:43:49 +09003826func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3827 t.Helper()
3828 assertArrayString(t, android.SortedStringKeys(m), expected)
3829}
3830
Colin Crosse1bb5d02019-09-24 14:55:04 -07003831func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003832 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003833 ctx := testCc(t, `
3834 cc_defaults {
3835 name: "defaults",
3836 srcs: ["foo.c"],
3837 static: {
3838 srcs: ["bar.c"],
3839 },
3840 shared: {
3841 srcs: ["baz.c"],
3842 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003843 bazel_module: {
3844 bp2build_available: true,
3845 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003846 }
3847
3848 cc_library_static {
3849 name: "libstatic",
3850 defaults: ["defaults"],
3851 }
3852
3853 cc_library_shared {
3854 name: "libshared",
3855 defaults: ["defaults"],
3856 }
3857
3858 cc_library {
3859 name: "libboth",
3860 defaults: ["defaults"],
3861 }
3862
3863 cc_binary {
3864 name: "binary",
3865 defaults: ["defaults"],
3866 }`)
3867
Colin Cross7113d202019-11-20 16:39:12 -08003868 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003869 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3870 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3871 }
Colin Cross7113d202019-11-20 16:39:12 -08003872 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003873 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3874 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3875 }
Colin Cross7113d202019-11-20 16:39:12 -08003876 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003877 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3878 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3879 }
3880
Colin Cross7113d202019-11-20 16:39:12 -08003881 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003882 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3883 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3884 }
Colin Cross7113d202019-11-20 16:39:12 -08003885 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003886 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3887 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3888 }
3889}
Colin Crosseabaedd2020-02-06 17:01:55 -08003890
3891func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003892 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003893 bp := `
3894 cc_defaults {
3895 name: "libfoo_defaults",
3896 srcs: ["foo.c"],
3897 cppflags: ["-DFOO"],
3898 product_variables: {
3899 debuggable: {
3900 cppflags: ["-DBAR"],
3901 },
3902 },
3903 }
3904
3905 cc_library {
3906 name: "libfoo",
3907 defaults: ["libfoo_defaults"],
3908 }
3909 `
3910
Paul Duffin8567f222021-03-23 00:02:06 +00003911 result := android.GroupFixturePreparers(
3912 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003913 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003914
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003915 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3916 variables.Debuggable = BoolPtr(true)
3917 }),
3918 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003919
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003920 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003921 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003922}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003923
3924func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3925 t.Parallel()
3926 bp := `
3927 cc_library_static {
3928 name: "libfoo",
3929 srcs: ["foo.c"],
3930 whole_static_libs: ["libbar"],
3931 }
3932
3933 cc_library_static {
3934 name: "libbar",
3935 whole_static_libs: ["libmissing"],
3936 }
3937 `
3938
Paul Duffin8567f222021-03-23 00:02:06 +00003939 result := android.GroupFixturePreparers(
3940 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003941 android.PrepareForTestWithAllowMissingDependencies,
3942 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003943
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003944 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003945 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003946
Paul Duffine84b1332021-03-12 11:59:43 +00003947 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003948
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003949 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003950 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003951}
Colin Crosse9fe2942020-11-10 18:12:15 -08003952
3953func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003954 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003955 bp := `
3956 cc_binary {
3957 name: "bin",
3958 host_supported: true,
3959 shared_libs: ["libshared"],
3960 runtime_libs: ["libruntime"],
3961 srcs: [":gen"],
3962 }
3963
3964 cc_library_shared {
3965 name: "libshared",
3966 host_supported: true,
3967 shared_libs: ["libtransitive"],
3968 }
3969
3970 cc_library_shared {
3971 name: "libtransitive",
3972 host_supported: true,
3973 }
3974
3975 cc_library_shared {
3976 name: "libruntime",
3977 host_supported: true,
3978 }
3979
3980 cc_binary_host {
3981 name: "tool",
3982 srcs: ["foo.cpp"],
3983 }
3984
3985 genrule {
3986 name: "gen",
3987 tools: ["tool"],
3988 out: ["gen.cpp"],
3989 cmd: "$(location tool) $(out)",
3990 }
3991 `
3992
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003993 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003994 ctx := testCcWithConfig(t, config)
3995
3996 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3997 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3998 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3999 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4000 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4001
4002 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4003 t.Errorf("expected host bin dependency %q, got %q", w, g)
4004 }
4005
4006 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4007 t.Errorf("expected host bin dependency %q, got %q", w, g)
4008 }
4009
4010 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4011 t.Errorf("expected host bin dependency %q, got %q", w, g)
4012 }
4013
4014 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4015 t.Errorf("expected host bin dependency %q, got %q", w, g)
4016 }
4017
4018 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4019 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4020 }
4021
4022 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4023 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4024 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4025 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4026
4027 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4028 t.Errorf("expected device bin dependency %q, got %q", w, g)
4029 }
4030
4031 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4032 t.Errorf("expected device bin dependency %q, got %q", w, g)
4033 }
4034
4035 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4036 t.Errorf("expected device bin dependency %q, got %q", w, g)
4037 }
4038
4039 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4040 t.Errorf("expected device bin dependency %q, got %q", w, g)
4041 }
4042
4043 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4044 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4045 }
4046
4047}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004048
4049func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004050 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09004051 ctx := testCc(t, `
4052 cc_library_shared {
4053 name: "libclient",
4054 srcs: ["foo.c"],
4055 shared_libs: ["libfoo#1"],
4056 }
4057
4058 cc_library_shared {
4059 name: "libfoo",
4060 srcs: ["foo.c"],
4061 shared_libs: ["libbar"],
4062 export_shared_lib_headers: ["libbar"],
4063 stubs: {
4064 symbol_file: "foo.map.txt",
4065 versions: ["1", "2", "3"],
4066 },
4067 }
4068
4069 cc_library_shared {
4070 name: "libbar",
4071 export_include_dirs: ["include/libbar"],
4072 srcs: ["foo.c"],
4073 }`)
4074
4075 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4076
4077 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4078 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4079 }
4080}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004081
4082func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004083 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09004084 ctx := testCc(t, `
4085 cc_library {
4086 name: "libfoo",
4087 srcs: ["a/Foo.aidl"],
4088 aidl: { flags: ["-Werror"], },
4089 }
4090 `)
4091
4092 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4093 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4094 aidlCommand := manifest.Commands[0].GetCommand()
4095 expectedAidlFlag := "-Werror"
4096 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4097 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4098 }
4099}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004100
Jooyung Han07f70c02021-11-06 07:08:45 +09004101func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004102 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09004103 for _, tc := range []struct {
4104 name string
4105 sdkVersion string
4106 variant string
4107 expected string
4108 }{
4109 {
4110 name: "default is current",
4111 sdkVersion: "",
4112 variant: "android_arm64_armv8-a_static",
4113 expected: "platform_apis",
4114 },
4115 {
4116 name: "use sdk_version",
4117 sdkVersion: `sdk_version: "29"`,
4118 variant: "android_arm64_armv8-a_static",
4119 expected: "platform_apis",
4120 },
4121 {
4122 name: "use sdk_version(sdk variant)",
4123 sdkVersion: `sdk_version: "29"`,
4124 variant: "android_arm64_armv8-a_sdk_static",
4125 expected: "29",
4126 },
4127 {
4128 name: "use min_sdk_version",
4129 sdkVersion: `min_sdk_version: "29"`,
4130 variant: "android_arm64_armv8-a_static",
4131 expected: "29",
4132 },
4133 } {
4134 t.Run(tc.name, func(t *testing.T) {
4135 ctx := testCc(t, `
4136 cc_library {
4137 name: "libfoo",
4138 stl: "none",
4139 srcs: ["a/Foo.aidl"],
4140 `+tc.sdkVersion+`
4141 }
4142 `)
4143 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
4144 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4145 aidlCommand := manifest.Commands[0].GetCommand()
4146 expectedAidlFlag := "--min_sdk_version=" + tc.expected
4147 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4148 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4149 }
4150 })
4151 }
4152}
4153
Jiyong Parka008fb02021-03-16 17:15:53 +09004154func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004155 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09004156 ctx := testCc(t, `
4157 cc_library_shared {
4158 name: "libfoo",
4159 srcs: ["foo.c"],
4160 min_sdk_version: "29",
4161 }`)
4162
4163 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4164 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
4165}
4166
Vinh Tranf1924742022-06-24 16:40:11 -04004167func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004168 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04004169 bp := `
4170 cc_library_shared {
4171 name: "libfoo",
4172 srcs: ["foo.c"],
4173 min_sdk_version: "S",
4174 }
4175 `
4176 result := android.GroupFixturePreparers(
4177 prepareForCcTest,
4178 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4179 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4180 }),
4181 ).RunTestWithBp(t, bp)
4182 ctx := result.TestContext
4183 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4184 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4185}
4186
Paul Duffin3cb603e2021-02-19 13:57:10 +00004187func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004188 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004189
4190 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4191 // embedded newline characters alone.
4192 trimIndentingSpaces := func(s string) string {
4193 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4194 }
4195
4196 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4197 t.Helper()
4198 expected = trimIndentingSpaces(expected)
4199 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4200 if expected != actual {
4201 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4202 }
4203 }
4204
4205 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4206
4207 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4208 t.Helper()
4209 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4210 name := module.Name()
4211
4212 for _, checker := range checkers {
4213 checker(t, name, exported)
4214 }
4215 }
4216
4217 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4218 return func(t *testing.T, name string, exported FlagExporterInfo) {
4219 t.Helper()
4220 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4221 }
4222 }
4223
4224 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4225 return func(t *testing.T, name string, exported FlagExporterInfo) {
4226 t.Helper()
4227 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4228 }
4229 }
4230
4231 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4232 return func(t *testing.T, name string, exported FlagExporterInfo) {
4233 t.Helper()
4234 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4235 }
4236 }
4237
4238 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4239 return func(t *testing.T, name string, exported FlagExporterInfo) {
4240 t.Helper()
4241 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4242 }
4243 }
4244
4245 genRuleModules := `
4246 genrule {
4247 name: "genrule_foo",
4248 cmd: "generate-foo",
4249 out: [
4250 "generated_headers/foo/generated_header.h",
4251 ],
4252 export_include_dirs: [
4253 "generated_headers",
4254 ],
4255 }
4256
4257 genrule {
4258 name: "genrule_bar",
4259 cmd: "generate-bar",
4260 out: [
4261 "generated_headers/bar/generated_header.h",
4262 ],
4263 export_include_dirs: [
4264 "generated_headers",
4265 ],
4266 }
4267 `
4268
4269 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4270 ctx := testCc(t, genRuleModules+`
4271 cc_library {
4272 name: "libfoo",
4273 srcs: ["foo.c"],
4274 export_include_dirs: ["foo/standard"],
4275 export_system_include_dirs: ["foo/system"],
4276 generated_headers: ["genrule_foo"],
4277 export_generated_headers: ["genrule_foo"],
4278 }
4279
4280 cc_library {
4281 name: "libbar",
4282 srcs: ["bar.c"],
4283 shared_libs: ["libfoo"],
4284 export_include_dirs: ["bar/standard"],
4285 export_system_include_dirs: ["bar/system"],
4286 generated_headers: ["genrule_bar"],
4287 export_generated_headers: ["genrule_bar"],
4288 }
4289 `)
4290 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4291 checkIncludeDirs(t, ctx, foo,
4292 expectedIncludeDirs(`
4293 foo/standard
4294 .intermediates/genrule_foo/gen/generated_headers
4295 `),
4296 expectedSystemIncludeDirs(`foo/system`),
4297 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4298 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4299 )
4300
4301 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4302 checkIncludeDirs(t, ctx, bar,
4303 expectedIncludeDirs(`
4304 bar/standard
4305 .intermediates/genrule_bar/gen/generated_headers
4306 `),
4307 expectedSystemIncludeDirs(`bar/system`),
4308 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4309 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4310 )
4311 })
4312
4313 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4314 ctx := testCc(t, genRuleModules+`
4315 cc_library {
4316 name: "libfoo",
4317 srcs: ["foo.c"],
4318 export_include_dirs: ["foo/standard"],
4319 export_system_include_dirs: ["foo/system"],
4320 generated_headers: ["genrule_foo"],
4321 export_generated_headers: ["genrule_foo"],
4322 }
4323
4324 cc_library {
4325 name: "libbar",
4326 srcs: ["bar.c"],
4327 whole_static_libs: ["libfoo"],
4328 export_include_dirs: ["bar/standard"],
4329 export_system_include_dirs: ["bar/system"],
4330 generated_headers: ["genrule_bar"],
4331 export_generated_headers: ["genrule_bar"],
4332 }
4333 `)
4334 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4335 checkIncludeDirs(t, ctx, foo,
4336 expectedIncludeDirs(`
4337 foo/standard
4338 .intermediates/genrule_foo/gen/generated_headers
4339 `),
4340 expectedSystemIncludeDirs(`foo/system`),
4341 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4342 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4343 )
4344
4345 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4346 checkIncludeDirs(t, ctx, bar,
4347 expectedIncludeDirs(`
4348 bar/standard
4349 foo/standard
4350 .intermediates/genrule_foo/gen/generated_headers
4351 .intermediates/genrule_bar/gen/generated_headers
4352 `),
4353 expectedSystemIncludeDirs(`
4354 bar/system
4355 foo/system
4356 `),
4357 expectedGeneratedHeaders(`
4358 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4359 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4360 `),
4361 expectedOrderOnlyDeps(`
4362 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4363 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4364 `),
4365 )
4366 })
4367
Paul Duffin3cb603e2021-02-19 13:57:10 +00004368 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4369 ctx := testCc(t, genRuleModules+`
4370 cc_library_shared {
4371 name: "libfoo",
4372 srcs: [
4373 "foo.c",
4374 "b.aidl",
4375 "a.proto",
4376 ],
4377 aidl: {
4378 export_aidl_headers: true,
4379 }
4380 }
4381 `)
4382 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4383 checkIncludeDirs(t, ctx, foo,
4384 expectedIncludeDirs(`
4385 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4386 `),
4387 expectedSystemIncludeDirs(``),
4388 expectedGeneratedHeaders(`
4389 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4390 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4391 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004392 `),
4393 expectedOrderOnlyDeps(`
4394 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4395 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4396 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004397 `),
4398 )
4399 })
4400
Paul Duffin3cb603e2021-02-19 13:57:10 +00004401 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4402 ctx := testCc(t, genRuleModules+`
4403 cc_library_shared {
4404 name: "libfoo",
4405 srcs: [
4406 "foo.c",
4407 "b.aidl",
4408 "a.proto",
4409 ],
4410 proto: {
4411 export_proto_headers: true,
4412 }
4413 }
4414 `)
4415 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4416 checkIncludeDirs(t, ctx, foo,
4417 expectedIncludeDirs(`
4418 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4419 `),
4420 expectedSystemIncludeDirs(``),
4421 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004422 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4423 `),
4424 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004425 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4426 `),
4427 )
4428 })
4429
Paul Duffin33056e82021-02-19 13:49:08 +00004430 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004431 ctx := testCc(t, genRuleModules+`
4432 cc_library_shared {
4433 name: "libfoo",
4434 srcs: [
4435 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004436 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004437 "b.aidl",
4438 "a.proto",
4439 ],
4440 }
4441 `)
4442 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4443 checkIncludeDirs(t, ctx, foo,
4444 expectedIncludeDirs(`
4445 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4446 `),
4447 expectedSystemIncludeDirs(``),
4448 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004449 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004450 `),
4451 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004452 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4453 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004454 `),
4455 )
4456 })
4457}
Colin Crossae628182021-06-14 16:52:28 -07004458
4459func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004460 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004461 baseExpectedFlags := []string{
4462 "${config.ArmThumbCflags}",
4463 "${config.ArmCflags}",
4464 "${config.CommonGlobalCflags}",
4465 "${config.DeviceGlobalCflags}",
4466 "${config.ExternalCflags}",
4467 "${config.ArmToolchainCflags}",
4468 "${config.ArmArmv7ANeonCflags}",
4469 "${config.ArmGenericCflags}",
4470 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004471 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004472 }
4473
4474 expectedIncludes := []string{
4475 "external/foo/android_arm_export_include_dirs",
4476 "external/foo/lib32_export_include_dirs",
4477 "external/foo/arm_export_include_dirs",
4478 "external/foo/android_export_include_dirs",
4479 "external/foo/linux_export_include_dirs",
4480 "external/foo/export_include_dirs",
4481 "external/foo/android_arm_local_include_dirs",
4482 "external/foo/lib32_local_include_dirs",
4483 "external/foo/arm_local_include_dirs",
4484 "external/foo/android_local_include_dirs",
4485 "external/foo/linux_local_include_dirs",
4486 "external/foo/local_include_dirs",
4487 "external/foo",
4488 "external/foo/libheader1",
4489 "external/foo/libheader2",
4490 "external/foo/libwhole1",
4491 "external/foo/libwhole2",
4492 "external/foo/libstatic1",
4493 "external/foo/libstatic2",
4494 "external/foo/libshared1",
4495 "external/foo/libshared2",
4496 "external/foo/liblinux",
4497 "external/foo/libandroid",
4498 "external/foo/libarm",
4499 "external/foo/lib32",
4500 "external/foo/libandroid_arm",
4501 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004502 }
4503
4504 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4505 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4506
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004507 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004508 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004509 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004510
4511 lastIncludes := []string{
4512 "out/soong/ndk/sysroot/usr/include",
4513 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4514 }
4515
4516 combineSlices := func(slices ...[]string) []string {
4517 var ret []string
4518 for _, s := range slices {
4519 ret = append(ret, s...)
4520 }
4521 return ret
4522 }
4523
4524 testCases := []struct {
4525 name string
4526 src string
4527 expected []string
4528 }{
4529 {
4530 name: "c",
4531 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004532 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004533 },
4534 {
4535 name: "cc",
4536 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004537 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004538 },
4539 {
4540 name: "assemble",
4541 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004542 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004543 },
4544 }
4545
4546 for _, tc := range testCases {
4547 t.Run(tc.name, func(t *testing.T) {
4548 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004549 cc_library {
4550 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004551 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004552 cflags: ["-std=candcpp"],
4553 conlyflags: ["-std=conly"],
4554 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004555 local_include_dirs: ["local_include_dirs"],
4556 export_include_dirs: ["export_include_dirs"],
4557 export_system_include_dirs: ["export_system_include_dirs"],
4558 static_libs: ["libstatic1", "libstatic2"],
4559 whole_static_libs: ["libwhole1", "libwhole2"],
4560 shared_libs: ["libshared1", "libshared2"],
4561 header_libs: ["libheader1", "libheader2"],
4562 target: {
4563 android: {
4564 shared_libs: ["libandroid"],
4565 local_include_dirs: ["android_local_include_dirs"],
4566 export_include_dirs: ["android_export_include_dirs"],
4567 },
4568 android_arm: {
4569 shared_libs: ["libandroid_arm"],
4570 local_include_dirs: ["android_arm_local_include_dirs"],
4571 export_include_dirs: ["android_arm_export_include_dirs"],
4572 },
4573 linux: {
4574 shared_libs: ["liblinux"],
4575 local_include_dirs: ["linux_local_include_dirs"],
4576 export_include_dirs: ["linux_export_include_dirs"],
4577 },
4578 },
4579 multilib: {
4580 lib32: {
4581 shared_libs: ["lib32"],
4582 local_include_dirs: ["lib32_local_include_dirs"],
4583 export_include_dirs: ["lib32_export_include_dirs"],
4584 },
4585 },
4586 arch: {
4587 arm: {
4588 shared_libs: ["libarm"],
4589 local_include_dirs: ["arm_local_include_dirs"],
4590 export_include_dirs: ["arm_export_include_dirs"],
4591 },
4592 },
4593 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004594 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004595 }
4596
4597 cc_library_headers {
4598 name: "libheader1",
4599 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004600 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004601 stl: "none",
4602 }
4603
4604 cc_library_headers {
4605 name: "libheader2",
4606 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004607 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004608 stl: "none",
4609 }
Liz Kammer08572c62021-09-30 10:11:04 -04004610 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004611
Liz Kammer08572c62021-09-30 10:11:04 -04004612 libs := []string{
4613 "libstatic1",
4614 "libstatic2",
4615 "libwhole1",
4616 "libwhole2",
4617 "libshared1",
4618 "libshared2",
4619 "libandroid",
4620 "libandroid_arm",
4621 "liblinux",
4622 "lib32",
4623 "libarm",
4624 }
Colin Crossae628182021-06-14 16:52:28 -07004625
Liz Kammer08572c62021-09-30 10:11:04 -04004626 for _, lib := range libs {
4627 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004628 cc_library {
4629 name: "%s",
4630 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004631 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004632 stl: "none",
4633 }
4634 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004635 }
4636
4637 ctx := android.GroupFixturePreparers(
4638 PrepareForIntegrationTestWithCc,
4639 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4640 ).RunTest(t)
4641 // Use the arm variant instead of the arm64 variant so that it gets headers from
4642 // ndk_libandroid_support to test LateStaticLibs.
4643 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4644
4645 var includes []string
4646 flags := strings.Split(cflags, " ")
4647 for _, flag := range flags {
4648 if strings.HasPrefix(flag, "-I") {
4649 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4650 } else if flag == "-isystem" {
4651 // skip isystem, include next
4652 } else if len(flag) > 0 {
4653 includes = append(includes, flag)
4654 }
4655 }
4656
4657 android.AssertArrayString(t, "includes", tc.expected, includes)
4658 })
Colin Crossae628182021-06-14 16:52:28 -07004659 }
4660
Colin Crossae628182021-06-14 16:52:28 -07004661}
Alixb5f6d9e2022-04-20 23:00:58 +00004662
zijunzhao933e3802023-01-12 07:26:20 +00004663func TestAddnoOverride64GlobalCflags(t *testing.T) {
4664 t.Parallel()
4665 ctx := testCc(t, `
4666 cc_library_shared {
4667 name: "libclient",
4668 srcs: ["foo.c"],
4669 shared_libs: ["libfoo#1"],
4670 }
4671
4672 cc_library_shared {
4673 name: "libfoo",
4674 srcs: ["foo.c"],
4675 shared_libs: ["libbar"],
4676 export_shared_lib_headers: ["libbar"],
4677 stubs: {
4678 symbol_file: "foo.map.txt",
4679 versions: ["1", "2", "3"],
4680 },
4681 }
4682
4683 cc_library_shared {
4684 name: "libbar",
4685 export_include_dirs: ["include/libbar"],
4686 srcs: ["foo.c"],
4687 }`)
4688
4689 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4690
4691 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4692 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4693 }
4694}
4695
Alixb5f6d9e2022-04-20 23:00:58 +00004696func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004697 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004698 tests := []struct {
4699 name string
4700 clang bool
4701 BuildBrokenClangProperty bool
4702 err string
4703 }{
4704 {
4705 name: "error when clang is set to false",
4706 clang: false,
4707 err: "is no longer supported",
4708 },
4709 {
4710 name: "error when clang is set to true",
4711 clang: true,
4712 err: "property is deprecated, see Changes.md",
4713 },
4714 {
4715 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4716 clang: true,
4717 BuildBrokenClangProperty: true,
4718 },
4719 }
4720
4721 for _, test := range tests {
4722 t.Run(test.name, func(t *testing.T) {
4723 bp := fmt.Sprintf(`
4724 cc_library {
4725 name: "foo",
4726 clang: %t,
4727 }`, test.clang)
4728
4729 if test.err == "" {
4730 android.GroupFixturePreparers(
4731 prepareForCcTest,
4732 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4733 if test.BuildBrokenClangProperty {
4734 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4735 }
4736 }),
4737 ).RunTestWithBp(t, bp)
4738 } else {
4739 prepareForCcTest.
4740 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4741 RunTestWithBp(t, bp)
4742 }
4743 })
4744 }
4745}
Alix Espinoef47e542022-09-14 19:10:51 +00004746
4747func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004748 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004749 tests := []struct {
4750 name string
4751 clangAsFlags []string
4752 BuildBrokenClangAsFlags bool
4753 err string
4754 }{
4755 {
4756 name: "error when clang_asflags is set",
4757 clangAsFlags: []string{"-a", "-b"},
4758 err: "clang_asflags: property is deprecated",
4759 },
4760 {
4761 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4762 clangAsFlags: []string{"-a", "-b"},
4763 BuildBrokenClangAsFlags: true,
4764 },
4765 }
4766
4767 for _, test := range tests {
4768 t.Run(test.name, func(t *testing.T) {
4769 bp := fmt.Sprintf(`
4770 cc_library {
4771 name: "foo",
4772 clang_asflags: %s,
4773 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4774
4775 if test.err == "" {
4776 android.GroupFixturePreparers(
4777 prepareForCcTest,
4778 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4779 if test.BuildBrokenClangAsFlags {
4780 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4781 }
4782 }),
4783 ).RunTestWithBp(t, bp)
4784 } else {
4785 prepareForCcTest.
4786 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4787 RunTestWithBp(t, bp)
4788 }
4789 })
4790 }
4791}
4792
4793func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004794 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004795 tests := []struct {
4796 name string
4797 clangCFlags []string
4798 BuildBrokenClangCFlags bool
4799 err string
4800 }{
4801 {
4802 name: "error when clang_cflags is set",
4803 clangCFlags: []string{"-a", "-b"},
4804 err: "clang_cflags: property is deprecated",
4805 },
4806 {
4807 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4808 clangCFlags: []string{"-a", "-b"},
4809 BuildBrokenClangCFlags: true,
4810 },
4811 }
4812
4813 for _, test := range tests {
4814 t.Run(test.name, func(t *testing.T) {
4815 bp := fmt.Sprintf(`
4816 cc_library {
4817 name: "foo",
4818 clang_cflags: %s,
4819 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4820
4821 if test.err == "" {
4822 android.GroupFixturePreparers(
4823 prepareForCcTest,
4824 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4825 if test.BuildBrokenClangCFlags {
4826 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4827 }
4828 }),
4829 ).RunTestWithBp(t, bp)
4830 } else {
4831 prepareForCcTest.
4832 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4833 RunTestWithBp(t, bp)
4834 }
4835 })
4836 }
4837}