blob: e4dfc97ad894b69b8d0b2c7f03e2811204c5dd2d [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"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000030 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090031}
32
Paul Duffin2e6f90e2021-03-22 23:20:25 +000033var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000034 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000035 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
36 variables.DeviceVndkVersion = StringPtr("current")
37 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090038 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000039 }),
40)
41
Paul Duffin8567f222021-03-23 00:02:06 +000042// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000043//
44// See testCc for an explanation as to how to stop using this deprecated method.
45//
46// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080047func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070048 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000049 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000050 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090051}
52
Paul Duffin8567f222021-03-23 00:02:06 +000053// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000054//
Paul Duffin8567f222021-03-23 00:02:06 +000055// 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 +000056// easier to customize the test behavior.
57//
58// 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 +000059// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000060// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
61// that it did not change the test behavior unexpectedly.
62//
63// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080064func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080065 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000066 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000067 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080068}
69
Paul Duffin8567f222021-03-23 00:02:06 +000070// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000071//
72// See testCc for an explanation as to how to stop using this deprecated method.
73//
74// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080075func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080076 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000077 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090078 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080079
Colin Cross98be1bb2019-12-13 20:41:13 -080080 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080081}
82
Paul Duffin8567f222021-03-23 00:02:06 +000083// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000084//
85// See testCc for an explanation as to how to stop using this deprecated method.
86//
87// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090088func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
89 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000090 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090091 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090092 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090093
94 return testCcWithConfig(t, config)
95}
96
Paul Duffin8567f222021-03-23 00:02:06 +000097// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000098//
99// See testCc for an explanation as to how to stop using this deprecated method.
100//
101// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900102func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800104
Paul Duffin8567f222021-03-23 00:02:06 +0000105 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000106 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
107 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800108}
109
Paul Duffin8567f222021-03-23 00:02:06 +0000110// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000111//
112// See testCc for an explanation as to how to stop using this deprecated method.
113//
114// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900116 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000117 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900119 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Paul Duffin8567f222021-03-23 00:02:06 +0000124// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000125//
126// See testCc for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900130 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000131 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900132 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
133 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900134 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900135 testCcErrorWithConfig(t, pattern, config)
136 return
137}
138
Logan Chienf3511742017-10-31 18:04:35 +0800139const (
Colin Cross7113d202019-11-20 16:39:12 -0800140 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900141 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
142 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800143 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800144)
145
Paul Duffindb462dd2021-03-21 22:01:55 +0000146// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
147// running it in a fixture that requires all source files to exist.
148func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
149 android.GroupFixturePreparers(
150 PrepareForTestWithCcDefaultModules,
151 android.PrepareForTestDisallowNonExistentPaths,
152 ).RunTest(t)
153}
154
Doug Hornc32c6b02019-01-17 14:44:05 -0800155func TestFuchsiaDeps(t *testing.T) {
156 t.Helper()
157
158 bp := `
159 cc_library {
160 name: "libTest",
161 srcs: ["foo.c"],
162 target: {
163 fuchsia: {
164 srcs: ["bar.c"],
165 },
166 },
167 }`
168
Paul Duffin8567f222021-03-23 00:02:06 +0000169 result := android.GroupFixturePreparers(
170 prepareForCcTest,
171 PrepareForTestOnFuchsia,
172 ).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800173
174 rt := false
175 fb := false
176
Paul Duffinecdac8a2021-02-24 19:18:42 +0000177 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800178 implicits := ld.Implicits
179 for _, lib := range implicits {
180 if strings.Contains(lib.Rel(), "libcompiler_rt") {
181 rt = true
182 }
183
184 if strings.Contains(lib.Rel(), "libbioniccompat") {
185 fb = true
186 }
187 }
188
189 if !rt || !fb {
190 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
191 }
192}
193
194func TestFuchsiaTargetDecl(t *testing.T) {
195 t.Helper()
196
197 bp := `
198 cc_library {
199 name: "libTest",
200 srcs: ["foo.c"],
201 target: {
202 fuchsia: {
203 srcs: ["bar.c"],
204 },
205 },
206 }`
207
Paul Duffin8567f222021-03-23 00:02:06 +0000208 result := android.GroupFixturePreparers(
209 prepareForCcTest,
210 PrepareForTestOnFuchsia,
211 ).RunTestWithBp(t, bp)
Paul Duffinecdac8a2021-02-24 19:18:42 +0000212 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800213 var objs []string
214 for _, o := range ld.Inputs {
215 objs = append(objs, o.Base())
216 }
Paul Duffine84b1332021-03-12 11:59:43 +0000217 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800218}
219
Jiyong Park6a43f042017-10-12 23:05:00 +0900220func TestVendorSrc(t *testing.T) {
221 ctx := testCc(t, `
222 cc_library {
223 name: "libTest",
224 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700225 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800226 nocrt: true,
227 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900228 vendor_available: true,
229 target: {
230 vendor: {
231 srcs: ["bar.c"],
232 },
233 },
234 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900235 `)
236
Logan Chienf3511742017-10-31 18:04:35 +0800237 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900238 var objs []string
239 for _, o := range ld.Inputs {
240 objs = append(objs, o.Base())
241 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800242 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900243 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
244 }
245}
246
Justin Yun7f99ec72021-04-12 13:19:28 +0900247func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
248 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
249 partitionDefined := false
250 checkPartition := func(specific bool, partition string) {
251 if specific {
252 if expected != partition && !partitionDefined {
253 // The variant is installed to the 'partition'
254 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
255 }
256 partitionDefined = true
257 } else {
258 // The variant is not installed to the 'partition'
259 if expected == partition {
260 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
261 }
262 }
263 }
264 socSpecific := func(m *Module) bool {
265 return m.SocSpecific() || m.socSpecificModuleContext()
266 }
267 deviceSpecific := func(m *Module) bool {
268 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
269 }
270 productSpecific := func(m *Module) bool {
271 return m.ProductSpecific() || m.productSpecificModuleContext()
272 }
273 systemExtSpecific := func(m *Module) bool {
274 return m.SystemExtSpecific()
275 }
276 checkPartition(socSpecific(mod), "vendor")
277 checkPartition(deviceSpecific(mod), "odm")
278 checkPartition(productSpecific(mod), "product")
279 checkPartition(systemExtSpecific(mod), "system_ext")
280 if !partitionDefined && expected != "system" {
281 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
282 " but installed to system partition", variant, name, expected)
283 }
284}
285
286func TestInstallPartition(t *testing.T) {
287 t.Helper()
288 ctx := prepareForCcTest.RunTestWithBp(t, `
289 cc_library {
290 name: "libsystem",
291 }
292 cc_library {
293 name: "libsystem_ext",
294 system_ext_specific: true,
295 }
296 cc_library {
297 name: "libproduct",
298 product_specific: true,
299 }
300 cc_library {
301 name: "libvendor",
302 vendor: true,
303 }
304 cc_library {
305 name: "libodm",
306 device_specific: true,
307 }
308 cc_library {
309 name: "liball_available",
310 vendor_available: true,
311 product_available: true,
312 }
313 cc_library {
314 name: "libsystem_ext_all_available",
315 system_ext_specific: true,
316 vendor_available: true,
317 product_available: true,
318 }
319 cc_library {
320 name: "liball_available_odm",
321 odm_available: true,
322 product_available: true,
323 }
324 cc_library {
325 name: "libproduct_vendoravailable",
326 product_specific: true,
327 vendor_available: true,
328 }
329 cc_library {
330 name: "libproduct_odmavailable",
331 product_specific: true,
332 odm_available: true,
333 }
334 `).TestContext
335
336 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
337 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
338 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
339 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
340 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
341
342 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
343 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
344 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
345
346 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
347 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
348 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
349
350 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
351 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
352 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
353
354 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
355 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
356
357 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
358 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
359}
360
Logan Chienf3511742017-10-31 18:04:35 +0800361func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900362 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800363
Logan Chiend3c59a22018-03-29 14:08:15 +0800364 t.Helper()
365
Justin Yun0ecf0b22020-02-28 15:07:59 +0900366 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800367
368 // Check library properties.
369 lib, ok := mod.compiler.(*libraryDecorator)
370 if !ok {
371 t.Errorf("%q must have libraryDecorator", name)
372 } else if lib.baseInstaller.subDir != subDir {
373 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
374 lib.baseInstaller.subDir)
375 }
376
377 // Check VNDK properties.
378 if mod.vndkdep == nil {
379 t.Fatalf("%q must have `vndkdep`", name)
380 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700381 if !mod.IsVndk() {
382 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800383 }
384 if mod.isVndkSp() != isVndkSp {
385 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
386 }
387
388 // Check VNDK extension properties.
389 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500390 if mod.IsVndkExt() != isVndkExt {
391 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800392 }
393
394 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
395 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
396 }
397}
398
Jose Galmes0a942a02021-02-03 14:23:15 -0800399func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700400 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000401 mod := ctx.ModuleForTests(moduleName, variant)
402 outputFiles := mod.OutputFiles(t, "")
403 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900404 t.Errorf("%q must have single output\n", moduleName)
405 return
406 }
407 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900408
Bill Peckham945441c2020-08-31 16:07:58 -0700409 if include {
410 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800411 if fake {
412 if out.Rule == nil {
413 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
414 }
415 } else {
416 if out.Input.String() != outputFiles[0].String() {
417 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
418 }
Bill Peckham945441c2020-08-31 16:07:58 -0700419 }
420 } else {
421 out := singleton.MaybeOutput(snapshotPath)
422 if out.Rule != nil {
423 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
424 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900425 }
426}
427
Bill Peckham945441c2020-08-31 16:07:58 -0700428func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000429 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800430 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700431}
432
433func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000434 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800435 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
436}
437
438func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000439 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800440 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700441}
442
Jooyung Han2216fb12019-11-06 16:46:15 +0900443func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
444 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800445 content := android.ContentFromFileRuleForTests(t, params)
446 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900447 assertArrayString(t, actual, expected)
448}
449
Jooyung Han097087b2019-10-22 19:32:18 +0900450func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
451 t.Helper()
452 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
454}
455
456func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
457 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800458 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
459 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900460}
461
Logan Chienf3511742017-10-31 18:04:35 +0800462func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800463 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800464 cc_library {
465 name: "libvndk",
466 vendor_available: true,
467 vndk: {
468 enabled: true,
469 },
470 nocrt: true,
471 }
472
473 cc_library {
474 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900475 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800476 vndk: {
477 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900478 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800479 },
480 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900481 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800482 }
483
484 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900485 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800486 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900487 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800488 vndk: {
489 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900490 },
491 nocrt: true,
492 target: {
493 vendor: {
494 cflags: ["-DTEST"],
495 },
496 product: {
497 cflags: ["-DTEST"],
498 },
499 },
500 }
501
502 cc_library {
503 name: "libvndk_sp",
504 vendor_available: true,
505 vndk: {
506 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800507 support_system_process: true,
508 },
509 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900510 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800511 }
512
513 cc_library {
514 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900515 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800516 vndk: {
517 enabled: true,
518 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900519 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800520 },
521 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900522 target: {
523 vendor: {
524 suffix: "-x",
525 },
526 },
Logan Chienf3511742017-10-31 18:04:35 +0800527 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900528
529 cc_library {
530 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900531 vendor_available: true,
532 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900533 vndk: {
534 enabled: true,
535 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900536 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 },
538 nocrt: true,
539 target: {
540 vendor: {
541 suffix: "-x",
542 },
543 product: {
544 suffix: "-x",
545 },
546 },
547 }
548
Colin Crosse4e44bc2020-12-28 13:50:21 -0800549 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900550 name: "llndk.libraries.txt",
551 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800552 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900553 name: "vndkcore.libraries.txt",
554 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800555 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900556 name: "vndksp.libraries.txt",
557 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800558 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900559 name: "vndkprivate.libraries.txt",
560 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800561 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900562 name: "vndkproduct.libraries.txt",
563 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800564 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900565 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800566 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900567 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800568 `
569
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000570 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800571 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900572 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900573 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800574
575 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800576
Jooyung Han261e1582020-10-20 18:54:21 +0900577 // subdir == "" because VNDK libs are not supposed to be installed separately.
578 // They are installed as part of VNDK APEX instead.
579 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
580 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900581 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900582 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
583 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900584 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900585
Justin Yun6977e8a2020-10-29 18:24:11 +0900586 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
587 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900588
Inseob Kim1f086e22019-05-09 13:29:15 +0900589 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900590 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000591 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900592
593 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
594 "arm64", "armv8-a"))
595 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
596 "arm", "armv7-a-neon"))
597
598 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
599 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
600 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
601 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
602
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900603 variant := "android_vendor.29_arm64_armv8-a_shared"
604 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900605
Inseob Kim7f283f42020-06-01 21:53:49 +0900606 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
607
608 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
609 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900610 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
611 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900612 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
613 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900614
Jooyung Han39edb6c2019-11-06 16:53:07 +0900615 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900616 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
617 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
618 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
619 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900620 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900621
Jooyung Han097087b2019-10-22 19:32:18 +0900622 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
623 "LLNDK: libc.so",
624 "LLNDK: libdl.so",
625 "LLNDK: libft2.so",
626 "LLNDK: libm.so",
627 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900628 "VNDK-SP: libvndk_sp-x.so",
629 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900630 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900631 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900632 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900633 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900634 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900635 "VNDK-private: libvndk-private.so",
636 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900637 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900638 "VNDK-product: libc++.so",
639 "VNDK-product: libvndk_product.so",
640 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900641 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900642 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900643 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
644 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
645 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 +0900646 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 +0900647 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
648}
649
Yo Chiangbba545e2020-06-09 16:15:37 +0800650func TestVndkWithHostSupported(t *testing.T) {
651 ctx := testCc(t, `
652 cc_library {
653 name: "libvndk_host_supported",
654 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900655 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800656 vndk: {
657 enabled: true,
658 },
659 host_supported: true,
660 }
661
662 cc_library {
663 name: "libvndk_host_supported_but_disabled_on_device",
664 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900665 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800666 vndk: {
667 enabled: true,
668 },
669 host_supported: true,
670 enabled: false,
671 target: {
672 host: {
673 enabled: true,
674 }
675 }
676 }
677
Colin Crosse4e44bc2020-12-28 13:50:21 -0800678 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800679 name: "vndkcore.libraries.txt",
680 }
681 `)
682
683 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
684}
685
Jooyung Han2216fb12019-11-06 16:46:15 +0900686func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800687 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800688 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900689 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800690 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800691 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000692 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800693 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900694 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800695 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900696
697 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700698 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900699 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900700}
701
702func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800703 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900704 cc_library {
705 name: "libvndk",
706 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900707 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900708 vndk: {
709 enabled: true,
710 },
711 nocrt: true,
712 }
713
714 cc_library {
715 name: "libvndk_sp",
716 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900717 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900718 vndk: {
719 enabled: true,
720 support_system_process: true,
721 },
722 nocrt: true,
723 }
724
725 cc_library {
726 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900727 vendor_available: true,
728 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900729 vndk: {
730 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900731 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900732 },
733 nocrt: true,
734 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900735
Colin Crosse4e44bc2020-12-28 13:50:21 -0800736 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900737 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800738 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900739 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800740 `
741
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000742 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800743 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900744 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800745 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
746
747 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
748
749 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900750
Jooyung Han2216fb12019-11-06 16:46:15 +0900751 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900752}
753
Chris Parsons79d66a52020-06-05 17:26:16 -0400754func TestDataLibs(t *testing.T) {
755 bp := `
756 cc_test_library {
757 name: "test_lib",
758 srcs: ["test_lib.cpp"],
759 gtest: false,
760 }
761
762 cc_test {
763 name: "main_test",
764 data_libs: ["test_lib"],
765 gtest: false,
766 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400767 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400768
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000769 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400770 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900771 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400772 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
773
774 ctx := testCcWithConfig(t, config)
775 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
776 testBinary := module.(*Module).linker.(*testBinary)
777 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
778 if err != nil {
779 t.Errorf("Expected cc_test to produce output files, error: %s", err)
780 return
781 }
782 if len(outputFiles) != 1 {
783 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
784 return
785 }
786 if len(testBinary.dataPaths()) != 1 {
787 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
788 return
789 }
790
791 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400792 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400793
794 if !strings.HasSuffix(outputPath, "/main_test") {
795 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
796 return
797 }
798 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
799 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
800 return
801 }
802}
803
Chris Parsons216e10a2020-07-09 17:12:52 -0400804func TestDataLibsRelativeInstallPath(t *testing.T) {
805 bp := `
806 cc_test_library {
807 name: "test_lib",
808 srcs: ["test_lib.cpp"],
809 relative_install_path: "foo/bar/baz",
810 gtest: false,
811 }
812
813 cc_test {
814 name: "main_test",
815 data_libs: ["test_lib"],
816 gtest: false,
817 }
818 `
819
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000820 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400821 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900822 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400823 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
824
825 ctx := testCcWithConfig(t, config)
826 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
827 testBinary := module.(*Module).linker.(*testBinary)
828 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
829 if err != nil {
830 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
831 }
832 if len(outputFiles) != 1 {
833 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
834 }
835 if len(testBinary.dataPaths()) != 1 {
836 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
837 }
838
839 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400840
841 if !strings.HasSuffix(outputPath, "/main_test") {
842 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
843 }
Colin Crossaa255532020-07-03 13:18:24 -0700844 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400845 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
846 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400847 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400848 }
849}
850
Jooyung Han0302a842019-10-30 18:43:49 +0900851func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900852 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900853 cc_library {
854 name: "libvndk",
855 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900856 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900857 vndk: {
858 enabled: true,
859 },
860 nocrt: true,
861 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900862 cc_library {
863 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900864 vendor_available: true,
865 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900866 vndk: {
867 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900868 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900869 },
870 nocrt: true,
871 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800872
873 cc_library {
874 name: "libllndk",
875 llndk_stubs: "libllndk.llndk",
876 }
877
878 llndk_library {
879 name: "libllndk.llndk",
880 symbol_file: "",
881 export_llndk_headers: ["libllndk_headers"],
882 }
883
884 llndk_headers {
885 name: "libllndk_headers",
886 export_include_dirs: ["include"],
887 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900888 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900889
890 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
891 "LLNDK: libc.so",
892 "LLNDK: libdl.so",
893 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800894 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900895 "LLNDK: libm.so",
896 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900897 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900898 "VNDK-core: libvndk.so",
899 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900900 "VNDK-private: libvndk-private.so",
901 "VNDK-product: libc++.so",
902 "VNDK-product: libvndk-private.so",
903 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900904 })
Logan Chienf3511742017-10-31 18:04:35 +0800905}
906
Justin Yun63e9ec72020-10-29 16:49:43 +0900907func TestVndkModuleError(t *testing.T) {
908 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900909 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900910 cc_library {
911 name: "libvndk",
912 vndk: {
913 enabled: true,
914 },
915 nocrt: true,
916 }
917 `)
918
Justin Yunc0d8c492021-01-07 17:45:31 +0900919 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900920 cc_library {
921 name: "libvndk",
922 product_available: true,
923 vndk: {
924 enabled: true,
925 },
926 nocrt: true,
927 }
928 `)
929
Justin Yun6977e8a2020-10-29 18:24:11 +0900930 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
931 cc_library {
932 name: "libvndkprop",
933 vendor_available: true,
934 product_available: true,
935 vndk: {
936 enabled: true,
937 },
938 nocrt: true,
939 target: {
940 vendor: {
941 cflags: ["-DTEST",],
942 },
943 },
944 }
945 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900946}
947
Logan Chiend3c59a22018-03-29 14:08:15 +0800948func TestVndkDepError(t *testing.T) {
949 // Check whether an error is emitted when a VNDK lib depends on a system lib.
950 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
951 cc_library {
952 name: "libvndk",
953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900954 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800955 vndk: {
956 enabled: true,
957 },
958 shared_libs: ["libfwk"], // Cause error
959 nocrt: true,
960 }
961
962 cc_library {
963 name: "libfwk",
964 nocrt: true,
965 }
966 `)
967
968 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
969 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
970 cc_library {
971 name: "libvndk",
972 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900973 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800974 vndk: {
975 enabled: true,
976 },
977 shared_libs: ["libvendor"], // Cause error
978 nocrt: true,
979 }
980
981 cc_library {
982 name: "libvendor",
983 vendor: true,
984 nocrt: true,
985 }
986 `)
987
988 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
989 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
990 cc_library {
991 name: "libvndk_sp",
992 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900993 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800994 vndk: {
995 enabled: true,
996 support_system_process: true,
997 },
998 shared_libs: ["libfwk"], // Cause error
999 nocrt: true,
1000 }
1001
1002 cc_library {
1003 name: "libfwk",
1004 nocrt: true,
1005 }
1006 `)
1007
1008 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1009 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1010 cc_library {
1011 name: "libvndk_sp",
1012 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001013 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001014 vndk: {
1015 enabled: true,
1016 support_system_process: true,
1017 },
1018 shared_libs: ["libvendor"], // Cause error
1019 nocrt: true,
1020 }
1021
1022 cc_library {
1023 name: "libvendor",
1024 vendor: true,
1025 nocrt: true,
1026 }
1027 `)
1028
1029 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1030 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1031 cc_library {
1032 name: "libvndk_sp",
1033 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001034 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001035 vndk: {
1036 enabled: true,
1037 support_system_process: true,
1038 },
1039 shared_libs: ["libvndk"], // Cause error
1040 nocrt: true,
1041 }
1042
1043 cc_library {
1044 name: "libvndk",
1045 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001046 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001047 vndk: {
1048 enabled: true,
1049 },
1050 nocrt: true,
1051 }
1052 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001053
1054 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1055 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1056 cc_library {
1057 name: "libvndk",
1058 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001059 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001060 vndk: {
1061 enabled: true,
1062 },
1063 shared_libs: ["libnonvndk"],
1064 nocrt: true,
1065 }
1066
1067 cc_library {
1068 name: "libnonvndk",
1069 vendor_available: true,
1070 nocrt: true,
1071 }
1072 `)
1073
1074 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1075 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1076 cc_library {
1077 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001078 vendor_available: true,
1079 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001080 vndk: {
1081 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001082 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001083 },
1084 shared_libs: ["libnonvndk"],
1085 nocrt: true,
1086 }
1087
1088 cc_library {
1089 name: "libnonvndk",
1090 vendor_available: true,
1091 nocrt: true,
1092 }
1093 `)
1094
1095 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1096 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1097 cc_library {
1098 name: "libvndksp",
1099 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001100 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001101 vndk: {
1102 enabled: true,
1103 support_system_process: true,
1104 },
1105 shared_libs: ["libnonvndk"],
1106 nocrt: true,
1107 }
1108
1109 cc_library {
1110 name: "libnonvndk",
1111 vendor_available: true,
1112 nocrt: true,
1113 }
1114 `)
1115
1116 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1117 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1118 cc_library {
1119 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001120 vendor_available: true,
1121 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001122 vndk: {
1123 enabled: true,
1124 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001125 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001126 },
1127 shared_libs: ["libnonvndk"],
1128 nocrt: true,
1129 }
1130
1131 cc_library {
1132 name: "libnonvndk",
1133 vendor_available: true,
1134 nocrt: true,
1135 }
1136 `)
1137}
1138
1139func TestDoubleLoadbleDep(t *testing.T) {
1140 // okay to link : LLNDK -> double_loadable VNDK
1141 testCc(t, `
1142 cc_library {
1143 name: "libllndk",
1144 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001145 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001146 }
1147
1148 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001149 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001150 symbol_file: "",
1151 }
1152
1153 cc_library {
1154 name: "libdoubleloadable",
1155 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001156 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001157 vndk: {
1158 enabled: true,
1159 },
1160 double_loadable: true,
1161 }
1162 `)
1163 // okay to link : LLNDK -> VNDK-SP
1164 testCc(t, `
1165 cc_library {
1166 name: "libllndk",
1167 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001168 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001169 }
1170
1171 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001172 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001173 symbol_file: "",
1174 }
1175
1176 cc_library {
1177 name: "libvndksp",
1178 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001179 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001180 vndk: {
1181 enabled: true,
1182 support_system_process: true,
1183 },
1184 }
1185 `)
1186 // okay to link : double_loadable -> double_loadable
1187 testCc(t, `
1188 cc_library {
1189 name: "libdoubleloadable1",
1190 shared_libs: ["libdoubleloadable2"],
1191 vendor_available: true,
1192 double_loadable: true,
1193 }
1194
1195 cc_library {
1196 name: "libdoubleloadable2",
1197 vendor_available: true,
1198 double_loadable: true,
1199 }
1200 `)
1201 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1202 testCc(t, `
1203 cc_library {
1204 name: "libdoubleloadable",
1205 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001206 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001207 vndk: {
1208 enabled: true,
1209 },
1210 double_loadable: true,
1211 shared_libs: ["libnondoubleloadable"],
1212 }
1213
1214 cc_library {
1215 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001216 vendor_available: true,
1217 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001218 vndk: {
1219 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001220 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 },
1222 double_loadable: true,
1223 }
1224 `)
1225 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1226 testCc(t, `
1227 cc_library {
1228 name: "libllndk",
1229 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001230 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001231 }
1232
1233 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001234 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001235 symbol_file: "",
1236 }
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) {
1253 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1254 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1255 cc_library {
1256 name: "libllndk",
1257 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001258 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001259 }
1260
1261 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001262 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001263 symbol_file: "",
1264 }
1265
1266 cc_library {
1267 name: "libnondoubleloadable",
1268 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001269 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001270 vndk: {
1271 enabled: true,
1272 },
1273 }
1274 `)
1275
1276 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1277 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1278 cc_library {
1279 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001280 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001281 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001282 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001283 }
1284
1285 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001286 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001287 symbol_file: "",
1288 }
1289
1290 cc_library {
1291 name: "libnondoubleloadable",
1292 vendor_available: true,
1293 }
1294 `)
1295
Jooyung Hana70f0672019-01-18 15:20:43 +09001296 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1297 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1298 cc_library {
1299 name: "libllndk",
1300 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001301 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001302 }
1303
1304 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001305 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001306 symbol_file: "",
1307 }
1308
1309 cc_library {
1310 name: "libcoreonly",
1311 shared_libs: ["libvendoravailable"],
1312 }
1313
1314 // indirect dependency of LLNDK
1315 cc_library {
1316 name: "libvendoravailable",
1317 vendor_available: true,
1318 }
1319 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001320
1321 // The error is not from 'client' but from 'libllndk'
1322 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1323 cc_library {
1324 name: "client",
1325 vendor_available: true,
1326 double_loadable: true,
1327 shared_libs: ["libllndk"],
1328 }
1329 cc_library {
1330 name: "libllndk",
1331 shared_libs: ["libnondoubleloadable"],
1332 llndk_stubs: "libllndk.llndk",
1333 }
1334 llndk_library {
1335 name: "libllndk.llndk",
1336 symbol_file: "",
1337 }
1338 cc_library {
1339 name: "libnondoubleloadable",
1340 vendor_available: true,
1341 }
1342 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001343}
1344
Jooyung Han479ca172020-10-19 18:51:07 +09001345func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1346 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1347 cc_library {
1348 name: "libvndksp",
1349 shared_libs: ["libanothervndksp"],
1350 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001351 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001352 vndk: {
1353 enabled: true,
1354 support_system_process: true,
1355 }
1356 }
1357
1358 cc_library {
1359 name: "libllndk",
1360 shared_libs: ["libanothervndksp"],
1361 }
1362
1363 llndk_library {
1364 name: "libllndk",
1365 symbol_file: "",
1366 }
1367
1368 cc_library {
1369 name: "libanothervndksp",
1370 vendor_available: true,
1371 }
1372 `)
1373}
1374
Logan Chienf3511742017-10-31 18:04:35 +08001375func TestVndkExt(t *testing.T) {
1376 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001377 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001378 cc_library {
1379 name: "libvndk",
1380 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001381 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001382 vndk: {
1383 enabled: true,
1384 },
1385 nocrt: true,
1386 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001387 cc_library {
1388 name: "libvndk2",
1389 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001390 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001391 vndk: {
1392 enabled: true,
1393 },
1394 target: {
1395 vendor: {
1396 suffix: "-suffix",
1397 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001398 product: {
1399 suffix: "-suffix",
1400 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001401 },
1402 nocrt: true,
1403 }
Logan Chienf3511742017-10-31 18:04:35 +08001404
1405 cc_library {
1406 name: "libvndk_ext",
1407 vendor: true,
1408 vndk: {
1409 enabled: true,
1410 extends: "libvndk",
1411 },
1412 nocrt: true,
1413 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001414
1415 cc_library {
1416 name: "libvndk2_ext",
1417 vendor: true,
1418 vndk: {
1419 enabled: true,
1420 extends: "libvndk2",
1421 },
1422 nocrt: true,
1423 }
Logan Chienf3511742017-10-31 18:04:35 +08001424
Justin Yun0ecf0b22020-02-28 15:07:59 +09001425 cc_library {
1426 name: "libvndk_ext_product",
1427 product_specific: true,
1428 vndk: {
1429 enabled: true,
1430 extends: "libvndk",
1431 },
1432 nocrt: true,
1433 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001434
Justin Yun0ecf0b22020-02-28 15:07:59 +09001435 cc_library {
1436 name: "libvndk2_ext_product",
1437 product_specific: true,
1438 vndk: {
1439 enabled: true,
1440 extends: "libvndk2",
1441 },
1442 nocrt: true,
1443 }
1444 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001445 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001446 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1447 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001448 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001449
1450 ctx := testCcWithConfig(t, config)
1451
1452 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1453 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1454
1455 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1456 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1457
1458 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1459 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001460}
1461
Logan Chiend3c59a22018-03-29 14:08:15 +08001462func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001463 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1464 ctx := testCcNoVndk(t, `
1465 cc_library {
1466 name: "libvndk",
1467 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001468 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001469 vndk: {
1470 enabled: true,
1471 },
1472 nocrt: true,
1473 }
1474
1475 cc_library {
1476 name: "libvndk_ext",
1477 vendor: true,
1478 vndk: {
1479 enabled: true,
1480 extends: "libvndk",
1481 },
1482 nocrt: true,
1483 }
1484 `)
1485
1486 // Ensures that the core variant of "libvndk_ext" can be found.
1487 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1488 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1489 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1490 }
1491}
1492
Justin Yun0ecf0b22020-02-28 15:07:59 +09001493func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1494 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001495 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001496 cc_library {
1497 name: "libvndk",
1498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001499 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001500 vndk: {
1501 enabled: true,
1502 },
1503 nocrt: true,
1504 }
1505
1506 cc_library {
1507 name: "libvndk_ext_product",
1508 product_specific: true,
1509 vndk: {
1510 enabled: true,
1511 extends: "libvndk",
1512 },
1513 nocrt: true,
1514 }
1515 `)
1516
1517 // Ensures that the core variant of "libvndk_ext_product" can be found.
1518 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1519 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1520 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1521 }
1522}
1523
Logan Chienf3511742017-10-31 18:04:35 +08001524func TestVndkExtError(t *testing.T) {
1525 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001526 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001527 cc_library {
1528 name: "libvndk",
1529 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001530 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001531 vndk: {
1532 enabled: true,
1533 },
1534 nocrt: true,
1535 }
1536
1537 cc_library {
1538 name: "libvndk_ext",
1539 vndk: {
1540 enabled: true,
1541 extends: "libvndk",
1542 },
1543 nocrt: true,
1544 }
1545 `)
1546
1547 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1548 cc_library {
1549 name: "libvndk",
1550 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001551 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001552 vndk: {
1553 enabled: true,
1554 },
1555 nocrt: true,
1556 }
1557
1558 cc_library {
1559 name: "libvndk_ext",
1560 vendor: true,
1561 vndk: {
1562 enabled: true,
1563 },
1564 nocrt: true,
1565 }
1566 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001567
1568 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1569 cc_library {
1570 name: "libvndk",
1571 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001572 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001573 vndk: {
1574 enabled: true,
1575 },
1576 nocrt: true,
1577 }
1578
1579 cc_library {
1580 name: "libvndk_ext_product",
1581 product_specific: true,
1582 vndk: {
1583 enabled: true,
1584 },
1585 nocrt: true,
1586 }
1587 `)
1588
1589 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1590 cc_library {
1591 name: "libvndk",
1592 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001593 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001594 vndk: {
1595 enabled: true,
1596 },
1597 nocrt: true,
1598 }
1599
1600 cc_library {
1601 name: "libvndk_ext_product",
1602 product_specific: true,
1603 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001604 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001605 vndk: {
1606 enabled: true,
1607 extends: "libvndk",
1608 },
1609 nocrt: true,
1610 }
1611 `)
Logan Chienf3511742017-10-31 18:04:35 +08001612}
1613
1614func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1615 // This test ensures an error is emitted for inconsistent support_system_process.
1616 testCcError(t, "module \".*\" with mismatched support_system_process", `
1617 cc_library {
1618 name: "libvndk",
1619 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001620 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001621 vndk: {
1622 enabled: true,
1623 },
1624 nocrt: true,
1625 }
1626
1627 cc_library {
1628 name: "libvndk_sp_ext",
1629 vendor: true,
1630 vndk: {
1631 enabled: true,
1632 extends: "libvndk",
1633 support_system_process: true,
1634 },
1635 nocrt: true,
1636 }
1637 `)
1638
1639 testCcError(t, "module \".*\" with mismatched support_system_process", `
1640 cc_library {
1641 name: "libvndk_sp",
1642 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001643 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001644 vndk: {
1645 enabled: true,
1646 support_system_process: true,
1647 },
1648 nocrt: true,
1649 }
1650
1651 cc_library {
1652 name: "libvndk_ext",
1653 vendor: true,
1654 vndk: {
1655 enabled: true,
1656 extends: "libvndk_sp",
1657 },
1658 nocrt: true,
1659 }
1660 `)
1661}
1662
1663func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001664 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001665 // with `private: true`.
1666 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001667 cc_library {
1668 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001669 vendor_available: true,
1670 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001671 vndk: {
1672 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001673 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001674 },
1675 nocrt: true,
1676 }
1677
1678 cc_library {
1679 name: "libvndk_ext",
1680 vendor: true,
1681 vndk: {
1682 enabled: true,
1683 extends: "libvndk",
1684 },
1685 nocrt: true,
1686 }
1687 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001688
Justin Yunfd9e8042020-12-23 18:23:14 +09001689 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001690 cc_library {
1691 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001692 vendor_available: true,
1693 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001694 vndk: {
1695 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001696 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001697 },
1698 nocrt: true,
1699 }
1700
1701 cc_library {
1702 name: "libvndk_ext_product",
1703 product_specific: true,
1704 vndk: {
1705 enabled: true,
1706 extends: "libvndk",
1707 },
1708 nocrt: true,
1709 }
1710 `)
Logan Chienf3511742017-10-31 18:04:35 +08001711}
1712
Logan Chiend3c59a22018-03-29 14:08:15 +08001713func TestVendorModuleUseVndkExt(t *testing.T) {
1714 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001715 testCc(t, `
1716 cc_library {
1717 name: "libvndk",
1718 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001719 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001720 vndk: {
1721 enabled: true,
1722 },
1723 nocrt: true,
1724 }
1725
1726 cc_library {
1727 name: "libvndk_ext",
1728 vendor: true,
1729 vndk: {
1730 enabled: true,
1731 extends: "libvndk",
1732 },
1733 nocrt: true,
1734 }
1735
1736 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001737 name: "libvndk_sp",
1738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001739 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001740 vndk: {
1741 enabled: true,
1742 support_system_process: true,
1743 },
1744 nocrt: true,
1745 }
1746
1747 cc_library {
1748 name: "libvndk_sp_ext",
1749 vendor: true,
1750 vndk: {
1751 enabled: true,
1752 extends: "libvndk_sp",
1753 support_system_process: true,
1754 },
1755 nocrt: true,
1756 }
1757
1758 cc_library {
1759 name: "libvendor",
1760 vendor: true,
1761 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1762 nocrt: true,
1763 }
1764 `)
1765}
1766
Logan Chiend3c59a22018-03-29 14:08:15 +08001767func TestVndkExtUseVendorLib(t *testing.T) {
1768 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001769 testCc(t, `
1770 cc_library {
1771 name: "libvndk",
1772 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001773 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001774 vndk: {
1775 enabled: true,
1776 },
1777 nocrt: true,
1778 }
1779
1780 cc_library {
1781 name: "libvndk_ext",
1782 vendor: true,
1783 vndk: {
1784 enabled: true,
1785 extends: "libvndk",
1786 },
1787 shared_libs: ["libvendor"],
1788 nocrt: true,
1789 }
1790
1791 cc_library {
1792 name: "libvendor",
1793 vendor: true,
1794 nocrt: true,
1795 }
1796 `)
Logan Chienf3511742017-10-31 18:04:35 +08001797
Logan Chiend3c59a22018-03-29 14:08:15 +08001798 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1799 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001800 cc_library {
1801 name: "libvndk_sp",
1802 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001803 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001804 vndk: {
1805 enabled: true,
1806 support_system_process: true,
1807 },
1808 nocrt: true,
1809 }
1810
1811 cc_library {
1812 name: "libvndk_sp_ext",
1813 vendor: true,
1814 vndk: {
1815 enabled: true,
1816 extends: "libvndk_sp",
1817 support_system_process: true,
1818 },
1819 shared_libs: ["libvendor"], // Cause an error
1820 nocrt: true,
1821 }
1822
1823 cc_library {
1824 name: "libvendor",
1825 vendor: true,
1826 nocrt: true,
1827 }
1828 `)
1829}
1830
Justin Yun0ecf0b22020-02-28 15:07:59 +09001831func TestProductVndkExtDependency(t *testing.T) {
1832 bp := `
1833 cc_library {
1834 name: "libvndk",
1835 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001836 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001837 vndk: {
1838 enabled: true,
1839 },
1840 nocrt: true,
1841 }
1842
1843 cc_library {
1844 name: "libvndk_ext_product",
1845 product_specific: true,
1846 vndk: {
1847 enabled: true,
1848 extends: "libvndk",
1849 },
1850 shared_libs: ["libproduct_for_vndklibs"],
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk_sp",
1856 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001857 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001858 vndk: {
1859 enabled: true,
1860 support_system_process: true,
1861 },
1862 nocrt: true,
1863 }
1864
1865 cc_library {
1866 name: "libvndk_sp_ext_product",
1867 product_specific: true,
1868 vndk: {
1869 enabled: true,
1870 extends: "libvndk_sp",
1871 support_system_process: true,
1872 },
1873 shared_libs: ["libproduct_for_vndklibs"],
1874 nocrt: true,
1875 }
1876
1877 cc_library {
1878 name: "libproduct",
1879 product_specific: true,
1880 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1881 nocrt: true,
1882 }
1883
1884 cc_library {
1885 name: "libproduct_for_vndklibs",
1886 product_specific: true,
1887 nocrt: true,
1888 }
1889 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001890 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001891 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1892 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001893 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001894
1895 testCcWithConfig(t, config)
1896}
1897
Logan Chiend3c59a22018-03-29 14:08:15 +08001898func TestVndkSpExtUseVndkError(t *testing.T) {
1899 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1900 // library.
1901 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1902 cc_library {
1903 name: "libvndk",
1904 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001905 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001906 vndk: {
1907 enabled: true,
1908 },
1909 nocrt: true,
1910 }
1911
1912 cc_library {
1913 name: "libvndk_sp",
1914 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001915 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001916 vndk: {
1917 enabled: true,
1918 support_system_process: true,
1919 },
1920 nocrt: true,
1921 }
1922
1923 cc_library {
1924 name: "libvndk_sp_ext",
1925 vendor: true,
1926 vndk: {
1927 enabled: true,
1928 extends: "libvndk_sp",
1929 support_system_process: true,
1930 },
1931 shared_libs: ["libvndk"], // Cause an error
1932 nocrt: true,
1933 }
1934 `)
1935
1936 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1937 // library.
1938 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1939 cc_library {
1940 name: "libvndk",
1941 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001942 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001943 vndk: {
1944 enabled: true,
1945 },
1946 nocrt: true,
1947 }
1948
1949 cc_library {
1950 name: "libvndk_ext",
1951 vendor: true,
1952 vndk: {
1953 enabled: true,
1954 extends: "libvndk",
1955 },
1956 nocrt: true,
1957 }
1958
1959 cc_library {
1960 name: "libvndk_sp",
1961 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001962 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001963 vndk: {
1964 enabled: true,
1965 support_system_process: true,
1966 },
1967 nocrt: true,
1968 }
1969
1970 cc_library {
1971 name: "libvndk_sp_ext",
1972 vendor: true,
1973 vndk: {
1974 enabled: true,
1975 extends: "libvndk_sp",
1976 support_system_process: true,
1977 },
1978 shared_libs: ["libvndk_ext"], // Cause an error
1979 nocrt: true,
1980 }
1981 `)
1982}
1983
1984func TestVndkUseVndkExtError(t *testing.T) {
1985 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1986 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001987 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1988 cc_library {
1989 name: "libvndk",
1990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001991 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001992 vndk: {
1993 enabled: true,
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_ext",
2000 vendor: true,
2001 vndk: {
2002 enabled: true,
2003 extends: "libvndk",
2004 },
2005 nocrt: true,
2006 }
2007
2008 cc_library {
2009 name: "libvndk2",
2010 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002011 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002012 vndk: {
2013 enabled: true,
2014 },
2015 shared_libs: ["libvndk_ext"],
2016 nocrt: true,
2017 }
2018 `)
2019
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002020 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002021 cc_library {
2022 name: "libvndk",
2023 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002024 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002025 vndk: {
2026 enabled: true,
2027 },
2028 nocrt: true,
2029 }
2030
2031 cc_library {
2032 name: "libvndk_ext",
2033 vendor: true,
2034 vndk: {
2035 enabled: true,
2036 extends: "libvndk",
2037 },
2038 nocrt: true,
2039 }
2040
2041 cc_library {
2042 name: "libvndk2",
2043 vendor_available: true,
2044 vndk: {
2045 enabled: true,
2046 },
2047 target: {
2048 vendor: {
2049 shared_libs: ["libvndk_ext"],
2050 },
2051 },
2052 nocrt: true,
2053 }
2054 `)
2055
2056 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2057 cc_library {
2058 name: "libvndk_sp",
2059 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002060 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002061 vndk: {
2062 enabled: true,
2063 support_system_process: true,
2064 },
2065 nocrt: true,
2066 }
2067
2068 cc_library {
2069 name: "libvndk_sp_ext",
2070 vendor: true,
2071 vndk: {
2072 enabled: true,
2073 extends: "libvndk_sp",
2074 support_system_process: true,
2075 },
2076 nocrt: true,
2077 }
2078
2079 cc_library {
2080 name: "libvndk_sp_2",
2081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002083 vndk: {
2084 enabled: true,
2085 support_system_process: true,
2086 },
2087 shared_libs: ["libvndk_sp_ext"],
2088 nocrt: true,
2089 }
2090 `)
2091
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002092 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002093 cc_library {
2094 name: "libvndk_sp",
2095 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002096 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002097 vndk: {
2098 enabled: true,
2099 },
2100 nocrt: true,
2101 }
2102
2103 cc_library {
2104 name: "libvndk_sp_ext",
2105 vendor: true,
2106 vndk: {
2107 enabled: true,
2108 extends: "libvndk_sp",
2109 },
2110 nocrt: true,
2111 }
2112
2113 cc_library {
2114 name: "libvndk_sp2",
2115 vendor_available: true,
2116 vndk: {
2117 enabled: true,
2118 },
2119 target: {
2120 vendor: {
2121 shared_libs: ["libvndk_sp_ext"],
2122 },
2123 },
2124 nocrt: true,
2125 }
2126 `)
2127}
2128
Justin Yun5f7f7e82019-11-18 19:52:14 +09002129func TestEnforceProductVndkVersion(t *testing.T) {
2130 bp := `
2131 cc_library {
2132 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002133 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002134 }
2135 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002136 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002137 symbol_file: "",
2138 }
2139 cc_library {
2140 name: "libvndk",
2141 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002142 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002143 vndk: {
2144 enabled: true,
2145 },
2146 nocrt: true,
2147 }
2148 cc_library {
2149 name: "libvndk_sp",
2150 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002151 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002152 vndk: {
2153 enabled: true,
2154 support_system_process: true,
2155 },
2156 nocrt: true,
2157 }
2158 cc_library {
2159 name: "libva",
2160 vendor_available: true,
2161 nocrt: true,
2162 }
2163 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002164 name: "libpa",
2165 product_available: true,
2166 nocrt: true,
2167 }
2168 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002169 name: "libboth_available",
2170 vendor_available: true,
2171 product_available: true,
2172 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002173 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002174 target: {
2175 vendor: {
2176 suffix: "-vendor",
2177 },
2178 product: {
2179 suffix: "-product",
2180 },
2181 }
2182 }
2183 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002184 name: "libproduct_va",
2185 product_specific: true,
2186 vendor_available: true,
2187 nocrt: true,
2188 }
2189 cc_library {
2190 name: "libprod",
2191 product_specific: true,
2192 shared_libs: [
2193 "libllndk",
2194 "libvndk",
2195 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002196 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002197 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002198 "libproduct_va",
2199 ],
2200 nocrt: true,
2201 }
2202 cc_library {
2203 name: "libvendor",
2204 vendor: true,
2205 shared_libs: [
2206 "libllndk",
2207 "libvndk",
2208 "libvndk_sp",
2209 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002210 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002211 "libproduct_va",
2212 ],
2213 nocrt: true,
2214 }
2215 `
2216
Paul Duffin8567f222021-03-23 00:02:06 +00002217 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002218
Jooyung Han261e1582020-10-20 18:54:21 +09002219 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2220 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002221
2222 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2223 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2224
2225 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2226 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002227
2228 ensureStringContains := func(t *testing.T, str string, substr string) {
2229 t.Helper()
2230 if !strings.Contains(str, substr) {
2231 t.Errorf("%q is not found in %v", substr, str)
2232 }
2233 }
2234 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2235 t.Helper()
2236 if strings.Contains(str, substr) {
2237 t.Errorf("%q is found in %v", substr, str)
2238 }
2239 }
2240
2241 // _static variant is used since _shared reuses *.o from the static variant
2242 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2243 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2244
2245 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2246 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2247 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2248 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2249
2250 product_cflags := product_static.Rule("cc").Args["cFlags"]
2251 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2252 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2253 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002254}
2255
2256func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002257 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002258 cc_library {
2259 name: "libprod",
2260 product_specific: true,
2261 shared_libs: [
2262 "libvendor",
2263 ],
2264 nocrt: true,
2265 }
2266 cc_library {
2267 name: "libvendor",
2268 vendor: true,
2269 nocrt: true,
2270 }
2271 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002272 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002273 cc_library {
2274 name: "libprod",
2275 product_specific: true,
2276 shared_libs: [
2277 "libsystem",
2278 ],
2279 nocrt: true,
2280 }
2281 cc_library {
2282 name: "libsystem",
2283 nocrt: true,
2284 }
2285 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002286 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002287 cc_library {
2288 name: "libprod",
2289 product_specific: true,
2290 shared_libs: [
2291 "libva",
2292 ],
2293 nocrt: true,
2294 }
2295 cc_library {
2296 name: "libva",
2297 vendor_available: true,
2298 nocrt: true,
2299 }
2300 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002301 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002302 cc_library {
2303 name: "libprod",
2304 product_specific: true,
2305 shared_libs: [
2306 "libvndk_private",
2307 ],
2308 nocrt: true,
2309 }
2310 cc_library {
2311 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002312 vendor_available: true,
2313 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002314 vndk: {
2315 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002316 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002317 },
2318 nocrt: true,
2319 }
2320 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002321 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002322 cc_library {
2323 name: "libprod",
2324 product_specific: true,
2325 shared_libs: [
2326 "libsystem_ext",
2327 ],
2328 nocrt: true,
2329 }
2330 cc_library {
2331 name: "libsystem_ext",
2332 system_ext_specific: true,
2333 nocrt: true,
2334 }
2335 `)
2336 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2337 cc_library {
2338 name: "libsystem",
2339 shared_libs: [
2340 "libproduct_va",
2341 ],
2342 nocrt: true,
2343 }
2344 cc_library {
2345 name: "libproduct_va",
2346 product_specific: true,
2347 vendor_available: true,
2348 nocrt: true,
2349 }
2350 `)
2351}
2352
Jooyung Han38002912019-05-16 04:01:54 +09002353func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002354 bp := `
2355 cc_library {
2356 name: "libvndk",
2357 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002358 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002359 vndk: {
2360 enabled: true,
2361 },
2362 }
2363 cc_library {
2364 name: "libvndksp",
2365 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002366 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002367 vndk: {
2368 enabled: true,
2369 support_system_process: true,
2370 },
2371 }
2372 cc_library {
2373 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002374 vendor_available: true,
2375 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002376 vndk: {
2377 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002378 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002379 },
2380 }
2381 cc_library {
2382 name: "libvendor",
2383 vendor: true,
2384 }
2385 cc_library {
2386 name: "libvndkext",
2387 vendor: true,
2388 vndk: {
2389 enabled: true,
2390 extends: "libvndk",
2391 },
2392 }
2393 vndk_prebuilt_shared {
2394 name: "prevndk",
2395 version: "27",
2396 target_arch: "arm",
2397 binder32bit: true,
2398 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002399 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002400 vndk: {
2401 enabled: true,
2402 },
2403 arch: {
2404 arm: {
2405 srcs: ["liba.so"],
2406 },
2407 },
2408 }
2409 cc_library {
2410 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002411 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002412 }
2413 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002414 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002415 symbol_file: "",
2416 }
2417 cc_library {
2418 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002419 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002420 }
2421 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002422 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002423 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002424 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002425 }
2426
2427 llndk_libraries_txt {
2428 name: "llndk.libraries.txt",
2429 }
2430 vndkcore_libraries_txt {
2431 name: "vndkcore.libraries.txt",
2432 }
2433 vndksp_libraries_txt {
2434 name: "vndksp.libraries.txt",
2435 }
2436 vndkprivate_libraries_txt {
2437 name: "vndkprivate.libraries.txt",
2438 }
2439 vndkcorevariant_libraries_txt {
2440 name: "vndkcorevariant.libraries.txt",
2441 insert_vndk_version: false,
2442 }
2443 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002444
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002445 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002446 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002447 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002448 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002449 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002450
Colin Cross78212242021-01-06 14:51:30 -08002451 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2452 []string{"libvndk.so", "libvndkprivate.so"})
2453 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2454 []string{"libc++.so", "libvndksp.so"})
2455 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2456 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2457 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2458 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002459
Colin Crossfb0c16e2019-11-20 17:12:35 -08002460 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002461
Jooyung Han38002912019-05-16 04:01:54 +09002462 tests := []struct {
2463 variant string
2464 name string
2465 expected string
2466 }{
2467 {vendorVariant, "libvndk", "native:vndk"},
2468 {vendorVariant, "libvndksp", "native:vndk"},
2469 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2470 {vendorVariant, "libvendor", "native:vendor"},
2471 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002472 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002473 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002474 {coreVariant, "libvndk", "native:platform"},
2475 {coreVariant, "libvndkprivate", "native:platform"},
2476 {coreVariant, "libllndk", "native:platform"},
2477 }
2478 for _, test := range tests {
2479 t.Run(test.name, func(t *testing.T) {
2480 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2481 assertString(t, module.makeLinkType, test.expected)
2482 })
2483 }
2484}
2485
Jeff Gaston294356f2017-09-27 17:05:30 -07002486var staticLinkDepOrderTestCases = []struct {
2487 // This is a string representation of a map[moduleName][]moduleDependency .
2488 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002489 inStatic string
2490
2491 // This is a string representation of a map[moduleName][]moduleDependency .
2492 // It models the dependencies declared in an Android.bp file.
2493 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002494
2495 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2496 // The keys of allOrdered specify which modules we would like to check.
2497 // The values of allOrdered specify the expected result (of the transitive closure of all
2498 // dependencies) for each module to test
2499 allOrdered string
2500
2501 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2502 // The keys of outOrdered specify which modules we would like to check.
2503 // The values of outOrdered specify the expected result (of the ordered linker command line)
2504 // for each module to test.
2505 outOrdered string
2506}{
2507 // Simple tests
2508 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002509 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002510 outOrdered: "",
2511 },
2512 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002513 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002514 outOrdered: "a:",
2515 },
2516 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002517 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002518 outOrdered: "a:b; b:",
2519 },
2520 // Tests of reordering
2521 {
2522 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002523 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002524 outOrdered: "a:b,c,d; b:d; c:d; d:",
2525 },
2526 {
2527 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002528 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002529 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2530 },
2531 {
2532 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002533 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002534 outOrdered: "a:d,b,e,c; d:b; e:c",
2535 },
2536 {
2537 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002538 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002539 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2540 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2541 },
2542 {
2543 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002544 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 -07002545 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2546 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2547 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002548 // shared dependencies
2549 {
2550 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2551 // So, we don't actually have to check that a shared dependency of c will change the order
2552 // of a library that depends statically on b and on c. We only need to check that if c has
2553 // a shared dependency on b, that that shows up in allOrdered.
2554 inShared: "c:b",
2555 allOrdered: "c:b",
2556 outOrdered: "c:",
2557 },
2558 {
2559 // This test doesn't actually include any shared dependencies but it's a reminder of what
2560 // the second phase of the above test would look like
2561 inStatic: "a:b,c; c:b",
2562 allOrdered: "a:c,b; c:b",
2563 outOrdered: "a:c,b; c:b",
2564 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002565 // tiebreakers for when two modules specifying different orderings and there is no dependency
2566 // to dictate an order
2567 {
2568 // 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 -08002569 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002570 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2571 },
2572 {
2573 // 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 -08002574 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 -07002575 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2576 },
2577 // Tests involving duplicate dependencies
2578 {
2579 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002580 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002581 outOrdered: "a:c,b",
2582 },
2583 {
2584 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002585 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002586 outOrdered: "a:d,c,b",
2587 },
2588 // Tests to confirm the nonexistence of infinite loops.
2589 // These cases should never happen, so as long as the test terminates and the
2590 // result is deterministic then that should be fine.
2591 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002592 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002593 outOrdered: "a:a",
2594 },
2595 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002597 allOrdered: "a:b,c; b:c,a; c:a,b",
2598 outOrdered: "a:b; b:c; c:a",
2599 },
2600 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002601 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002602 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2603 outOrdered: "a:c,b; b:a,c; c:b,a",
2604 },
2605}
2606
2607// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2608func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2609 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2610 strippedText := strings.Replace(text, " ", "", -1)
2611 if len(strippedText) < 1 {
2612 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2613 }
2614 allDeps = make(map[android.Path][]android.Path, 0)
2615
2616 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2617 moduleTexts := strings.Split(strippedText, ";")
2618
2619 outputForModuleName := func(moduleName string) android.Path {
2620 return android.PathForTesting(moduleName)
2621 }
2622
2623 for _, moduleText := range moduleTexts {
2624 // convert from "a:b,c" to ["a", "b,c"]
2625 components := strings.Split(moduleText, ":")
2626 if len(components) != 2 {
2627 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2628 }
2629 moduleName := components[0]
2630 moduleOutput := outputForModuleName(moduleName)
2631 modulesInOrder = append(modulesInOrder, moduleOutput)
2632
2633 depString := components[1]
2634 // convert from "b,c" to ["b", "c"]
2635 depNames := strings.Split(depString, ",")
2636 if len(depString) < 1 {
2637 depNames = []string{}
2638 }
2639 var deps []android.Path
2640 for _, depName := range depNames {
2641 deps = append(deps, outputForModuleName(depName))
2642 }
2643 allDeps[moduleOutput] = deps
2644 }
2645 return modulesInOrder, allDeps
2646}
2647
Jeff Gaston294356f2017-09-27 17:05:30 -07002648func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2649 for _, moduleName := range moduleNames {
2650 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002651 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002652 paths = append(paths, output)
2653 }
2654 return paths
2655}
2656
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002657func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002658 ctx := testCc(t, `
2659 cc_library {
2660 name: "a",
2661 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002662 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002663 }
2664 cc_library {
2665 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002666 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002667 }
2668 cc_library {
2669 name: "c",
2670 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002671 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002672 }
2673 cc_library {
2674 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002675 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002676 }
2677
2678 `)
2679
Colin Cross7113d202019-11-20 16:39:12 -08002680 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002681 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002682 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2683 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002684 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002685
2686 if !reflect.DeepEqual(actual, expected) {
2687 t.Errorf("staticDeps orderings were not propagated correctly"+
2688 "\nactual: %v"+
2689 "\nexpected: %v",
2690 actual,
2691 expected,
2692 )
2693 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002694}
Jeff Gaston294356f2017-09-27 17:05:30 -07002695
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002696func TestStaticLibDepReorderingWithShared(t *testing.T) {
2697 ctx := testCc(t, `
2698 cc_library {
2699 name: "a",
2700 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002701 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002702 }
2703 cc_library {
2704 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002705 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002706 }
2707 cc_library {
2708 name: "c",
2709 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002710 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002711 }
2712
2713 `)
2714
Colin Cross7113d202019-11-20 16:39:12 -08002715 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002716 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002717 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2718 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002719 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002720
2721 if !reflect.DeepEqual(actual, expected) {
2722 t.Errorf("staticDeps orderings did not account for shared libs"+
2723 "\nactual: %v"+
2724 "\nexpected: %v",
2725 actual,
2726 expected,
2727 )
2728 }
2729}
2730
Jooyung Hanb04a4992020-03-13 18:57:35 +09002731func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002732 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002733 if !reflect.DeepEqual(actual, expected) {
2734 t.Errorf(message+
2735 "\nactual: %v"+
2736 "\nexpected: %v",
2737 actual,
2738 expected,
2739 )
2740 }
2741}
2742
Jooyung Han61b66e92020-03-21 14:21:46 +00002743func TestLlndkLibrary(t *testing.T) {
2744 ctx := testCc(t, `
2745 cc_library {
2746 name: "libllndk",
2747 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002748 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002749 }
2750 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002751 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002752 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002753
2754 cc_prebuilt_library_shared {
2755 name: "libllndkprebuilt",
2756 stubs: { versions: ["1", "2"] },
2757 llndk_stubs: "libllndkprebuilt.llndk",
2758 }
2759 llndk_library {
2760 name: "libllndkprebuilt.llndk",
2761 }
2762
2763 cc_library {
2764 name: "libllndk_with_external_headers",
2765 stubs: { versions: ["1", "2"] },
2766 llndk_stubs: "libllndk_with_external_headers.llndk",
2767 header_libs: ["libexternal_headers"],
2768 export_header_lib_headers: ["libexternal_headers"],
2769 }
2770 llndk_library {
2771 name: "libllndk_with_external_headers.llndk",
2772 }
2773 cc_library_headers {
2774 name: "libexternal_headers",
2775 export_include_dirs: ["include"],
2776 vendor_available: true,
2777 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002778 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002779 actual := ctx.ModuleVariantsForTests("libllndk")
2780 for i := 0; i < len(actual); i++ {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002781 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
Colin Cross127bb8b2020-12-16 16:46:01 -08002782 actual = append(actual[:i], actual[i+1:]...)
2783 i--
2784 }
2785 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002786 expected := []string{
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002787 "android_vendor.29_arm64_armv8-a_shared_1",
2788 "android_vendor.29_arm64_armv8-a_shared_2",
2789 "android_vendor.29_arm64_armv8-a_shared_current",
2790 "android_vendor.29_arm64_armv8-a_shared",
2791 "android_vendor.29_arm_armv7-a-neon_shared_1",
2792 "android_vendor.29_arm_armv7-a-neon_shared_2",
2793 "android_vendor.29_arm_armv7-a-neon_shared_current",
2794 "android_vendor.29_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002795 }
2796 checkEquals(t, "variants for llndk stubs", expected, actual)
2797
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002798 params := ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002799 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2800
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002801 params = ctx.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002802 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2803}
2804
Jiyong Parka46a4d52017-12-14 19:54:34 +09002805func TestLlndkHeaders(t *testing.T) {
2806 ctx := testCc(t, `
2807 llndk_headers {
2808 name: "libllndk_headers",
2809 export_include_dirs: ["my_include"],
2810 }
2811 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002812 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002813 export_llndk_headers: ["libllndk_headers"],
2814 }
2815 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002816 name: "libllndk",
2817 llndk_stubs: "libllndk.llndk",
2818 }
2819
2820 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002821 name: "libvendor",
2822 shared_libs: ["libllndk"],
2823 vendor: true,
2824 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002825 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002826 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002827 }
2828 `)
2829
2830 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002831 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002832 cflags := cc.Args["cFlags"]
2833 if !strings.Contains(cflags, "-Imy_include") {
2834 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2835 }
2836}
2837
Logan Chien43d34c32017-12-20 01:17:32 +08002838func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2839 actual := module.Properties.AndroidMkRuntimeLibs
2840 if !reflect.DeepEqual(actual, expected) {
2841 t.Errorf("incorrect runtime_libs for shared libs"+
2842 "\nactual: %v"+
2843 "\nexpected: %v",
2844 actual,
2845 expected,
2846 )
2847 }
2848}
2849
2850const runtimeLibAndroidBp = `
2851 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002852 name: "liball_available",
2853 vendor_available: true,
2854 product_available: true,
2855 no_libcrt : true,
2856 nocrt : true,
2857 system_shared_libs : [],
2858 }
2859 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002860 name: "libvendor_available1",
2861 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002862 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002863 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002864 nocrt : true,
2865 system_shared_libs : [],
2866 }
2867 cc_library {
2868 name: "libvendor_available2",
2869 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002870 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002871 target: {
2872 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002873 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002874 }
2875 },
Yi Konge7fe9912019-06-02 00:53:50 -07002876 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002877 nocrt : true,
2878 system_shared_libs : [],
2879 }
2880 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002881 name: "libproduct_vendor",
2882 product_specific: true,
2883 vendor_available: true,
2884 no_libcrt : true,
2885 nocrt : true,
2886 system_shared_libs : [],
2887 }
2888 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002889 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002890 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002891 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002892 nocrt : true,
2893 system_shared_libs : [],
2894 }
2895 cc_library {
2896 name: "libvendor1",
2897 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002898 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002899 nocrt : true,
2900 system_shared_libs : [],
2901 }
2902 cc_library {
2903 name: "libvendor2",
2904 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002905 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002906 no_libcrt : true,
2907 nocrt : true,
2908 system_shared_libs : [],
2909 }
2910 cc_library {
2911 name: "libproduct_available1",
2912 product_available: true,
2913 runtime_libs: ["liball_available"],
2914 no_libcrt : true,
2915 nocrt : true,
2916 system_shared_libs : [],
2917 }
2918 cc_library {
2919 name: "libproduct1",
2920 product_specific: true,
2921 no_libcrt : true,
2922 nocrt : true,
2923 system_shared_libs : [],
2924 }
2925 cc_library {
2926 name: "libproduct2",
2927 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002928 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002929 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002930 nocrt : true,
2931 system_shared_libs : [],
2932 }
2933`
2934
2935func TestRuntimeLibs(t *testing.T) {
2936 ctx := testCc(t, runtimeLibAndroidBp)
2937
2938 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002939 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002940
Justin Yun8a2600c2020-12-07 12:44:03 +09002941 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2942 checkRuntimeLibs(t, []string{"liball_available"}, module)
2943
2944 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2945 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002946
2947 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002948 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002949
2950 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2951 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002952 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002953
Justin Yun8a2600c2020-12-07 12:44:03 +09002954 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2955 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002956
2957 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002958 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002959
2960 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2961 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002962 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002963
2964 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2965 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2966
2967 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002968 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002969}
2970
2971func TestExcludeRuntimeLibs(t *testing.T) {
2972 ctx := testCc(t, runtimeLibAndroidBp)
2973
Colin Cross7113d202019-11-20 16:39:12 -08002974 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002975 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2976 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002977
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002978 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002979 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002980 checkRuntimeLibs(t, nil, module)
2981}
2982
2983func TestRuntimeLibsNoVndk(t *testing.T) {
2984 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2985
2986 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2987
Colin Cross7113d202019-11-20 16:39:12 -08002988 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002989
Justin Yun8a2600c2020-12-07 12:44:03 +09002990 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2991 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002992
2993 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002994 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002995
2996 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002997 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002998}
2999
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003000func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003001 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003002 actual := module.Properties.AndroidMkStaticLibs
3003 if !reflect.DeepEqual(actual, expected) {
3004 t.Errorf("incorrect static_libs"+
3005 "\nactual: %v"+
3006 "\nexpected: %v",
3007 actual,
3008 expected,
3009 )
3010 }
3011}
3012
3013const staticLibAndroidBp = `
3014 cc_library {
3015 name: "lib1",
3016 }
3017 cc_library {
3018 name: "lib2",
3019 static_libs: ["lib1"],
3020 }
3021`
3022
3023func TestStaticLibDepExport(t *testing.T) {
3024 ctx := testCc(t, staticLibAndroidBp)
3025
3026 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003027 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003028 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07003029 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003030
3031 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003032 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003033 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3034 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07003035 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003036}
3037
Jiyong Parkd08b6972017-09-26 10:50:54 +09003038var compilerFlagsTestCases = []struct {
3039 in string
3040 out bool
3041}{
3042 {
3043 in: "a",
3044 out: false,
3045 },
3046 {
3047 in: "-a",
3048 out: true,
3049 },
3050 {
3051 in: "-Ipath/to/something",
3052 out: false,
3053 },
3054 {
3055 in: "-isystempath/to/something",
3056 out: false,
3057 },
3058 {
3059 in: "--coverage",
3060 out: false,
3061 },
3062 {
3063 in: "-include a/b",
3064 out: true,
3065 },
3066 {
3067 in: "-include a/b c/d",
3068 out: false,
3069 },
3070 {
3071 in: "-DMACRO",
3072 out: true,
3073 },
3074 {
3075 in: "-DMAC RO",
3076 out: false,
3077 },
3078 {
3079 in: "-a -b",
3080 out: false,
3081 },
3082 {
3083 in: "-DMACRO=definition",
3084 out: true,
3085 },
3086 {
3087 in: "-DMACRO=defi nition",
3088 out: true, // TODO(jiyong): this should be false
3089 },
3090 {
3091 in: "-DMACRO(x)=x + 1",
3092 out: true,
3093 },
3094 {
3095 in: "-DMACRO=\"defi nition\"",
3096 out: true,
3097 },
3098}
3099
3100type mockContext struct {
3101 BaseModuleContext
3102 result bool
3103}
3104
3105func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3106 // CheckBadCompilerFlags calls this function when the flag should be rejected
3107 ctx.result = false
3108}
3109
3110func TestCompilerFlags(t *testing.T) {
3111 for _, testCase := range compilerFlagsTestCases {
3112 ctx := &mockContext{result: true}
3113 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3114 if ctx.result != testCase.out {
3115 t.Errorf("incorrect output:")
3116 t.Errorf(" input: %#v", testCase.in)
3117 t.Errorf(" expected: %#v", testCase.out)
3118 t.Errorf(" got: %#v", ctx.result)
3119 }
3120 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003121}
Jiyong Park374510b2018-03-19 18:23:01 +09003122
3123func TestVendorPublicLibraries(t *testing.T) {
3124 ctx := testCc(t, `
3125 cc_library_headers {
3126 name: "libvendorpublic_headers",
3127 export_include_dirs: ["my_include"],
3128 }
3129 vendor_public_library {
3130 name: "libvendorpublic",
3131 symbol_file: "",
3132 export_public_headers: ["libvendorpublic_headers"],
3133 }
3134 cc_library {
3135 name: "libvendorpublic",
3136 srcs: ["foo.c"],
3137 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003138 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003139 nocrt: true,
3140 }
3141
3142 cc_library {
3143 name: "libsystem",
3144 shared_libs: ["libvendorpublic"],
3145 vendor: false,
3146 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003147 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003148 nocrt: true,
3149 }
3150 cc_library {
3151 name: "libvendor",
3152 shared_libs: ["libvendorpublic"],
3153 vendor: true,
3154 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003155 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003156 nocrt: true,
3157 }
3158 `)
3159
Colin Cross7113d202019-11-20 16:39:12 -08003160 coreVariant := "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003161 vendorVariant := "android_vendor.29_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003162
3163 // test if header search paths are correctly added
3164 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003165 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003166 cflags := cc.Args["cFlags"]
3167 if !strings.Contains(cflags, "-Imy_include") {
3168 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3169 }
3170
3171 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003172 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003173 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003174 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003175 if !strings.Contains(libflags, stubPaths[0].String()) {
3176 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3177 }
3178
3179 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003180 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003181 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003182 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003183 if !strings.Contains(libflags, stubPaths[0].String()) {
3184 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3185 }
3186
3187}
Jiyong Park37b25202018-07-11 10:49:27 +09003188
3189func TestRecovery(t *testing.T) {
3190 ctx := testCc(t, `
3191 cc_library_shared {
3192 name: "librecovery",
3193 recovery: true,
3194 }
3195 cc_library_shared {
3196 name: "librecovery32",
3197 recovery: true,
3198 compile_multilib:"32",
3199 }
Jiyong Park5baac542018-08-28 09:55:37 +09003200 cc_library_shared {
3201 name: "libHalInRecovery",
3202 recovery_available: true,
3203 vendor: true,
3204 }
Jiyong Park37b25202018-07-11 10:49:27 +09003205 `)
3206
3207 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003208 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003209 if len(variants) != 1 || !android.InList(arm64, variants) {
3210 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3211 }
3212
3213 variants = ctx.ModuleVariantsForTests("librecovery32")
3214 if android.InList(arm64, variants) {
3215 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3216 }
Jiyong Park5baac542018-08-28 09:55:37 +09003217
3218 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3219 if !recoveryModule.Platform() {
3220 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3221 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003222}
Jiyong Park5baac542018-08-28 09:55:37 +09003223
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003224func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3225 bp := `
3226 cc_prebuilt_test_library_shared {
3227 name: "test_lib",
3228 relative_install_path: "foo/bar/baz",
3229 srcs: ["srcpath/dontusethispath/baz.so"],
3230 }
3231
3232 cc_test {
3233 name: "main_test",
3234 data_libs: ["test_lib"],
3235 gtest: false,
3236 }
3237 `
3238
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003239 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003240 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003241 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003242 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3243
3244 ctx := testCcWithConfig(t, config)
3245 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3246 testBinary := module.(*Module).linker.(*testBinary)
3247 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3248 if err != nil {
3249 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3250 }
3251 if len(outputFiles) != 1 {
3252 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3253 }
3254 if len(testBinary.dataPaths()) != 1 {
3255 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3256 }
3257
3258 outputPath := outputFiles[0].String()
3259
3260 if !strings.HasSuffix(outputPath, "/main_test") {
3261 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3262 }
Colin Crossaa255532020-07-03 13:18:24 -07003263 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003264 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3265 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3266 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3267 }
3268}
3269
Jiyong Park7ed9de32018-10-15 22:25:07 +09003270func TestVersionedStubs(t *testing.T) {
3271 ctx := testCc(t, `
3272 cc_library_shared {
3273 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003274 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003275 stubs: {
3276 symbol_file: "foo.map.txt",
3277 versions: ["1", "2", "3"],
3278 },
3279 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003280
Jiyong Park7ed9de32018-10-15 22:25:07 +09003281 cc_library_shared {
3282 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003283 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003284 shared_libs: ["libFoo#1"],
3285 }`)
3286
3287 variants := ctx.ModuleVariantsForTests("libFoo")
3288 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003289 "android_arm64_armv8-a_shared",
3290 "android_arm64_armv8-a_shared_1",
3291 "android_arm64_armv8-a_shared_2",
3292 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003293 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003294 "android_arm_armv7-a-neon_shared",
3295 "android_arm_armv7-a-neon_shared_1",
3296 "android_arm_armv7-a-neon_shared_2",
3297 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003298 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003299 }
3300 variantsMismatch := false
3301 if len(variants) != len(expectedVariants) {
3302 variantsMismatch = true
3303 } else {
3304 for _, v := range expectedVariants {
3305 if !inList(v, variants) {
3306 variantsMismatch = false
3307 }
3308 }
3309 }
3310 if variantsMismatch {
3311 t.Errorf("variants of libFoo expected:\n")
3312 for _, v := range expectedVariants {
3313 t.Errorf("%q\n", v)
3314 }
3315 t.Errorf(", but got:\n")
3316 for _, v := range variants {
3317 t.Errorf("%q\n", v)
3318 }
3319 }
3320
Colin Cross7113d202019-11-20 16:39:12 -08003321 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003322 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003323 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003324 if !strings.Contains(libFlags, libFoo1StubPath) {
3325 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3326 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003327
Colin Cross7113d202019-11-20 16:39:12 -08003328 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003329 cFlags := libBarCompileRule.Args["cFlags"]
3330 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3331 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3332 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3333 }
Jiyong Park37b25202018-07-11 10:49:27 +09003334}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003335
Jooyung Hanb04a4992020-03-13 18:57:35 +09003336func TestVersioningMacro(t *testing.T) {
3337 for _, tc := range []struct{ moduleName, expected string }{
3338 {"libc", "__LIBC_API__"},
3339 {"libfoo", "__LIBFOO_API__"},
3340 {"libfoo@1", "__LIBFOO_1_API__"},
3341 {"libfoo-v1", "__LIBFOO_V1_API__"},
3342 {"libfoo.v1", "__LIBFOO_V1_API__"},
3343 } {
3344 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3345 }
3346}
3347
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003348func TestStaticExecutable(t *testing.T) {
3349 ctx := testCc(t, `
3350 cc_binary {
3351 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003352 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003353 static_executable: true,
3354 }`)
3355
Colin Cross7113d202019-11-20 16:39:12 -08003356 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003357 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3358 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003359 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003360 for _, lib := range systemStaticLibs {
3361 if !strings.Contains(libFlags, lib) {
3362 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3363 }
3364 }
3365 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3366 for _, lib := range systemSharedLibs {
3367 if strings.Contains(libFlags, lib) {
3368 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3369 }
3370 }
3371}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003372
3373func TestStaticDepsOrderWithStubs(t *testing.T) {
3374 ctx := testCc(t, `
3375 cc_binary {
3376 name: "mybin",
3377 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003378 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003379 static_executable: true,
3380 stl: "none",
3381 }
3382
3383 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003384 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003385 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003386 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003387 stl: "none",
3388 }
3389
3390 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003391 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003392 srcs: ["foo.c"],
3393 stl: "none",
3394 stubs: {
3395 versions: ["1"],
3396 },
3397 }`)
3398
Colin Cross0de8a1e2020-09-18 14:15:30 -07003399 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3400 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003401 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003402
3403 if !reflect.DeepEqual(actual, expected) {
3404 t.Errorf("staticDeps orderings were not propagated correctly"+
3405 "\nactual: %v"+
3406 "\nexpected: %v",
3407 actual,
3408 expected,
3409 )
3410 }
3411}
Jooyung Han38002912019-05-16 04:01:54 +09003412
Jooyung Hand48f3c32019-08-23 11:18:57 +09003413func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3414 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3415 cc_library {
3416 name: "libA",
3417 srcs: ["foo.c"],
3418 shared_libs: ["libB"],
3419 stl: "none",
3420 }
3421
3422 cc_library {
3423 name: "libB",
3424 srcs: ["foo.c"],
3425 enabled: false,
3426 stl: "none",
3427 }
3428 `)
3429}
3430
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003431// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3432// correctly.
3433func TestFuzzTarget(t *testing.T) {
3434 ctx := testCc(t, `
3435 cc_fuzz {
3436 name: "fuzz_smoke_test",
3437 srcs: ["foo.c"],
3438 }`)
3439
Paul Duffin075c4172019-12-19 19:06:13 +00003440 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003441 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3442}
3443
Jiyong Park29074592019-07-07 16:27:47 +09003444func TestAidl(t *testing.T) {
3445}
3446
Jooyung Han38002912019-05-16 04:01:54 +09003447func assertString(t *testing.T, got, expected string) {
3448 t.Helper()
3449 if got != expected {
3450 t.Errorf("expected %q got %q", expected, got)
3451 }
3452}
3453
3454func assertArrayString(t *testing.T, got, expected []string) {
3455 t.Helper()
3456 if len(got) != len(expected) {
3457 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3458 return
3459 }
3460 for i := range got {
3461 if got[i] != expected[i] {
3462 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3463 i, expected[i], expected, got[i], got)
3464 return
3465 }
3466 }
3467}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003468
Jooyung Han0302a842019-10-30 18:43:49 +09003469func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3470 t.Helper()
3471 assertArrayString(t, android.SortedStringKeys(m), expected)
3472}
3473
Colin Crosse1bb5d02019-09-24 14:55:04 -07003474func TestDefaults(t *testing.T) {
3475 ctx := testCc(t, `
3476 cc_defaults {
3477 name: "defaults",
3478 srcs: ["foo.c"],
3479 static: {
3480 srcs: ["bar.c"],
3481 },
3482 shared: {
3483 srcs: ["baz.c"],
3484 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003485 bazel_module: {
3486 bp2build_available: true,
3487 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003488 }
3489
3490 cc_library_static {
3491 name: "libstatic",
3492 defaults: ["defaults"],
3493 }
3494
3495 cc_library_shared {
3496 name: "libshared",
3497 defaults: ["defaults"],
3498 }
3499
3500 cc_library {
3501 name: "libboth",
3502 defaults: ["defaults"],
3503 }
3504
3505 cc_binary {
3506 name: "binary",
3507 defaults: ["defaults"],
3508 }`)
3509
3510 pathsToBase := func(paths android.Paths) []string {
3511 var ret []string
3512 for _, p := range paths {
3513 ret = append(ret, p.Base())
3514 }
3515 return ret
3516 }
3517
Colin Cross7113d202019-11-20 16:39:12 -08003518 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003519 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3520 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3521 }
Colin Cross7113d202019-11-20 16:39:12 -08003522 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003523 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3524 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3525 }
Colin Cross7113d202019-11-20 16:39:12 -08003526 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003527 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3528 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3529 }
3530
Colin Cross7113d202019-11-20 16:39:12 -08003531 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003532 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3533 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3534 }
Colin Cross7113d202019-11-20 16:39:12 -08003535 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003536 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3537 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3538 }
3539}
Colin Crosseabaedd2020-02-06 17:01:55 -08003540
3541func TestProductVariableDefaults(t *testing.T) {
3542 bp := `
3543 cc_defaults {
3544 name: "libfoo_defaults",
3545 srcs: ["foo.c"],
3546 cppflags: ["-DFOO"],
3547 product_variables: {
3548 debuggable: {
3549 cppflags: ["-DBAR"],
3550 },
3551 },
3552 }
3553
3554 cc_library {
3555 name: "libfoo",
3556 defaults: ["libfoo_defaults"],
3557 }
3558 `
3559
Paul Duffin8567f222021-03-23 00:02:06 +00003560 result := android.GroupFixturePreparers(
3561 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003562 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003563
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003564 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3565 variables.Debuggable = BoolPtr(true)
3566 }),
3567 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003568
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003569 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003570 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003571}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003572
3573func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3574 t.Parallel()
3575 bp := `
3576 cc_library_static {
3577 name: "libfoo",
3578 srcs: ["foo.c"],
3579 whole_static_libs: ["libbar"],
3580 }
3581
3582 cc_library_static {
3583 name: "libbar",
3584 whole_static_libs: ["libmissing"],
3585 }
3586 `
3587
Paul Duffin8567f222021-03-23 00:02:06 +00003588 result := android.GroupFixturePreparers(
3589 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003590 android.PrepareForTestWithAllowMissingDependencies,
3591 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003592
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003593 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003594 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003595
Paul Duffine84b1332021-03-12 11:59:43 +00003596 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003597
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003598 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003599 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003600}
Colin Crosse9fe2942020-11-10 18:12:15 -08003601
3602func TestInstallSharedLibs(t *testing.T) {
3603 bp := `
3604 cc_binary {
3605 name: "bin",
3606 host_supported: true,
3607 shared_libs: ["libshared"],
3608 runtime_libs: ["libruntime"],
3609 srcs: [":gen"],
3610 }
3611
3612 cc_library_shared {
3613 name: "libshared",
3614 host_supported: true,
3615 shared_libs: ["libtransitive"],
3616 }
3617
3618 cc_library_shared {
3619 name: "libtransitive",
3620 host_supported: true,
3621 }
3622
3623 cc_library_shared {
3624 name: "libruntime",
3625 host_supported: true,
3626 }
3627
3628 cc_binary_host {
3629 name: "tool",
3630 srcs: ["foo.cpp"],
3631 }
3632
3633 genrule {
3634 name: "gen",
3635 tools: ["tool"],
3636 out: ["gen.cpp"],
3637 cmd: "$(location tool) $(out)",
3638 }
3639 `
3640
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003641 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003642 ctx := testCcWithConfig(t, config)
3643
3644 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3645 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3646 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3647 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3648 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3649
3650 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3651 t.Errorf("expected host bin dependency %q, got %q", w, g)
3652 }
3653
3654 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3655 t.Errorf("expected host bin dependency %q, got %q", w, g)
3656 }
3657
3658 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3659 t.Errorf("expected host bin dependency %q, got %q", w, g)
3660 }
3661
3662 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3663 t.Errorf("expected host bin dependency %q, got %q", w, g)
3664 }
3665
3666 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3667 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3668 }
3669
3670 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3671 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3672 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3673 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3674
3675 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3676 t.Errorf("expected device bin dependency %q, got %q", w, g)
3677 }
3678
3679 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3680 t.Errorf("expected device bin dependency %q, got %q", w, g)
3681 }
3682
3683 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3684 t.Errorf("expected device bin dependency %q, got %q", w, g)
3685 }
3686
3687 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3688 t.Errorf("expected device bin dependency %q, got %q", w, g)
3689 }
3690
3691 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3692 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3693 }
3694
3695}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003696
3697func TestStubsLibReexportsHeaders(t *testing.T) {
3698 ctx := testCc(t, `
3699 cc_library_shared {
3700 name: "libclient",
3701 srcs: ["foo.c"],
3702 shared_libs: ["libfoo#1"],
3703 }
3704
3705 cc_library_shared {
3706 name: "libfoo",
3707 srcs: ["foo.c"],
3708 shared_libs: ["libbar"],
3709 export_shared_lib_headers: ["libbar"],
3710 stubs: {
3711 symbol_file: "foo.map.txt",
3712 versions: ["1", "2", "3"],
3713 },
3714 }
3715
3716 cc_library_shared {
3717 name: "libbar",
3718 export_include_dirs: ["include/libbar"],
3719 srcs: ["foo.c"],
3720 }`)
3721
3722 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3723
3724 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3725 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3726 }
3727}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003728
3729func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3730 ctx := testCc(t, `
3731 cc_library {
3732 name: "libfoo",
3733 srcs: ["a/Foo.aidl"],
3734 aidl: { flags: ["-Werror"], },
3735 }
3736 `)
3737
3738 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3739 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3740 aidlCommand := manifest.Commands[0].GetCommand()
3741 expectedAidlFlag := "-Werror"
3742 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3743 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3744 }
3745}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003746
Jiyong Parka008fb02021-03-16 17:15:53 +09003747func TestMinSdkVersionInClangTriple(t *testing.T) {
3748 ctx := testCc(t, `
3749 cc_library_shared {
3750 name: "libfoo",
3751 srcs: ["foo.c"],
3752 min_sdk_version: "29",
3753 }`)
3754
3755 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3756 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3757}
3758
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003759func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3760 ctx := testCc(t, `
3761 cc_object {
3762 name: "crt_foo",
3763 srcs: ["foo.c"],
3764 crt: true,
3765 stl: "none",
3766 min_sdk_version: "28",
3767
3768 }`)
3769
3770 arch := "android_arm64_armv8-a"
3771 for _, v := range []string{"", "28", "29", "30", "current"} {
3772 var variant string
3773 if v == "" {
3774 variant = arch
3775 } else {
3776 variant = arch + "_sdk_" + v
3777 }
3778 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3779 vNum := v
3780 if v == "current" || v == "" {
3781 vNum = "10000"
3782 }
3783 expected := "-target aarch64-linux-android" + vNum + " "
3784 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3785 }
3786}
3787
3788func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3789 ctx := testCc(t, `
3790 cc_binary {
3791 name: "bin",
3792 srcs: ["foo.c"],
3793 stl: "none",
3794 min_sdk_version: "29",
3795 sdk_version: "current",
3796 }
3797 `)
3798
3799 // Sdk variant uses the crt object of the matching min_sdk_version
3800 variant := "android_arm64_armv8-a_sdk"
3801 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3802 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3803 variant+"_29/crtbegin_dynamic.o")
3804
3805 // platform variant uses the crt object built for platform
3806 variant = "android_arm64_armv8-a"
3807 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3808 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3809 variant+"/crtbegin_dynamic.o")
3810}
3811
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003812type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003813
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003814const (
3815 None MemtagNoteType = iota + 1
3816 Sync
3817 Async
3818)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003819
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003820func (t MemtagNoteType) str() string {
3821 switch t {
3822 case None:
3823 return "none"
3824 case Sync:
3825 return "sync"
3826 case Async:
3827 return "async"
3828 default:
3829 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003830 }
3831}
3832
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003833func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3834 note_async := "note_memtag_heap_async"
3835 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003836
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003837 found := None
3838 implicits := m.Rule("ld").Implicits
3839 for _, lib := range implicits {
3840 if strings.Contains(lib.Rel(), note_async) {
3841 found = Async
3842 break
3843 } else if strings.Contains(lib.Rel(), note_sync) {
3844 found = Sync
3845 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003846 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003847 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003848
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003849 if found != expected {
3850 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3851 }
3852}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003853
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003854var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3855 android.FixtureModifyMockFS(func(fs android.MockFS) {
3856 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003857 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003858 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003859 gtest: false,
3860 }
3861
3862 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003863 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003864 gtest: false,
3865 sanitize: { memtag_heap: false },
3866 }
3867
3868 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003869 name: "%[1]s_test_true",
3870 gtest: false,
3871 sanitize: { memtag_heap: true },
3872 }
3873
3874 cc_test {
3875 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003876 gtest: false,
3877 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3878 }
3879
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003880 cc_test {
3881 name: "%[1]s_test_true_diag",
3882 gtest: false,
3883 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3884 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003885
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003886 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003887 name: "%[1]s_binary",
3888 }
3889
3890 cc_binary {
3891 name: "%[1]s_binary_false",
3892 sanitize: { memtag_heap: false },
3893 }
3894
3895 cc_binary {
3896 name: "%[1]s_binary_true",
3897 sanitize: { memtag_heap: true },
3898 }
3899
3900 cc_binary {
3901 name: "%[1]s_binary_true_nodiag",
3902 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3903 }
3904
3905 cc_binary {
3906 name: "%[1]s_binary_true_diag",
3907 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003908 }
3909 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003910 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3911 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3912 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3913 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003914
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003915 fs.Merge(android.MockFS{
3916 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3917 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3918 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3919 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3920 })
3921 }),
3922 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3923 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3924 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3925 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3926 }),
3927)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003928
3929func TestSanitizeMemtagHeap(t *testing.T) {
3930 variant := "android_arm64_armv8-a"
3931
Paul Duffin8567f222021-03-23 00:02:06 +00003932 result := android.GroupFixturePreparers(
3933 prepareForCcTest,
3934 prepareForTestWithMemtagHeap,
3935 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003936 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003937
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003938 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3940 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3941 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3942 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3943
3944 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3947 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3949
3950 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3955
3956 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3961
3962 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3985}
3986
3987func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003988 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003989
Paul Duffin8567f222021-03-23 00:02:06 +00003990 result := android.GroupFixturePreparers(
3991 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003992 prepareForTestWithMemtagHeap,
3993 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3994 variables.SanitizeDevice = []string{"memtag_heap"}
3995 }),
3996 ).RunTest(t)
3997 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003998
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003999 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4001 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
4002 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4003 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08004004
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004005 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
4006 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4007 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
4008 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4009 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4010
4011 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4012 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4013 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
4014 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4015 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4016
4017 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4018 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4019 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
4020 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4021 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4022
4023 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4024 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4025 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
4026 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4027 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4028
4029 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
4030 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4031 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
4032 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4033 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4034
4035 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4036 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4037 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4038 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4039 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4040
4041 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4042 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4043 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4044 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4045 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
4046}
4047
4048func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
4049 variant := "android_arm64_armv8-a"
4050
Paul Duffin8567f222021-03-23 00:02:06 +00004051 result := android.GroupFixturePreparers(
4052 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00004053 prepareForTestWithMemtagHeap,
4054 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4055 variables.SanitizeDevice = []string{"memtag_heap"}
4056 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
4057 }),
4058 ).RunTest(t)
4059 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08004060
4061 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
4062 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
4063 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
4064 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
4065 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
4066
4067 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
4068 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
4069 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
4070 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
4071 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
4072
4073 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
4074 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
4075 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
4076 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
4077 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
4078
4079 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
4080 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
4081 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
4082 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
4083 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
4084
4085 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
4086 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
4087 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
4088 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
4089 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
4090
4091 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
4092 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
4093 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
4094 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
4095 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
4096
4097 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
4098 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
4099 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
4100 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
4101 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
4102
4103 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
4104 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
4105 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
4106 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4107 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004108}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004109
4110func TestIncludeDirsExporting(t *testing.T) {
4111
4112 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4113 // embedded newline characters alone.
4114 trimIndentingSpaces := func(s string) string {
4115 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4116 }
4117
4118 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4119 t.Helper()
4120 expected = trimIndentingSpaces(expected)
4121 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4122 if expected != actual {
4123 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4124 }
4125 }
4126
4127 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4128
4129 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4130 t.Helper()
4131 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4132 name := module.Name()
4133
4134 for _, checker := range checkers {
4135 checker(t, name, exported)
4136 }
4137 }
4138
4139 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4140 return func(t *testing.T, name string, exported FlagExporterInfo) {
4141 t.Helper()
4142 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4143 }
4144 }
4145
4146 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4147 return func(t *testing.T, name string, exported FlagExporterInfo) {
4148 t.Helper()
4149 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4150 }
4151 }
4152
4153 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4154 return func(t *testing.T, name string, exported FlagExporterInfo) {
4155 t.Helper()
4156 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4157 }
4158 }
4159
4160 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4161 return func(t *testing.T, name string, exported FlagExporterInfo) {
4162 t.Helper()
4163 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4164 }
4165 }
4166
4167 genRuleModules := `
4168 genrule {
4169 name: "genrule_foo",
4170 cmd: "generate-foo",
4171 out: [
4172 "generated_headers/foo/generated_header.h",
4173 ],
4174 export_include_dirs: [
4175 "generated_headers",
4176 ],
4177 }
4178
4179 genrule {
4180 name: "genrule_bar",
4181 cmd: "generate-bar",
4182 out: [
4183 "generated_headers/bar/generated_header.h",
4184 ],
4185 export_include_dirs: [
4186 "generated_headers",
4187 ],
4188 }
4189 `
4190
4191 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4192 ctx := testCc(t, genRuleModules+`
4193 cc_library {
4194 name: "libfoo",
4195 srcs: ["foo.c"],
4196 export_include_dirs: ["foo/standard"],
4197 export_system_include_dirs: ["foo/system"],
4198 generated_headers: ["genrule_foo"],
4199 export_generated_headers: ["genrule_foo"],
4200 }
4201
4202 cc_library {
4203 name: "libbar",
4204 srcs: ["bar.c"],
4205 shared_libs: ["libfoo"],
4206 export_include_dirs: ["bar/standard"],
4207 export_system_include_dirs: ["bar/system"],
4208 generated_headers: ["genrule_bar"],
4209 export_generated_headers: ["genrule_bar"],
4210 }
4211 `)
4212 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4213 checkIncludeDirs(t, ctx, foo,
4214 expectedIncludeDirs(`
4215 foo/standard
4216 .intermediates/genrule_foo/gen/generated_headers
4217 `),
4218 expectedSystemIncludeDirs(`foo/system`),
4219 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4220 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4221 )
4222
4223 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4224 checkIncludeDirs(t, ctx, bar,
4225 expectedIncludeDirs(`
4226 bar/standard
4227 .intermediates/genrule_bar/gen/generated_headers
4228 `),
4229 expectedSystemIncludeDirs(`bar/system`),
4230 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4231 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4232 )
4233 })
4234
4235 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4236 ctx := testCc(t, genRuleModules+`
4237 cc_library {
4238 name: "libfoo",
4239 srcs: ["foo.c"],
4240 export_include_dirs: ["foo/standard"],
4241 export_system_include_dirs: ["foo/system"],
4242 generated_headers: ["genrule_foo"],
4243 export_generated_headers: ["genrule_foo"],
4244 }
4245
4246 cc_library {
4247 name: "libbar",
4248 srcs: ["bar.c"],
4249 whole_static_libs: ["libfoo"],
4250 export_include_dirs: ["bar/standard"],
4251 export_system_include_dirs: ["bar/system"],
4252 generated_headers: ["genrule_bar"],
4253 export_generated_headers: ["genrule_bar"],
4254 }
4255 `)
4256 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4257 checkIncludeDirs(t, ctx, foo,
4258 expectedIncludeDirs(`
4259 foo/standard
4260 .intermediates/genrule_foo/gen/generated_headers
4261 `),
4262 expectedSystemIncludeDirs(`foo/system`),
4263 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4264 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4265 )
4266
4267 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4268 checkIncludeDirs(t, ctx, bar,
4269 expectedIncludeDirs(`
4270 bar/standard
4271 foo/standard
4272 .intermediates/genrule_foo/gen/generated_headers
4273 .intermediates/genrule_bar/gen/generated_headers
4274 `),
4275 expectedSystemIncludeDirs(`
4276 bar/system
4277 foo/system
4278 `),
4279 expectedGeneratedHeaders(`
4280 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4281 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4282 `),
4283 expectedOrderOnlyDeps(`
4284 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4285 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4286 `),
4287 )
4288 })
4289
Paul Duffin3cb603e2021-02-19 13:57:10 +00004290 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4291 ctx := testCc(t, genRuleModules+`
4292 cc_library_shared {
4293 name: "libfoo",
4294 srcs: [
4295 "foo.c",
4296 "b.aidl",
4297 "a.proto",
4298 ],
4299 aidl: {
4300 export_aidl_headers: true,
4301 }
4302 }
4303 `)
4304 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4305 checkIncludeDirs(t, ctx, foo,
4306 expectedIncludeDirs(`
4307 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4308 `),
4309 expectedSystemIncludeDirs(``),
4310 expectedGeneratedHeaders(`
4311 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4312 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4313 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004314 `),
4315 expectedOrderOnlyDeps(`
4316 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4317 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4318 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004319 `),
4320 )
4321 })
4322
Paul Duffin3cb603e2021-02-19 13:57:10 +00004323 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4324 ctx := testCc(t, genRuleModules+`
4325 cc_library_shared {
4326 name: "libfoo",
4327 srcs: [
4328 "foo.c",
4329 "b.aidl",
4330 "a.proto",
4331 ],
4332 proto: {
4333 export_proto_headers: true,
4334 }
4335 }
4336 `)
4337 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4338 checkIncludeDirs(t, ctx, foo,
4339 expectedIncludeDirs(`
4340 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4341 `),
4342 expectedSystemIncludeDirs(``),
4343 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004344 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4345 `),
4346 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004347 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4348 `),
4349 )
4350 })
4351
Paul Duffin33056e82021-02-19 13:49:08 +00004352 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004353 ctx := testCc(t, genRuleModules+`
4354 cc_library_shared {
4355 name: "libfoo",
4356 srcs: [
4357 "foo.c",
4358 "a.sysprop",
4359 "b.aidl",
4360 "a.proto",
4361 ],
4362 }
4363 `)
4364 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4365 checkIncludeDirs(t, ctx, foo,
4366 expectedIncludeDirs(`
4367 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4368 `),
4369 expectedSystemIncludeDirs(``),
4370 expectedGeneratedHeaders(`
4371 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004372 `),
4373 expectedOrderOnlyDeps(`
4374 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4375 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004376 `),
4377 )
4378 })
4379}