blob: 675c0203b12860b3dfc32b67a99ba8d73677a822 [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")
38 variables.Platform_vndk_version = StringPtr("VER")
39 }),
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)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070078 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
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")
92 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
93
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")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 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")
134 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
135 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"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800141 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900142 productVariant = "android_product.VER_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
Logan Chienf3511742017-10-31 18:04:35 +0800247func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900248 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800249
Logan Chiend3c59a22018-03-29 14:08:15 +0800250 t.Helper()
251
Justin Yun0ecf0b22020-02-28 15:07:59 +0900252 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800253
254 // Check library properties.
255 lib, ok := mod.compiler.(*libraryDecorator)
256 if !ok {
257 t.Errorf("%q must have libraryDecorator", name)
258 } else if lib.baseInstaller.subDir != subDir {
259 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
260 lib.baseInstaller.subDir)
261 }
262
263 // Check VNDK properties.
264 if mod.vndkdep == nil {
265 t.Fatalf("%q must have `vndkdep`", name)
266 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700267 if !mod.IsVndk() {
268 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800269 }
270 if mod.isVndkSp() != isVndkSp {
271 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
272 }
273
274 // Check VNDK extension properties.
275 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500276 if mod.IsVndkExt() != isVndkExt {
277 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800278 }
279
280 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
281 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
282 }
283}
284
Jose Galmes0a942a02021-02-03 14:23:15 -0800285func 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 -0700286 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000287 mod := ctx.ModuleForTests(moduleName, variant)
288 outputFiles := mod.OutputFiles(t, "")
289 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900290 t.Errorf("%q must have single output\n", moduleName)
291 return
292 }
293 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900294
Bill Peckham945441c2020-08-31 16:07:58 -0700295 if include {
296 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800297 if fake {
298 if out.Rule == nil {
299 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
300 }
301 } else {
302 if out.Input.String() != outputFiles[0].String() {
303 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
304 }
Bill Peckham945441c2020-08-31 16:07:58 -0700305 }
306 } else {
307 out := singleton.MaybeOutput(snapshotPath)
308 if out.Rule != nil {
309 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
310 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900311 }
312}
313
Bill Peckham945441c2020-08-31 16:07:58 -0700314func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000315 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800316 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700317}
318
319func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000320 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800321 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
322}
323
324func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000325 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800326 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700327}
328
Jooyung Han2216fb12019-11-06 16:46:15 +0900329func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
330 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800331 content := android.ContentFromFileRuleForTests(t, params)
332 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900333 assertArrayString(t, actual, expected)
334}
335
Jooyung Han097087b2019-10-22 19:32:18 +0900336func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
337 t.Helper()
338 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900339 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
340}
341
342func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
343 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800344 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
345 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900346}
347
Logan Chienf3511742017-10-31 18:04:35 +0800348func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800349 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800350 cc_library {
351 name: "libvndk",
352 vendor_available: true,
353 vndk: {
354 enabled: true,
355 },
356 nocrt: true,
357 }
358
359 cc_library {
360 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900361 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800362 vndk: {
363 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900364 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800365 },
366 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900367 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800368 }
369
370 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900371 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800372 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900373 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800374 vndk: {
375 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900376 },
377 nocrt: true,
378 target: {
379 vendor: {
380 cflags: ["-DTEST"],
381 },
382 product: {
383 cflags: ["-DTEST"],
384 },
385 },
386 }
387
388 cc_library {
389 name: "libvndk_sp",
390 vendor_available: true,
391 vndk: {
392 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800393 support_system_process: true,
394 },
395 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900396 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800397 }
398
399 cc_library {
400 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900401 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800402 vndk: {
403 enabled: true,
404 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900405 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800406 },
407 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900408 target: {
409 vendor: {
410 suffix: "-x",
411 },
412 },
Logan Chienf3511742017-10-31 18:04:35 +0800413 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900414
415 cc_library {
416 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900417 vendor_available: true,
418 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900419 vndk: {
420 enabled: true,
421 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900422 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900423 },
424 nocrt: true,
425 target: {
426 vendor: {
427 suffix: "-x",
428 },
429 product: {
430 suffix: "-x",
431 },
432 },
433 }
434
Colin Crosse4e44bc2020-12-28 13:50:21 -0800435 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900436 name: "llndk.libraries.txt",
437 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800438 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900439 name: "vndkcore.libraries.txt",
440 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800441 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900442 name: "vndksp.libraries.txt",
443 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800444 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900445 name: "vndkprivate.libraries.txt",
446 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800447 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900448 name: "vndkproduct.libraries.txt",
449 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800450 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900451 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800452 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800454 `
455
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000456 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800457 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900458 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800459 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
460
461 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800462
Jooyung Han261e1582020-10-20 18:54:21 +0900463 // subdir == "" because VNDK libs are not supposed to be installed separately.
464 // They are installed as part of VNDK APEX instead.
465 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
466 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900467 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900468 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
469 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900470 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900471
Justin Yun6977e8a2020-10-29 18:24:11 +0900472 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
473 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900474
Inseob Kim1f086e22019-05-09 13:29:15 +0900475 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900476 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000477 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900478
479 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
480 "arm64", "armv8-a"))
481 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
482 "arm", "armv7-a-neon"))
483
484 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
485 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
486 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
487 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
488
Colin Crossfb0c16e2019-11-20 17:12:35 -0800489 variant := "android_vendor.VER_arm64_armv8-a_shared"
490 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900491
Inseob Kim7f283f42020-06-01 21:53:49 +0900492 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
493
494 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
495 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900496 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
497 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900498 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
499 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900500
Jooyung Han39edb6c2019-11-06 16:53:07 +0900501 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900502 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
503 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
504 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
505 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900506 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900507
Jooyung Han097087b2019-10-22 19:32:18 +0900508 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
509 "LLNDK: libc.so",
510 "LLNDK: libdl.so",
511 "LLNDK: libft2.so",
512 "LLNDK: libm.so",
513 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900514 "VNDK-SP: libvndk_sp-x.so",
515 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900516 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900517 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900518 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900519 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900520 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900521 "VNDK-private: libvndk-private.so",
522 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900523 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900524 "VNDK-product: libc++.so",
525 "VNDK-product: libvndk_product.so",
526 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900527 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900528 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900529 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
530 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
531 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 +0900532 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 +0900533 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
534}
535
Yo Chiangbba545e2020-06-09 16:15:37 +0800536func TestVndkWithHostSupported(t *testing.T) {
537 ctx := testCc(t, `
538 cc_library {
539 name: "libvndk_host_supported",
540 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900541 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800542 vndk: {
543 enabled: true,
544 },
545 host_supported: true,
546 }
547
548 cc_library {
549 name: "libvndk_host_supported_but_disabled_on_device",
550 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900551 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800552 vndk: {
553 enabled: true,
554 },
555 host_supported: true,
556 enabled: false,
557 target: {
558 host: {
559 enabled: true,
560 }
561 }
562 }
563
Colin Crosse4e44bc2020-12-28 13:50:21 -0800564 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800565 name: "vndkcore.libraries.txt",
566 }
567 `)
568
569 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
570}
571
Jooyung Han2216fb12019-11-06 16:46:15 +0900572func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800573 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800574 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900575 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800576 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800577 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000578 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800579 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
580 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
581 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900582
583 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700584 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900585 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900586}
587
588func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800589 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900590 cc_library {
591 name: "libvndk",
592 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900593 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900594 vndk: {
595 enabled: true,
596 },
597 nocrt: true,
598 }
599
600 cc_library {
601 name: "libvndk_sp",
602 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900603 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900604 vndk: {
605 enabled: true,
606 support_system_process: true,
607 },
608 nocrt: true,
609 }
610
611 cc_library {
612 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900613 vendor_available: true,
614 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900615 vndk: {
616 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900617 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900618 },
619 nocrt: true,
620 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900621
Colin Crosse4e44bc2020-12-28 13:50:21 -0800622 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900623 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800624 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900625 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800626 `
627
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000628 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800629 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
630 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
631 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
632
633 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
634
635 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900636
Jooyung Han2216fb12019-11-06 16:46:15 +0900637 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900638}
639
Chris Parsons79d66a52020-06-05 17:26:16 -0400640func TestDataLibs(t *testing.T) {
641 bp := `
642 cc_test_library {
643 name: "test_lib",
644 srcs: ["test_lib.cpp"],
645 gtest: false,
646 }
647
648 cc_test {
649 name: "main_test",
650 data_libs: ["test_lib"],
651 gtest: false,
652 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400653 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400654
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000655 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400656 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
657 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
658 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
659
660 ctx := testCcWithConfig(t, config)
661 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
662 testBinary := module.(*Module).linker.(*testBinary)
663 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
664 if err != nil {
665 t.Errorf("Expected cc_test to produce output files, error: %s", err)
666 return
667 }
668 if len(outputFiles) != 1 {
669 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
670 return
671 }
672 if len(testBinary.dataPaths()) != 1 {
673 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
674 return
675 }
676
677 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400678 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400679
680 if !strings.HasSuffix(outputPath, "/main_test") {
681 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
682 return
683 }
684 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
685 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
686 return
687 }
688}
689
Chris Parsons216e10a2020-07-09 17:12:52 -0400690func TestDataLibsRelativeInstallPath(t *testing.T) {
691 bp := `
692 cc_test_library {
693 name: "test_lib",
694 srcs: ["test_lib.cpp"],
695 relative_install_path: "foo/bar/baz",
696 gtest: false,
697 }
698
699 cc_test {
700 name: "main_test",
701 data_libs: ["test_lib"],
702 gtest: false,
703 }
704 `
705
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000706 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400707 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
708 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
709 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
710
711 ctx := testCcWithConfig(t, config)
712 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
713 testBinary := module.(*Module).linker.(*testBinary)
714 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
715 if err != nil {
716 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
717 }
718 if len(outputFiles) != 1 {
719 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
720 }
721 if len(testBinary.dataPaths()) != 1 {
722 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
723 }
724
725 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400726
727 if !strings.HasSuffix(outputPath, "/main_test") {
728 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
729 }
Colin Crossaa255532020-07-03 13:18:24 -0700730 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400731 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
732 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400733 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400734 }
735}
736
Jooyung Han0302a842019-10-30 18:43:49 +0900737func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900738 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900739 cc_library {
740 name: "libvndk",
741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900742 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900743 vndk: {
744 enabled: true,
745 },
746 nocrt: true,
747 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900748 cc_library {
749 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900750 vendor_available: true,
751 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900752 vndk: {
753 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900754 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900755 },
756 nocrt: true,
757 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800758
759 cc_library {
760 name: "libllndk",
761 llndk_stubs: "libllndk.llndk",
762 }
763
764 llndk_library {
765 name: "libllndk.llndk",
766 symbol_file: "",
767 export_llndk_headers: ["libllndk_headers"],
768 }
769
770 llndk_headers {
771 name: "libllndk_headers",
772 export_include_dirs: ["include"],
773 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900774 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900775
776 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
777 "LLNDK: libc.so",
778 "LLNDK: libdl.so",
779 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800780 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900781 "LLNDK: libm.so",
782 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900783 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900784 "VNDK-core: libvndk.so",
785 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900786 "VNDK-private: libvndk-private.so",
787 "VNDK-product: libc++.so",
788 "VNDK-product: libvndk-private.so",
789 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900790 })
Logan Chienf3511742017-10-31 18:04:35 +0800791}
792
Justin Yun63e9ec72020-10-29 16:49:43 +0900793func TestVndkModuleError(t *testing.T) {
794 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900795 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900796 cc_library {
797 name: "libvndk",
798 vndk: {
799 enabled: true,
800 },
801 nocrt: true,
802 }
803 `)
804
Justin Yunc0d8c492021-01-07 17:45:31 +0900805 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900806 cc_library {
807 name: "libvndk",
808 product_available: true,
809 vndk: {
810 enabled: true,
811 },
812 nocrt: true,
813 }
814 `)
815
Justin Yun6977e8a2020-10-29 18:24:11 +0900816 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
817 cc_library {
818 name: "libvndkprop",
819 vendor_available: true,
820 product_available: true,
821 vndk: {
822 enabled: true,
823 },
824 nocrt: true,
825 target: {
826 vendor: {
827 cflags: ["-DTEST",],
828 },
829 },
830 }
831 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900832}
833
Logan Chiend3c59a22018-03-29 14:08:15 +0800834func TestVndkDepError(t *testing.T) {
835 // Check whether an error is emitted when a VNDK lib depends on a system lib.
836 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
837 cc_library {
838 name: "libvndk",
839 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900840 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800841 vndk: {
842 enabled: true,
843 },
844 shared_libs: ["libfwk"], // Cause error
845 nocrt: true,
846 }
847
848 cc_library {
849 name: "libfwk",
850 nocrt: true,
851 }
852 `)
853
854 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
855 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
856 cc_library {
857 name: "libvndk",
858 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900859 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800860 vndk: {
861 enabled: true,
862 },
863 shared_libs: ["libvendor"], // Cause error
864 nocrt: true,
865 }
866
867 cc_library {
868 name: "libvendor",
869 vendor: true,
870 nocrt: true,
871 }
872 `)
873
874 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
875 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
876 cc_library {
877 name: "libvndk_sp",
878 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900879 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800880 vndk: {
881 enabled: true,
882 support_system_process: true,
883 },
884 shared_libs: ["libfwk"], // Cause error
885 nocrt: true,
886 }
887
888 cc_library {
889 name: "libfwk",
890 nocrt: true,
891 }
892 `)
893
894 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
895 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
896 cc_library {
897 name: "libvndk_sp",
898 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900899 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800900 vndk: {
901 enabled: true,
902 support_system_process: true,
903 },
904 shared_libs: ["libvendor"], // Cause error
905 nocrt: true,
906 }
907
908 cc_library {
909 name: "libvendor",
910 vendor: true,
911 nocrt: true,
912 }
913 `)
914
915 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
916 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
917 cc_library {
918 name: "libvndk_sp",
919 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900920 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800921 vndk: {
922 enabled: true,
923 support_system_process: true,
924 },
925 shared_libs: ["libvndk"], // Cause error
926 nocrt: true,
927 }
928
929 cc_library {
930 name: "libvndk",
931 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900932 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800933 vndk: {
934 enabled: true,
935 },
936 nocrt: true,
937 }
938 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900939
940 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
941 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
942 cc_library {
943 name: "libvndk",
944 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900945 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900946 vndk: {
947 enabled: true,
948 },
949 shared_libs: ["libnonvndk"],
950 nocrt: true,
951 }
952
953 cc_library {
954 name: "libnonvndk",
955 vendor_available: true,
956 nocrt: true,
957 }
958 `)
959
960 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
961 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
962 cc_library {
963 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900964 vendor_available: true,
965 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900966 vndk: {
967 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900968 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900969 },
970 shared_libs: ["libnonvndk"],
971 nocrt: true,
972 }
973
974 cc_library {
975 name: "libnonvndk",
976 vendor_available: true,
977 nocrt: true,
978 }
979 `)
980
981 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
982 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
983 cc_library {
984 name: "libvndksp",
985 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900986 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900987 vndk: {
988 enabled: true,
989 support_system_process: true,
990 },
991 shared_libs: ["libnonvndk"],
992 nocrt: true,
993 }
994
995 cc_library {
996 name: "libnonvndk",
997 vendor_available: true,
998 nocrt: true,
999 }
1000 `)
1001
1002 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1003 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1004 cc_library {
1005 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001006 vendor_available: true,
1007 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001008 vndk: {
1009 enabled: true,
1010 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001011 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001012 },
1013 shared_libs: ["libnonvndk"],
1014 nocrt: true,
1015 }
1016
1017 cc_library {
1018 name: "libnonvndk",
1019 vendor_available: true,
1020 nocrt: true,
1021 }
1022 `)
1023}
1024
1025func TestDoubleLoadbleDep(t *testing.T) {
1026 // okay to link : LLNDK -> double_loadable VNDK
1027 testCc(t, `
1028 cc_library {
1029 name: "libllndk",
1030 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001031 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001032 }
1033
1034 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001035 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001036 symbol_file: "",
1037 }
1038
1039 cc_library {
1040 name: "libdoubleloadable",
1041 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001042 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001043 vndk: {
1044 enabled: true,
1045 },
1046 double_loadable: true,
1047 }
1048 `)
1049 // okay to link : LLNDK -> VNDK-SP
1050 testCc(t, `
1051 cc_library {
1052 name: "libllndk",
1053 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001054 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001055 }
1056
1057 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001058 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001059 symbol_file: "",
1060 }
1061
1062 cc_library {
1063 name: "libvndksp",
1064 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001065 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001066 vndk: {
1067 enabled: true,
1068 support_system_process: true,
1069 },
1070 }
1071 `)
1072 // okay to link : double_loadable -> double_loadable
1073 testCc(t, `
1074 cc_library {
1075 name: "libdoubleloadable1",
1076 shared_libs: ["libdoubleloadable2"],
1077 vendor_available: true,
1078 double_loadable: true,
1079 }
1080
1081 cc_library {
1082 name: "libdoubleloadable2",
1083 vendor_available: true,
1084 double_loadable: true,
1085 }
1086 `)
1087 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1088 testCc(t, `
1089 cc_library {
1090 name: "libdoubleloadable",
1091 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001092 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001093 vndk: {
1094 enabled: true,
1095 },
1096 double_loadable: true,
1097 shared_libs: ["libnondoubleloadable"],
1098 }
1099
1100 cc_library {
1101 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001102 vendor_available: true,
1103 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001104 vndk: {
1105 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001106 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001107 },
1108 double_loadable: true,
1109 }
1110 `)
1111 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1112 testCc(t, `
1113 cc_library {
1114 name: "libllndk",
1115 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001116 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001117 }
1118
1119 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001120 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001121 symbol_file: "",
1122 }
1123
1124 cc_library {
1125 name: "libcoreonly",
1126 shared_libs: ["libvendoravailable"],
1127 }
1128
1129 // indirect dependency of LLNDK
1130 cc_library {
1131 name: "libvendoravailable",
1132 vendor_available: true,
1133 double_loadable: true,
1134 }
1135 `)
1136}
1137
1138func TestDoubleLoadableDepError(t *testing.T) {
1139 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1140 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1141 cc_library {
1142 name: "libllndk",
1143 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001144 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001145 }
1146
1147 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001148 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001149 symbol_file: "",
1150 }
1151
1152 cc_library {
1153 name: "libnondoubleloadable",
1154 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001155 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001156 vndk: {
1157 enabled: true,
1158 },
1159 }
1160 `)
1161
1162 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1163 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1164 cc_library {
1165 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001166 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001167 shared_libs: ["libnondoubleloadable"],
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: "libnondoubleloadable",
1178 vendor_available: true,
1179 }
1180 `)
1181
Jooyung Hana70f0672019-01-18 15:20:43 +09001182 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1183 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1184 cc_library {
1185 name: "libllndk",
1186 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001187 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001188 }
1189
1190 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001191 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001192 symbol_file: "",
1193 }
1194
1195 cc_library {
1196 name: "libcoreonly",
1197 shared_libs: ["libvendoravailable"],
1198 }
1199
1200 // indirect dependency of LLNDK
1201 cc_library {
1202 name: "libvendoravailable",
1203 vendor_available: true,
1204 }
1205 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001206
1207 // The error is not from 'client' but from 'libllndk'
1208 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1209 cc_library {
1210 name: "client",
1211 vendor_available: true,
1212 double_loadable: true,
1213 shared_libs: ["libllndk"],
1214 }
1215 cc_library {
1216 name: "libllndk",
1217 shared_libs: ["libnondoubleloadable"],
1218 llndk_stubs: "libllndk.llndk",
1219 }
1220 llndk_library {
1221 name: "libllndk.llndk",
1222 symbol_file: "",
1223 }
1224 cc_library {
1225 name: "libnondoubleloadable",
1226 vendor_available: true,
1227 }
1228 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001229}
1230
Jooyung Han479ca172020-10-19 18:51:07 +09001231func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1232 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1233 cc_library {
1234 name: "libvndksp",
1235 shared_libs: ["libanothervndksp"],
1236 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001237 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001238 vndk: {
1239 enabled: true,
1240 support_system_process: true,
1241 }
1242 }
1243
1244 cc_library {
1245 name: "libllndk",
1246 shared_libs: ["libanothervndksp"],
1247 }
1248
1249 llndk_library {
1250 name: "libllndk",
1251 symbol_file: "",
1252 }
1253
1254 cc_library {
1255 name: "libanothervndksp",
1256 vendor_available: true,
1257 }
1258 `)
1259}
1260
Logan Chienf3511742017-10-31 18:04:35 +08001261func TestVndkExt(t *testing.T) {
1262 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001263 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001264 cc_library {
1265 name: "libvndk",
1266 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001267 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001268 vndk: {
1269 enabled: true,
1270 },
1271 nocrt: true,
1272 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001273 cc_library {
1274 name: "libvndk2",
1275 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001276 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001277 vndk: {
1278 enabled: true,
1279 },
1280 target: {
1281 vendor: {
1282 suffix: "-suffix",
1283 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001284 product: {
1285 suffix: "-suffix",
1286 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001287 },
1288 nocrt: true,
1289 }
Logan Chienf3511742017-10-31 18:04:35 +08001290
1291 cc_library {
1292 name: "libvndk_ext",
1293 vendor: true,
1294 vndk: {
1295 enabled: true,
1296 extends: "libvndk",
1297 },
1298 nocrt: true,
1299 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001300
1301 cc_library {
1302 name: "libvndk2_ext",
1303 vendor: true,
1304 vndk: {
1305 enabled: true,
1306 extends: "libvndk2",
1307 },
1308 nocrt: true,
1309 }
Logan Chienf3511742017-10-31 18:04:35 +08001310
Justin Yun0ecf0b22020-02-28 15:07:59 +09001311 cc_library {
1312 name: "libvndk_ext_product",
1313 product_specific: true,
1314 vndk: {
1315 enabled: true,
1316 extends: "libvndk",
1317 },
1318 nocrt: true,
1319 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001320
Justin Yun0ecf0b22020-02-28 15:07:59 +09001321 cc_library {
1322 name: "libvndk2_ext_product",
1323 product_specific: true,
1324 vndk: {
1325 enabled: true,
1326 extends: "libvndk2",
1327 },
1328 nocrt: true,
1329 }
1330 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001331 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001332 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1333 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1334 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1335
1336 ctx := testCcWithConfig(t, config)
1337
1338 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1339 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1340
1341 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1342 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1343
1344 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1345 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001346}
1347
Logan Chiend3c59a22018-03-29 14:08:15 +08001348func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001349 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1350 ctx := testCcNoVndk(t, `
1351 cc_library {
1352 name: "libvndk",
1353 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001354 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001355 vndk: {
1356 enabled: true,
1357 },
1358 nocrt: true,
1359 }
1360
1361 cc_library {
1362 name: "libvndk_ext",
1363 vendor: true,
1364 vndk: {
1365 enabled: true,
1366 extends: "libvndk",
1367 },
1368 nocrt: true,
1369 }
1370 `)
1371
1372 // Ensures that the core variant of "libvndk_ext" can be found.
1373 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1374 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1375 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1376 }
1377}
1378
Justin Yun0ecf0b22020-02-28 15:07:59 +09001379func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1380 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001381 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001382 cc_library {
1383 name: "libvndk",
1384 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001385 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001386 vndk: {
1387 enabled: true,
1388 },
1389 nocrt: true,
1390 }
1391
1392 cc_library {
1393 name: "libvndk_ext_product",
1394 product_specific: true,
1395 vndk: {
1396 enabled: true,
1397 extends: "libvndk",
1398 },
1399 nocrt: true,
1400 }
1401 `)
1402
1403 // Ensures that the core variant of "libvndk_ext_product" can be found.
1404 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1405 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1406 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1407 }
1408}
1409
Logan Chienf3511742017-10-31 18:04:35 +08001410func TestVndkExtError(t *testing.T) {
1411 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001412 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001413 cc_library {
1414 name: "libvndk",
1415 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001416 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001417 vndk: {
1418 enabled: true,
1419 },
1420 nocrt: true,
1421 }
1422
1423 cc_library {
1424 name: "libvndk_ext",
1425 vndk: {
1426 enabled: true,
1427 extends: "libvndk",
1428 },
1429 nocrt: true,
1430 }
1431 `)
1432
1433 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1434 cc_library {
1435 name: "libvndk",
1436 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001437 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001438 vndk: {
1439 enabled: true,
1440 },
1441 nocrt: true,
1442 }
1443
1444 cc_library {
1445 name: "libvndk_ext",
1446 vendor: true,
1447 vndk: {
1448 enabled: true,
1449 },
1450 nocrt: true,
1451 }
1452 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001453
1454 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1455 cc_library {
1456 name: "libvndk",
1457 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001458 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001459 vndk: {
1460 enabled: true,
1461 },
1462 nocrt: true,
1463 }
1464
1465 cc_library {
1466 name: "libvndk_ext_product",
1467 product_specific: true,
1468 vndk: {
1469 enabled: true,
1470 },
1471 nocrt: true,
1472 }
1473 `)
1474
1475 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1476 cc_library {
1477 name: "libvndk",
1478 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001479 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001480 vndk: {
1481 enabled: true,
1482 },
1483 nocrt: true,
1484 }
1485
1486 cc_library {
1487 name: "libvndk_ext_product",
1488 product_specific: true,
1489 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001490 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001491 vndk: {
1492 enabled: true,
1493 extends: "libvndk",
1494 },
1495 nocrt: true,
1496 }
1497 `)
Logan Chienf3511742017-10-31 18:04:35 +08001498}
1499
1500func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1501 // This test ensures an error is emitted for inconsistent support_system_process.
1502 testCcError(t, "module \".*\" with mismatched support_system_process", `
1503 cc_library {
1504 name: "libvndk",
1505 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001506 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001507 vndk: {
1508 enabled: true,
1509 },
1510 nocrt: true,
1511 }
1512
1513 cc_library {
1514 name: "libvndk_sp_ext",
1515 vendor: true,
1516 vndk: {
1517 enabled: true,
1518 extends: "libvndk",
1519 support_system_process: true,
1520 },
1521 nocrt: true,
1522 }
1523 `)
1524
1525 testCcError(t, "module \".*\" with mismatched support_system_process", `
1526 cc_library {
1527 name: "libvndk_sp",
1528 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001529 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001530 vndk: {
1531 enabled: true,
1532 support_system_process: true,
1533 },
1534 nocrt: true,
1535 }
1536
1537 cc_library {
1538 name: "libvndk_ext",
1539 vendor: true,
1540 vndk: {
1541 enabled: true,
1542 extends: "libvndk_sp",
1543 },
1544 nocrt: true,
1545 }
1546 `)
1547}
1548
1549func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001550 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001551 // with `private: true`.
1552 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001553 cc_library {
1554 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001555 vendor_available: true,
1556 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001557 vndk: {
1558 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001559 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001560 },
1561 nocrt: true,
1562 }
1563
1564 cc_library {
1565 name: "libvndk_ext",
1566 vendor: true,
1567 vndk: {
1568 enabled: true,
1569 extends: "libvndk",
1570 },
1571 nocrt: true,
1572 }
1573 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001574
Justin Yunfd9e8042020-12-23 18:23:14 +09001575 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001576 cc_library {
1577 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001578 vendor_available: true,
1579 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001580 vndk: {
1581 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001582 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001583 },
1584 nocrt: true,
1585 }
1586
1587 cc_library {
1588 name: "libvndk_ext_product",
1589 product_specific: true,
1590 vndk: {
1591 enabled: true,
1592 extends: "libvndk",
1593 },
1594 nocrt: true,
1595 }
1596 `)
Logan Chienf3511742017-10-31 18:04:35 +08001597}
1598
Logan Chiend3c59a22018-03-29 14:08:15 +08001599func TestVendorModuleUseVndkExt(t *testing.T) {
1600 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001601 testCc(t, `
1602 cc_library {
1603 name: "libvndk",
1604 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001605 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001606 vndk: {
1607 enabled: true,
1608 },
1609 nocrt: true,
1610 }
1611
1612 cc_library {
1613 name: "libvndk_ext",
1614 vendor: true,
1615 vndk: {
1616 enabled: true,
1617 extends: "libvndk",
1618 },
1619 nocrt: true,
1620 }
1621
1622 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001623 name: "libvndk_sp",
1624 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001625 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001626 vndk: {
1627 enabled: true,
1628 support_system_process: true,
1629 },
1630 nocrt: true,
1631 }
1632
1633 cc_library {
1634 name: "libvndk_sp_ext",
1635 vendor: true,
1636 vndk: {
1637 enabled: true,
1638 extends: "libvndk_sp",
1639 support_system_process: true,
1640 },
1641 nocrt: true,
1642 }
1643
1644 cc_library {
1645 name: "libvendor",
1646 vendor: true,
1647 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1648 nocrt: true,
1649 }
1650 `)
1651}
1652
Logan Chiend3c59a22018-03-29 14:08:15 +08001653func TestVndkExtUseVendorLib(t *testing.T) {
1654 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001655 testCc(t, `
1656 cc_library {
1657 name: "libvndk",
1658 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001659 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001660 vndk: {
1661 enabled: true,
1662 },
1663 nocrt: true,
1664 }
1665
1666 cc_library {
1667 name: "libvndk_ext",
1668 vendor: true,
1669 vndk: {
1670 enabled: true,
1671 extends: "libvndk",
1672 },
1673 shared_libs: ["libvendor"],
1674 nocrt: true,
1675 }
1676
1677 cc_library {
1678 name: "libvendor",
1679 vendor: true,
1680 nocrt: true,
1681 }
1682 `)
Logan Chienf3511742017-10-31 18:04:35 +08001683
Logan Chiend3c59a22018-03-29 14:08:15 +08001684 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1685 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001686 cc_library {
1687 name: "libvndk_sp",
1688 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001689 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001690 vndk: {
1691 enabled: true,
1692 support_system_process: true,
1693 },
1694 nocrt: true,
1695 }
1696
1697 cc_library {
1698 name: "libvndk_sp_ext",
1699 vendor: true,
1700 vndk: {
1701 enabled: true,
1702 extends: "libvndk_sp",
1703 support_system_process: true,
1704 },
1705 shared_libs: ["libvendor"], // Cause an error
1706 nocrt: true,
1707 }
1708
1709 cc_library {
1710 name: "libvendor",
1711 vendor: true,
1712 nocrt: true,
1713 }
1714 `)
1715}
1716
Justin Yun0ecf0b22020-02-28 15:07:59 +09001717func TestProductVndkExtDependency(t *testing.T) {
1718 bp := `
1719 cc_library {
1720 name: "libvndk",
1721 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001722 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001723 vndk: {
1724 enabled: true,
1725 },
1726 nocrt: true,
1727 }
1728
1729 cc_library {
1730 name: "libvndk_ext_product",
1731 product_specific: true,
1732 vndk: {
1733 enabled: true,
1734 extends: "libvndk",
1735 },
1736 shared_libs: ["libproduct_for_vndklibs"],
1737 nocrt: true,
1738 }
1739
1740 cc_library {
1741 name: "libvndk_sp",
1742 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001743 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001744 vndk: {
1745 enabled: true,
1746 support_system_process: true,
1747 },
1748 nocrt: true,
1749 }
1750
1751 cc_library {
1752 name: "libvndk_sp_ext_product",
1753 product_specific: true,
1754 vndk: {
1755 enabled: true,
1756 extends: "libvndk_sp",
1757 support_system_process: true,
1758 },
1759 shared_libs: ["libproduct_for_vndklibs"],
1760 nocrt: true,
1761 }
1762
1763 cc_library {
1764 name: "libproduct",
1765 product_specific: true,
1766 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1767 nocrt: true,
1768 }
1769
1770 cc_library {
1771 name: "libproduct_for_vndklibs",
1772 product_specific: true,
1773 nocrt: true,
1774 }
1775 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001776 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001777 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1778 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1779 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1780
1781 testCcWithConfig(t, config)
1782}
1783
Logan Chiend3c59a22018-03-29 14:08:15 +08001784func TestVndkSpExtUseVndkError(t *testing.T) {
1785 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1786 // library.
1787 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1788 cc_library {
1789 name: "libvndk",
1790 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001791 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001792 vndk: {
1793 enabled: true,
1794 },
1795 nocrt: true,
1796 }
1797
1798 cc_library {
1799 name: "libvndk_sp",
1800 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001801 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001802 vndk: {
1803 enabled: true,
1804 support_system_process: true,
1805 },
1806 nocrt: true,
1807 }
1808
1809 cc_library {
1810 name: "libvndk_sp_ext",
1811 vendor: true,
1812 vndk: {
1813 enabled: true,
1814 extends: "libvndk_sp",
1815 support_system_process: true,
1816 },
1817 shared_libs: ["libvndk"], // Cause an error
1818 nocrt: true,
1819 }
1820 `)
1821
1822 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1823 // library.
1824 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1825 cc_library {
1826 name: "libvndk",
1827 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001828 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001829 vndk: {
1830 enabled: true,
1831 },
1832 nocrt: true,
1833 }
1834
1835 cc_library {
1836 name: "libvndk_ext",
1837 vendor: true,
1838 vndk: {
1839 enabled: true,
1840 extends: "libvndk",
1841 },
1842 nocrt: true,
1843 }
1844
1845 cc_library {
1846 name: "libvndk_sp",
1847 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001848 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001849 vndk: {
1850 enabled: true,
1851 support_system_process: true,
1852 },
1853 nocrt: true,
1854 }
1855
1856 cc_library {
1857 name: "libvndk_sp_ext",
1858 vendor: true,
1859 vndk: {
1860 enabled: true,
1861 extends: "libvndk_sp",
1862 support_system_process: true,
1863 },
1864 shared_libs: ["libvndk_ext"], // Cause an error
1865 nocrt: true,
1866 }
1867 `)
1868}
1869
1870func TestVndkUseVndkExtError(t *testing.T) {
1871 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1872 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001873 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1874 cc_library {
1875 name: "libvndk",
1876 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001877 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001878 vndk: {
1879 enabled: true,
1880 },
1881 nocrt: true,
1882 }
1883
1884 cc_library {
1885 name: "libvndk_ext",
1886 vendor: true,
1887 vndk: {
1888 enabled: true,
1889 extends: "libvndk",
1890 },
1891 nocrt: true,
1892 }
1893
1894 cc_library {
1895 name: "libvndk2",
1896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001897 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001898 vndk: {
1899 enabled: true,
1900 },
1901 shared_libs: ["libvndk_ext"],
1902 nocrt: true,
1903 }
1904 `)
1905
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001906 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001907 cc_library {
1908 name: "libvndk",
1909 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001910 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001911 vndk: {
1912 enabled: true,
1913 },
1914 nocrt: true,
1915 }
1916
1917 cc_library {
1918 name: "libvndk_ext",
1919 vendor: true,
1920 vndk: {
1921 enabled: true,
1922 extends: "libvndk",
1923 },
1924 nocrt: true,
1925 }
1926
1927 cc_library {
1928 name: "libvndk2",
1929 vendor_available: true,
1930 vndk: {
1931 enabled: true,
1932 },
1933 target: {
1934 vendor: {
1935 shared_libs: ["libvndk_ext"],
1936 },
1937 },
1938 nocrt: true,
1939 }
1940 `)
1941
1942 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1943 cc_library {
1944 name: "libvndk_sp",
1945 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001946 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001947 vndk: {
1948 enabled: true,
1949 support_system_process: true,
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_sp_ext",
1956 vendor: true,
1957 vndk: {
1958 enabled: true,
1959 extends: "libvndk_sp",
1960 support_system_process: true,
1961 },
1962 nocrt: true,
1963 }
1964
1965 cc_library {
1966 name: "libvndk_sp_2",
1967 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001968 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001969 vndk: {
1970 enabled: true,
1971 support_system_process: true,
1972 },
1973 shared_libs: ["libvndk_sp_ext"],
1974 nocrt: true,
1975 }
1976 `)
1977
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001978 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001979 cc_library {
1980 name: "libvndk_sp",
1981 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001982 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001983 vndk: {
1984 enabled: true,
1985 },
1986 nocrt: true,
1987 }
1988
1989 cc_library {
1990 name: "libvndk_sp_ext",
1991 vendor: true,
1992 vndk: {
1993 enabled: true,
1994 extends: "libvndk_sp",
1995 },
1996 nocrt: true,
1997 }
1998
1999 cc_library {
2000 name: "libvndk_sp2",
2001 vendor_available: true,
2002 vndk: {
2003 enabled: true,
2004 },
2005 target: {
2006 vendor: {
2007 shared_libs: ["libvndk_sp_ext"],
2008 },
2009 },
2010 nocrt: true,
2011 }
2012 `)
2013}
2014
Justin Yun5f7f7e82019-11-18 19:52:14 +09002015func TestEnforceProductVndkVersion(t *testing.T) {
2016 bp := `
2017 cc_library {
2018 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002019 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002020 }
2021 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002022 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002023 symbol_file: "",
2024 }
2025 cc_library {
2026 name: "libvndk",
2027 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002028 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002029 vndk: {
2030 enabled: true,
2031 },
2032 nocrt: true,
2033 }
2034 cc_library {
2035 name: "libvndk_sp",
2036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002037 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002038 vndk: {
2039 enabled: true,
2040 support_system_process: true,
2041 },
2042 nocrt: true,
2043 }
2044 cc_library {
2045 name: "libva",
2046 vendor_available: true,
2047 nocrt: true,
2048 }
2049 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002050 name: "libpa",
2051 product_available: true,
2052 nocrt: true,
2053 }
2054 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002055 name: "libboth_available",
2056 vendor_available: true,
2057 product_available: true,
2058 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002059 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002060 target: {
2061 vendor: {
2062 suffix: "-vendor",
2063 },
2064 product: {
2065 suffix: "-product",
2066 },
2067 }
2068 }
2069 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002070 name: "libproduct_va",
2071 product_specific: true,
2072 vendor_available: true,
2073 nocrt: true,
2074 }
2075 cc_library {
2076 name: "libprod",
2077 product_specific: true,
2078 shared_libs: [
2079 "libllndk",
2080 "libvndk",
2081 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002082 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002083 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002084 "libproduct_va",
2085 ],
2086 nocrt: true,
2087 }
2088 cc_library {
2089 name: "libvendor",
2090 vendor: true,
2091 shared_libs: [
2092 "libllndk",
2093 "libvndk",
2094 "libvndk_sp",
2095 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002096 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002097 "libproduct_va",
2098 ],
2099 nocrt: true,
2100 }
2101 `
2102
Paul Duffin8567f222021-03-23 00:02:06 +00002103 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002104
Jooyung Han261e1582020-10-20 18:54:21 +09002105 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2106 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002107
2108 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2109 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2110
2111 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2112 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002113
2114 ensureStringContains := func(t *testing.T, str string, substr string) {
2115 t.Helper()
2116 if !strings.Contains(str, substr) {
2117 t.Errorf("%q is not found in %v", substr, str)
2118 }
2119 }
2120 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2121 t.Helper()
2122 if strings.Contains(str, substr) {
2123 t.Errorf("%q is found in %v", substr, str)
2124 }
2125 }
2126
2127 // _static variant is used since _shared reuses *.o from the static variant
2128 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2129 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2130
2131 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2132 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2133 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2134 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2135
2136 product_cflags := product_static.Rule("cc").Args["cFlags"]
2137 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2138 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2139 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002140}
2141
2142func TestEnforceProductVndkVersionErrors(t *testing.T) {
2143 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2144 cc_library {
2145 name: "libprod",
2146 product_specific: true,
2147 shared_libs: [
2148 "libvendor",
2149 ],
2150 nocrt: true,
2151 }
2152 cc_library {
2153 name: "libvendor",
2154 vendor: true,
2155 nocrt: true,
2156 }
2157 `)
2158 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2159 cc_library {
2160 name: "libprod",
2161 product_specific: true,
2162 shared_libs: [
2163 "libsystem",
2164 ],
2165 nocrt: true,
2166 }
2167 cc_library {
2168 name: "libsystem",
2169 nocrt: true,
2170 }
2171 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002172 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2173 cc_library {
2174 name: "libprod",
2175 product_specific: true,
2176 shared_libs: [
2177 "libva",
2178 ],
2179 nocrt: true,
2180 }
2181 cc_library {
2182 name: "libva",
2183 vendor_available: true,
2184 nocrt: true,
2185 }
2186 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002187 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002188 cc_library {
2189 name: "libprod",
2190 product_specific: true,
2191 shared_libs: [
2192 "libvndk_private",
2193 ],
2194 nocrt: true,
2195 }
2196 cc_library {
2197 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002198 vendor_available: true,
2199 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002200 vndk: {
2201 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002202 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002203 },
2204 nocrt: true,
2205 }
2206 `)
2207 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2208 cc_library {
2209 name: "libprod",
2210 product_specific: true,
2211 shared_libs: [
2212 "libsystem_ext",
2213 ],
2214 nocrt: true,
2215 }
2216 cc_library {
2217 name: "libsystem_ext",
2218 system_ext_specific: true,
2219 nocrt: true,
2220 }
2221 `)
2222 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2223 cc_library {
2224 name: "libsystem",
2225 shared_libs: [
2226 "libproduct_va",
2227 ],
2228 nocrt: true,
2229 }
2230 cc_library {
2231 name: "libproduct_va",
2232 product_specific: true,
2233 vendor_available: true,
2234 nocrt: true,
2235 }
2236 `)
2237}
2238
Jooyung Han38002912019-05-16 04:01:54 +09002239func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002240 bp := `
2241 cc_library {
2242 name: "libvndk",
2243 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002244 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002245 vndk: {
2246 enabled: true,
2247 },
2248 }
2249 cc_library {
2250 name: "libvndksp",
2251 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002252 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002253 vndk: {
2254 enabled: true,
2255 support_system_process: true,
2256 },
2257 }
2258 cc_library {
2259 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002260 vendor_available: true,
2261 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002262 vndk: {
2263 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002264 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002265 },
2266 }
2267 cc_library {
2268 name: "libvendor",
2269 vendor: true,
2270 }
2271 cc_library {
2272 name: "libvndkext",
2273 vendor: true,
2274 vndk: {
2275 enabled: true,
2276 extends: "libvndk",
2277 },
2278 }
2279 vndk_prebuilt_shared {
2280 name: "prevndk",
2281 version: "27",
2282 target_arch: "arm",
2283 binder32bit: true,
2284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002285 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002286 vndk: {
2287 enabled: true,
2288 },
2289 arch: {
2290 arm: {
2291 srcs: ["liba.so"],
2292 },
2293 },
2294 }
2295 cc_library {
2296 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002297 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002298 }
2299 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002300 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002301 symbol_file: "",
2302 }
2303 cc_library {
2304 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002305 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002306 }
2307 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002308 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002309 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002310 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002311 }
2312
2313 llndk_libraries_txt {
2314 name: "llndk.libraries.txt",
2315 }
2316 vndkcore_libraries_txt {
2317 name: "vndkcore.libraries.txt",
2318 }
2319 vndksp_libraries_txt {
2320 name: "vndksp.libraries.txt",
2321 }
2322 vndkprivate_libraries_txt {
2323 name: "vndkprivate.libraries.txt",
2324 }
2325 vndkcorevariant_libraries_txt {
2326 name: "vndkcorevariant.libraries.txt",
2327 insert_vndk_version: false,
2328 }
2329 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002330
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002331 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002332 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2333 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2334 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002335 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002336
Colin Cross78212242021-01-06 14:51:30 -08002337 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2338 []string{"libvndk.so", "libvndkprivate.so"})
2339 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2340 []string{"libc++.so", "libvndksp.so"})
2341 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2342 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2343 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2344 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002345
Colin Crossfb0c16e2019-11-20 17:12:35 -08002346 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002347
Jooyung Han38002912019-05-16 04:01:54 +09002348 tests := []struct {
2349 variant string
2350 name string
2351 expected string
2352 }{
2353 {vendorVariant, "libvndk", "native:vndk"},
2354 {vendorVariant, "libvndksp", "native:vndk"},
2355 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2356 {vendorVariant, "libvendor", "native:vendor"},
2357 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002358 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002359 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002360 {coreVariant, "libvndk", "native:platform"},
2361 {coreVariant, "libvndkprivate", "native:platform"},
2362 {coreVariant, "libllndk", "native:platform"},
2363 }
2364 for _, test := range tests {
2365 t.Run(test.name, func(t *testing.T) {
2366 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2367 assertString(t, module.makeLinkType, test.expected)
2368 })
2369 }
2370}
2371
Jeff Gaston294356f2017-09-27 17:05:30 -07002372var staticLinkDepOrderTestCases = []struct {
2373 // This is a string representation of a map[moduleName][]moduleDependency .
2374 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002375 inStatic string
2376
2377 // This is a string representation of a map[moduleName][]moduleDependency .
2378 // It models the dependencies declared in an Android.bp file.
2379 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002380
2381 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2382 // The keys of allOrdered specify which modules we would like to check.
2383 // The values of allOrdered specify the expected result (of the transitive closure of all
2384 // dependencies) for each module to test
2385 allOrdered string
2386
2387 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2388 // The keys of outOrdered specify which modules we would like to check.
2389 // The values of outOrdered specify the expected result (of the ordered linker command line)
2390 // for each module to test.
2391 outOrdered string
2392}{
2393 // Simple tests
2394 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002395 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002396 outOrdered: "",
2397 },
2398 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002399 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002400 outOrdered: "a:",
2401 },
2402 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002403 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002404 outOrdered: "a:b; b:",
2405 },
2406 // Tests of reordering
2407 {
2408 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002409 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002410 outOrdered: "a:b,c,d; b:d; c:d; d:",
2411 },
2412 {
2413 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002414 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002415 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2416 },
2417 {
2418 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002419 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002420 outOrdered: "a:d,b,e,c; d:b; e:c",
2421 },
2422 {
2423 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002424 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002425 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2426 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2427 },
2428 {
2429 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002430 inStatic: "a:b,c,d,e,f,g,h; f:b,c,d; b:c,d; c:d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002431 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2432 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2433 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002434 // shared dependencies
2435 {
2436 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2437 // So, we don't actually have to check that a shared dependency of c will change the order
2438 // of a library that depends statically on b and on c. We only need to check that if c has
2439 // a shared dependency on b, that that shows up in allOrdered.
2440 inShared: "c:b",
2441 allOrdered: "c:b",
2442 outOrdered: "c:",
2443 },
2444 {
2445 // This test doesn't actually include any shared dependencies but it's a reminder of what
2446 // the second phase of the above test would look like
2447 inStatic: "a:b,c; c:b",
2448 allOrdered: "a:c,b; c:b",
2449 outOrdered: "a:c,b; c:b",
2450 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002451 // tiebreakers for when two modules specifying different orderings and there is no dependency
2452 // to dictate an order
2453 {
2454 // if the tie is between two modules at the end of a's deps, then a's order wins
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002455 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002456 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2457 },
2458 {
2459 // if the tie is between two modules at the start of a's deps, then c's order is used
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002460 inStatic: "a1:d,e,b1,c1; b1:d,e; c1:e,d; a2:d,e,b2,c2; b2:d,e; c2:d,e",
Jeff Gaston294356f2017-09-27 17:05:30 -07002461 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2462 },
2463 // Tests involving duplicate dependencies
2464 {
2465 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002466 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002467 outOrdered: "a:c,b",
2468 },
2469 {
2470 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002471 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002472 outOrdered: "a:d,c,b",
2473 },
2474 // Tests to confirm the nonexistence of infinite loops.
2475 // These cases should never happen, so as long as the test terminates and the
2476 // result is deterministic then that should be fine.
2477 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002478 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002479 outOrdered: "a:a",
2480 },
2481 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002482 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002483 allOrdered: "a:b,c; b:c,a; c:a,b",
2484 outOrdered: "a:b; b:c; c:a",
2485 },
2486 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002487 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002488 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2489 outOrdered: "a:c,b; b:a,c; c:b,a",
2490 },
2491}
2492
2493// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2494func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2495 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2496 strippedText := strings.Replace(text, " ", "", -1)
2497 if len(strippedText) < 1 {
2498 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2499 }
2500 allDeps = make(map[android.Path][]android.Path, 0)
2501
2502 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2503 moduleTexts := strings.Split(strippedText, ";")
2504
2505 outputForModuleName := func(moduleName string) android.Path {
2506 return android.PathForTesting(moduleName)
2507 }
2508
2509 for _, moduleText := range moduleTexts {
2510 // convert from "a:b,c" to ["a", "b,c"]
2511 components := strings.Split(moduleText, ":")
2512 if len(components) != 2 {
2513 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2514 }
2515 moduleName := components[0]
2516 moduleOutput := outputForModuleName(moduleName)
2517 modulesInOrder = append(modulesInOrder, moduleOutput)
2518
2519 depString := components[1]
2520 // convert from "b,c" to ["b", "c"]
2521 depNames := strings.Split(depString, ",")
2522 if len(depString) < 1 {
2523 depNames = []string{}
2524 }
2525 var deps []android.Path
2526 for _, depName := range depNames {
2527 deps = append(deps, outputForModuleName(depName))
2528 }
2529 allDeps[moduleOutput] = deps
2530 }
2531 return modulesInOrder, allDeps
2532}
2533
Jeff Gaston294356f2017-09-27 17:05:30 -07002534func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2535 for _, moduleName := range moduleNames {
2536 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002537 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002538 paths = append(paths, output)
2539 }
2540 return paths
2541}
2542
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002543func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002544 ctx := testCc(t, `
2545 cc_library {
2546 name: "a",
2547 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002548 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002549 }
2550 cc_library {
2551 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002552 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002553 }
2554 cc_library {
2555 name: "c",
2556 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002557 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002558 }
2559 cc_library {
2560 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002561 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002562 }
2563
2564 `)
2565
Colin Cross7113d202019-11-20 16:39:12 -08002566 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002568 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2569 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002570 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002571
2572 if !reflect.DeepEqual(actual, expected) {
2573 t.Errorf("staticDeps orderings were not propagated correctly"+
2574 "\nactual: %v"+
2575 "\nexpected: %v",
2576 actual,
2577 expected,
2578 )
2579 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002580}
Jeff Gaston294356f2017-09-27 17:05:30 -07002581
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002582func TestStaticLibDepReorderingWithShared(t *testing.T) {
2583 ctx := testCc(t, `
2584 cc_library {
2585 name: "a",
2586 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002587 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002588 }
2589 cc_library {
2590 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002591 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002592 }
2593 cc_library {
2594 name: "c",
2595 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002596 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002597 }
2598
2599 `)
2600
Colin Cross7113d202019-11-20 16:39:12 -08002601 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002602 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002603 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2604 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002605 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002606
2607 if !reflect.DeepEqual(actual, expected) {
2608 t.Errorf("staticDeps orderings did not account for shared libs"+
2609 "\nactual: %v"+
2610 "\nexpected: %v",
2611 actual,
2612 expected,
2613 )
2614 }
2615}
2616
Jooyung Hanb04a4992020-03-13 18:57:35 +09002617func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002618 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002619 if !reflect.DeepEqual(actual, expected) {
2620 t.Errorf(message+
2621 "\nactual: %v"+
2622 "\nexpected: %v",
2623 actual,
2624 expected,
2625 )
2626 }
2627}
2628
Jooyung Han61b66e92020-03-21 14:21:46 +00002629func TestLlndkLibrary(t *testing.T) {
2630 ctx := testCc(t, `
2631 cc_library {
2632 name: "libllndk",
2633 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002634 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002635 }
2636 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002637 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002638 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002639
2640 cc_prebuilt_library_shared {
2641 name: "libllndkprebuilt",
2642 stubs: { versions: ["1", "2"] },
2643 llndk_stubs: "libllndkprebuilt.llndk",
2644 }
2645 llndk_library {
2646 name: "libllndkprebuilt.llndk",
2647 }
2648
2649 cc_library {
2650 name: "libllndk_with_external_headers",
2651 stubs: { versions: ["1", "2"] },
2652 llndk_stubs: "libllndk_with_external_headers.llndk",
2653 header_libs: ["libexternal_headers"],
2654 export_header_lib_headers: ["libexternal_headers"],
2655 }
2656 llndk_library {
2657 name: "libllndk_with_external_headers.llndk",
2658 }
2659 cc_library_headers {
2660 name: "libexternal_headers",
2661 export_include_dirs: ["include"],
2662 vendor_available: true,
2663 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002664 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002665 actual := ctx.ModuleVariantsForTests("libllndk")
2666 for i := 0; i < len(actual); i++ {
2667 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2668 actual = append(actual[:i], actual[i+1:]...)
2669 i--
2670 }
2671 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002672 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002673 "android_vendor.VER_arm64_armv8-a_shared_1",
2674 "android_vendor.VER_arm64_armv8-a_shared_2",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002675 "android_vendor.VER_arm64_armv8-a_shared_current",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002676 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002677 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2678 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002679 "android_vendor.VER_arm_armv7-a-neon_shared_current",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002680 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002681 }
2682 checkEquals(t, "variants for llndk stubs", expected, actual)
2683
Colin Cross127bb8b2020-12-16 16:46:01 -08002684 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002685 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2686
Colin Cross127bb8b2020-12-16 16:46:01 -08002687 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002688 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2689}
2690
Jiyong Parka46a4d52017-12-14 19:54:34 +09002691func TestLlndkHeaders(t *testing.T) {
2692 ctx := testCc(t, `
2693 llndk_headers {
2694 name: "libllndk_headers",
2695 export_include_dirs: ["my_include"],
2696 }
2697 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002698 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002699 export_llndk_headers: ["libllndk_headers"],
2700 }
2701 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002702 name: "libllndk",
2703 llndk_stubs: "libllndk.llndk",
2704 }
2705
2706 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002707 name: "libvendor",
2708 shared_libs: ["libllndk"],
2709 vendor: true,
2710 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002711 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002712 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002713 }
2714 `)
2715
2716 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002717 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002718 cflags := cc.Args["cFlags"]
2719 if !strings.Contains(cflags, "-Imy_include") {
2720 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2721 }
2722}
2723
Logan Chien43d34c32017-12-20 01:17:32 +08002724func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2725 actual := module.Properties.AndroidMkRuntimeLibs
2726 if !reflect.DeepEqual(actual, expected) {
2727 t.Errorf("incorrect runtime_libs for shared libs"+
2728 "\nactual: %v"+
2729 "\nexpected: %v",
2730 actual,
2731 expected,
2732 )
2733 }
2734}
2735
2736const runtimeLibAndroidBp = `
2737 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002738 name: "liball_available",
2739 vendor_available: true,
2740 product_available: true,
2741 no_libcrt : true,
2742 nocrt : true,
2743 system_shared_libs : [],
2744 }
2745 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002746 name: "libvendor_available1",
2747 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002748 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002749 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002750 nocrt : true,
2751 system_shared_libs : [],
2752 }
2753 cc_library {
2754 name: "libvendor_available2",
2755 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002756 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002757 target: {
2758 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002759 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002760 }
2761 },
Yi Konge7fe9912019-06-02 00:53:50 -07002762 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002763 nocrt : true,
2764 system_shared_libs : [],
2765 }
2766 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002767 name: "libproduct_vendor",
2768 product_specific: true,
2769 vendor_available: true,
2770 no_libcrt : true,
2771 nocrt : true,
2772 system_shared_libs : [],
2773 }
2774 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002775 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002776 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002777 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002778 nocrt : true,
2779 system_shared_libs : [],
2780 }
2781 cc_library {
2782 name: "libvendor1",
2783 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002784 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002785 nocrt : true,
2786 system_shared_libs : [],
2787 }
2788 cc_library {
2789 name: "libvendor2",
2790 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002791 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002792 no_libcrt : true,
2793 nocrt : true,
2794 system_shared_libs : [],
2795 }
2796 cc_library {
2797 name: "libproduct_available1",
2798 product_available: true,
2799 runtime_libs: ["liball_available"],
2800 no_libcrt : true,
2801 nocrt : true,
2802 system_shared_libs : [],
2803 }
2804 cc_library {
2805 name: "libproduct1",
2806 product_specific: true,
2807 no_libcrt : true,
2808 nocrt : true,
2809 system_shared_libs : [],
2810 }
2811 cc_library {
2812 name: "libproduct2",
2813 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002814 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002815 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002816 nocrt : true,
2817 system_shared_libs : [],
2818 }
2819`
2820
2821func TestRuntimeLibs(t *testing.T) {
2822 ctx := testCc(t, runtimeLibAndroidBp)
2823
2824 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002825 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002826
Justin Yun8a2600c2020-12-07 12:44:03 +09002827 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2828 checkRuntimeLibs(t, []string{"liball_available"}, module)
2829
2830 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2831 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002832
2833 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002834 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002835
2836 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2837 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002838 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002839
Justin Yun8a2600c2020-12-07 12:44:03 +09002840 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2841 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002842
2843 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002844 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002845
2846 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2847 // and product variants.
2848 variant = "android_product.VER_arm64_armv8-a_shared"
2849
2850 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2851 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2852
2853 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002854 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002855}
2856
2857func TestExcludeRuntimeLibs(t *testing.T) {
2858 ctx := testCc(t, runtimeLibAndroidBp)
2859
Colin Cross7113d202019-11-20 16:39:12 -08002860 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002861 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2862 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002863
Colin Crossfb0c16e2019-11-20 17:12:35 -08002864 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002865 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002866 checkRuntimeLibs(t, nil, module)
2867}
2868
2869func TestRuntimeLibsNoVndk(t *testing.T) {
2870 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2871
2872 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2873
Colin Cross7113d202019-11-20 16:39:12 -08002874 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002875
Justin Yun8a2600c2020-12-07 12:44:03 +09002876 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2877 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002878
2879 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002880 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002881
2882 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002883 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002884}
2885
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002886func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002887 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002888 actual := module.Properties.AndroidMkStaticLibs
2889 if !reflect.DeepEqual(actual, expected) {
2890 t.Errorf("incorrect static_libs"+
2891 "\nactual: %v"+
2892 "\nexpected: %v",
2893 actual,
2894 expected,
2895 )
2896 }
2897}
2898
2899const staticLibAndroidBp = `
2900 cc_library {
2901 name: "lib1",
2902 }
2903 cc_library {
2904 name: "lib2",
2905 static_libs: ["lib1"],
2906 }
2907`
2908
2909func TestStaticLibDepExport(t *testing.T) {
2910 ctx := testCc(t, staticLibAndroidBp)
2911
2912 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002913 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002914 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002915 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002916
2917 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002918 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002919 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2920 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002921 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002922}
2923
Jiyong Parkd08b6972017-09-26 10:50:54 +09002924var compilerFlagsTestCases = []struct {
2925 in string
2926 out bool
2927}{
2928 {
2929 in: "a",
2930 out: false,
2931 },
2932 {
2933 in: "-a",
2934 out: true,
2935 },
2936 {
2937 in: "-Ipath/to/something",
2938 out: false,
2939 },
2940 {
2941 in: "-isystempath/to/something",
2942 out: false,
2943 },
2944 {
2945 in: "--coverage",
2946 out: false,
2947 },
2948 {
2949 in: "-include a/b",
2950 out: true,
2951 },
2952 {
2953 in: "-include a/b c/d",
2954 out: false,
2955 },
2956 {
2957 in: "-DMACRO",
2958 out: true,
2959 },
2960 {
2961 in: "-DMAC RO",
2962 out: false,
2963 },
2964 {
2965 in: "-a -b",
2966 out: false,
2967 },
2968 {
2969 in: "-DMACRO=definition",
2970 out: true,
2971 },
2972 {
2973 in: "-DMACRO=defi nition",
2974 out: true, // TODO(jiyong): this should be false
2975 },
2976 {
2977 in: "-DMACRO(x)=x + 1",
2978 out: true,
2979 },
2980 {
2981 in: "-DMACRO=\"defi nition\"",
2982 out: true,
2983 },
2984}
2985
2986type mockContext struct {
2987 BaseModuleContext
2988 result bool
2989}
2990
2991func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2992 // CheckBadCompilerFlags calls this function when the flag should be rejected
2993 ctx.result = false
2994}
2995
2996func TestCompilerFlags(t *testing.T) {
2997 for _, testCase := range compilerFlagsTestCases {
2998 ctx := &mockContext{result: true}
2999 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3000 if ctx.result != testCase.out {
3001 t.Errorf("incorrect output:")
3002 t.Errorf(" input: %#v", testCase.in)
3003 t.Errorf(" expected: %#v", testCase.out)
3004 t.Errorf(" got: %#v", ctx.result)
3005 }
3006 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003007}
Jiyong Park374510b2018-03-19 18:23:01 +09003008
3009func TestVendorPublicLibraries(t *testing.T) {
3010 ctx := testCc(t, `
3011 cc_library_headers {
3012 name: "libvendorpublic_headers",
3013 export_include_dirs: ["my_include"],
3014 }
3015 vendor_public_library {
3016 name: "libvendorpublic",
3017 symbol_file: "",
3018 export_public_headers: ["libvendorpublic_headers"],
3019 }
3020 cc_library {
3021 name: "libvendorpublic",
3022 srcs: ["foo.c"],
3023 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003024 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003025 nocrt: true,
3026 }
3027
3028 cc_library {
3029 name: "libsystem",
3030 shared_libs: ["libvendorpublic"],
3031 vendor: false,
3032 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003033 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003034 nocrt: true,
3035 }
3036 cc_library {
3037 name: "libvendor",
3038 shared_libs: ["libvendorpublic"],
3039 vendor: true,
3040 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003041 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003042 nocrt: true,
3043 }
3044 `)
3045
Colin Cross7113d202019-11-20 16:39:12 -08003046 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003047 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003048
3049 // test if header search paths are correctly added
3050 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003051 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003052 cflags := cc.Args["cFlags"]
3053 if !strings.Contains(cflags, "-Imy_include") {
3054 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3055 }
3056
3057 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003058 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003059 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003060 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003061 if !strings.Contains(libflags, stubPaths[0].String()) {
3062 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3063 }
3064
3065 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003066 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003067 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003068 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003069 if !strings.Contains(libflags, stubPaths[0].String()) {
3070 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3071 }
3072
3073}
Jiyong Park37b25202018-07-11 10:49:27 +09003074
3075func TestRecovery(t *testing.T) {
3076 ctx := testCc(t, `
3077 cc_library_shared {
3078 name: "librecovery",
3079 recovery: true,
3080 }
3081 cc_library_shared {
3082 name: "librecovery32",
3083 recovery: true,
3084 compile_multilib:"32",
3085 }
Jiyong Park5baac542018-08-28 09:55:37 +09003086 cc_library_shared {
3087 name: "libHalInRecovery",
3088 recovery_available: true,
3089 vendor: true,
3090 }
Jiyong Park37b25202018-07-11 10:49:27 +09003091 `)
3092
3093 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003094 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003095 if len(variants) != 1 || !android.InList(arm64, variants) {
3096 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3097 }
3098
3099 variants = ctx.ModuleVariantsForTests("librecovery32")
3100 if android.InList(arm64, variants) {
3101 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3102 }
Jiyong Park5baac542018-08-28 09:55:37 +09003103
3104 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3105 if !recoveryModule.Platform() {
3106 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3107 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003108}
Jiyong Park5baac542018-08-28 09:55:37 +09003109
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003110func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3111 bp := `
3112 cc_prebuilt_test_library_shared {
3113 name: "test_lib",
3114 relative_install_path: "foo/bar/baz",
3115 srcs: ["srcpath/dontusethispath/baz.so"],
3116 }
3117
3118 cc_test {
3119 name: "main_test",
3120 data_libs: ["test_lib"],
3121 gtest: false,
3122 }
3123 `
3124
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003125 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003126 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3127 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3128 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3129
3130 ctx := testCcWithConfig(t, config)
3131 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3132 testBinary := module.(*Module).linker.(*testBinary)
3133 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3134 if err != nil {
3135 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3136 }
3137 if len(outputFiles) != 1 {
3138 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3139 }
3140 if len(testBinary.dataPaths()) != 1 {
3141 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3142 }
3143
3144 outputPath := outputFiles[0].String()
3145
3146 if !strings.HasSuffix(outputPath, "/main_test") {
3147 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3148 }
Colin Crossaa255532020-07-03 13:18:24 -07003149 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003150 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3151 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3152 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3153 }
3154}
3155
Jiyong Park7ed9de32018-10-15 22:25:07 +09003156func TestVersionedStubs(t *testing.T) {
3157 ctx := testCc(t, `
3158 cc_library_shared {
3159 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003160 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003161 stubs: {
3162 symbol_file: "foo.map.txt",
3163 versions: ["1", "2", "3"],
3164 },
3165 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003166
Jiyong Park7ed9de32018-10-15 22:25:07 +09003167 cc_library_shared {
3168 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003169 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003170 shared_libs: ["libFoo#1"],
3171 }`)
3172
3173 variants := ctx.ModuleVariantsForTests("libFoo")
3174 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003175 "android_arm64_armv8-a_shared",
3176 "android_arm64_armv8-a_shared_1",
3177 "android_arm64_armv8-a_shared_2",
3178 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003179 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003180 "android_arm_armv7-a-neon_shared",
3181 "android_arm_armv7-a-neon_shared_1",
3182 "android_arm_armv7-a-neon_shared_2",
3183 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003184 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003185 }
3186 variantsMismatch := false
3187 if len(variants) != len(expectedVariants) {
3188 variantsMismatch = true
3189 } else {
3190 for _, v := range expectedVariants {
3191 if !inList(v, variants) {
3192 variantsMismatch = false
3193 }
3194 }
3195 }
3196 if variantsMismatch {
3197 t.Errorf("variants of libFoo expected:\n")
3198 for _, v := range expectedVariants {
3199 t.Errorf("%q\n", v)
3200 }
3201 t.Errorf(", but got:\n")
3202 for _, v := range variants {
3203 t.Errorf("%q\n", v)
3204 }
3205 }
3206
Colin Cross7113d202019-11-20 16:39:12 -08003207 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003208 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003209 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003210 if !strings.Contains(libFlags, libFoo1StubPath) {
3211 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3212 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003213
Colin Cross7113d202019-11-20 16:39:12 -08003214 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003215 cFlags := libBarCompileRule.Args["cFlags"]
3216 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3217 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3218 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3219 }
Jiyong Park37b25202018-07-11 10:49:27 +09003220}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003221
Jooyung Hanb04a4992020-03-13 18:57:35 +09003222func TestVersioningMacro(t *testing.T) {
3223 for _, tc := range []struct{ moduleName, expected string }{
3224 {"libc", "__LIBC_API__"},
3225 {"libfoo", "__LIBFOO_API__"},
3226 {"libfoo@1", "__LIBFOO_1_API__"},
3227 {"libfoo-v1", "__LIBFOO_V1_API__"},
3228 {"libfoo.v1", "__LIBFOO_V1_API__"},
3229 } {
3230 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3231 }
3232}
3233
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003234func TestStaticExecutable(t *testing.T) {
3235 ctx := testCc(t, `
3236 cc_binary {
3237 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003238 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003239 static_executable: true,
3240 }`)
3241
Colin Cross7113d202019-11-20 16:39:12 -08003242 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003243 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3244 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003245 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003246 for _, lib := range systemStaticLibs {
3247 if !strings.Contains(libFlags, lib) {
3248 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3249 }
3250 }
3251 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3252 for _, lib := range systemSharedLibs {
3253 if strings.Contains(libFlags, lib) {
3254 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3255 }
3256 }
3257}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003258
3259func TestStaticDepsOrderWithStubs(t *testing.T) {
3260 ctx := testCc(t, `
3261 cc_binary {
3262 name: "mybin",
3263 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003264 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003265 static_executable: true,
3266 stl: "none",
3267 }
3268
3269 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003270 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003271 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003272 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003273 stl: "none",
3274 }
3275
3276 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003277 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003278 srcs: ["foo.c"],
3279 stl: "none",
3280 stubs: {
3281 versions: ["1"],
3282 },
3283 }`)
3284
Colin Cross0de8a1e2020-09-18 14:15:30 -07003285 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3286 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003287 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003288
3289 if !reflect.DeepEqual(actual, expected) {
3290 t.Errorf("staticDeps orderings were not propagated correctly"+
3291 "\nactual: %v"+
3292 "\nexpected: %v",
3293 actual,
3294 expected,
3295 )
3296 }
3297}
Jooyung Han38002912019-05-16 04:01:54 +09003298
Jooyung Hand48f3c32019-08-23 11:18:57 +09003299func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3300 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3301 cc_library {
3302 name: "libA",
3303 srcs: ["foo.c"],
3304 shared_libs: ["libB"],
3305 stl: "none",
3306 }
3307
3308 cc_library {
3309 name: "libB",
3310 srcs: ["foo.c"],
3311 enabled: false,
3312 stl: "none",
3313 }
3314 `)
3315}
3316
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003317// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3318// correctly.
3319func TestFuzzTarget(t *testing.T) {
3320 ctx := testCc(t, `
3321 cc_fuzz {
3322 name: "fuzz_smoke_test",
3323 srcs: ["foo.c"],
3324 }`)
3325
Paul Duffin075c4172019-12-19 19:06:13 +00003326 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003327 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3328}
3329
Jiyong Park29074592019-07-07 16:27:47 +09003330func TestAidl(t *testing.T) {
3331}
3332
Jooyung Han38002912019-05-16 04:01:54 +09003333func assertString(t *testing.T, got, expected string) {
3334 t.Helper()
3335 if got != expected {
3336 t.Errorf("expected %q got %q", expected, got)
3337 }
3338}
3339
3340func assertArrayString(t *testing.T, got, expected []string) {
3341 t.Helper()
3342 if len(got) != len(expected) {
3343 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3344 return
3345 }
3346 for i := range got {
3347 if got[i] != expected[i] {
3348 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3349 i, expected[i], expected, got[i], got)
3350 return
3351 }
3352 }
3353}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003354
Jooyung Han0302a842019-10-30 18:43:49 +09003355func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3356 t.Helper()
3357 assertArrayString(t, android.SortedStringKeys(m), expected)
3358}
3359
Colin Crosse1bb5d02019-09-24 14:55:04 -07003360func TestDefaults(t *testing.T) {
3361 ctx := testCc(t, `
3362 cc_defaults {
3363 name: "defaults",
3364 srcs: ["foo.c"],
3365 static: {
3366 srcs: ["bar.c"],
3367 },
3368 shared: {
3369 srcs: ["baz.c"],
3370 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003371 bazel_module: {
3372 bp2build_available: true,
3373 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003374 }
3375
3376 cc_library_static {
3377 name: "libstatic",
3378 defaults: ["defaults"],
3379 }
3380
3381 cc_library_shared {
3382 name: "libshared",
3383 defaults: ["defaults"],
3384 }
3385
3386 cc_library {
3387 name: "libboth",
3388 defaults: ["defaults"],
3389 }
3390
3391 cc_binary {
3392 name: "binary",
3393 defaults: ["defaults"],
3394 }`)
3395
3396 pathsToBase := func(paths android.Paths) []string {
3397 var ret []string
3398 for _, p := range paths {
3399 ret = append(ret, p.Base())
3400 }
3401 return ret
3402 }
3403
Colin Cross7113d202019-11-20 16:39:12 -08003404 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003405 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3406 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3407 }
Colin Cross7113d202019-11-20 16:39:12 -08003408 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003409 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3410 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3411 }
Colin Cross7113d202019-11-20 16:39:12 -08003412 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003413 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3414 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3415 }
3416
Colin Cross7113d202019-11-20 16:39:12 -08003417 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003418 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3419 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3420 }
Colin Cross7113d202019-11-20 16:39:12 -08003421 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003422 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3423 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3424 }
3425}
Colin Crosseabaedd2020-02-06 17:01:55 -08003426
3427func TestProductVariableDefaults(t *testing.T) {
3428 bp := `
3429 cc_defaults {
3430 name: "libfoo_defaults",
3431 srcs: ["foo.c"],
3432 cppflags: ["-DFOO"],
3433 product_variables: {
3434 debuggable: {
3435 cppflags: ["-DBAR"],
3436 },
3437 },
3438 }
3439
3440 cc_library {
3441 name: "libfoo",
3442 defaults: ["libfoo_defaults"],
3443 }
3444 `
3445
Paul Duffin8567f222021-03-23 00:02:06 +00003446 result := android.GroupFixturePreparers(
3447 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003448 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003449
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003450 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3451 variables.Debuggable = BoolPtr(true)
3452 }),
3453 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003454
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003455 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003456 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003457}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003458
3459func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3460 t.Parallel()
3461 bp := `
3462 cc_library_static {
3463 name: "libfoo",
3464 srcs: ["foo.c"],
3465 whole_static_libs: ["libbar"],
3466 }
3467
3468 cc_library_static {
3469 name: "libbar",
3470 whole_static_libs: ["libmissing"],
3471 }
3472 `
3473
Paul Duffin8567f222021-03-23 00:02:06 +00003474 result := android.GroupFixturePreparers(
3475 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003476 android.PrepareForTestWithAllowMissingDependencies,
3477 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003478
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003479 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003480 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003481
Paul Duffine84b1332021-03-12 11:59:43 +00003482 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003483
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003484 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003485 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003486}
Colin Crosse9fe2942020-11-10 18:12:15 -08003487
3488func TestInstallSharedLibs(t *testing.T) {
3489 bp := `
3490 cc_binary {
3491 name: "bin",
3492 host_supported: true,
3493 shared_libs: ["libshared"],
3494 runtime_libs: ["libruntime"],
3495 srcs: [":gen"],
3496 }
3497
3498 cc_library_shared {
3499 name: "libshared",
3500 host_supported: true,
3501 shared_libs: ["libtransitive"],
3502 }
3503
3504 cc_library_shared {
3505 name: "libtransitive",
3506 host_supported: true,
3507 }
3508
3509 cc_library_shared {
3510 name: "libruntime",
3511 host_supported: true,
3512 }
3513
3514 cc_binary_host {
3515 name: "tool",
3516 srcs: ["foo.cpp"],
3517 }
3518
3519 genrule {
3520 name: "gen",
3521 tools: ["tool"],
3522 out: ["gen.cpp"],
3523 cmd: "$(location tool) $(out)",
3524 }
3525 `
3526
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003527 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003528 ctx := testCcWithConfig(t, config)
3529
3530 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3531 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3532 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3533 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3534 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3535
3536 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3537 t.Errorf("expected host bin dependency %q, got %q", w, g)
3538 }
3539
3540 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3541 t.Errorf("expected host bin dependency %q, got %q", w, g)
3542 }
3543
3544 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3545 t.Errorf("expected host bin dependency %q, got %q", w, g)
3546 }
3547
3548 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3549 t.Errorf("expected host bin dependency %q, got %q", w, g)
3550 }
3551
3552 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3553 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3554 }
3555
3556 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3557 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3558 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3559 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3560
3561 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3562 t.Errorf("expected device bin dependency %q, got %q", w, g)
3563 }
3564
3565 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3566 t.Errorf("expected device bin dependency %q, got %q", w, g)
3567 }
3568
3569 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3570 t.Errorf("expected device bin dependency %q, got %q", w, g)
3571 }
3572
3573 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3574 t.Errorf("expected device bin dependency %q, got %q", w, g)
3575 }
3576
3577 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3578 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3579 }
3580
3581}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003582
3583func TestStubsLibReexportsHeaders(t *testing.T) {
3584 ctx := testCc(t, `
3585 cc_library_shared {
3586 name: "libclient",
3587 srcs: ["foo.c"],
3588 shared_libs: ["libfoo#1"],
3589 }
3590
3591 cc_library_shared {
3592 name: "libfoo",
3593 srcs: ["foo.c"],
3594 shared_libs: ["libbar"],
3595 export_shared_lib_headers: ["libbar"],
3596 stubs: {
3597 symbol_file: "foo.map.txt",
3598 versions: ["1", "2", "3"],
3599 },
3600 }
3601
3602 cc_library_shared {
3603 name: "libbar",
3604 export_include_dirs: ["include/libbar"],
3605 srcs: ["foo.c"],
3606 }`)
3607
3608 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3609
3610 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3611 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3612 }
3613}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003614
3615func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3616 ctx := testCc(t, `
3617 cc_library {
3618 name: "libfoo",
3619 srcs: ["a/Foo.aidl"],
3620 aidl: { flags: ["-Werror"], },
3621 }
3622 `)
3623
3624 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3625 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3626 aidlCommand := manifest.Commands[0].GetCommand()
3627 expectedAidlFlag := "-Werror"
3628 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3629 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3630 }
3631}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003632
Jiyong Parka008fb02021-03-16 17:15:53 +09003633func TestMinSdkVersionInClangTriple(t *testing.T) {
3634 ctx := testCc(t, `
3635 cc_library_shared {
3636 name: "libfoo",
3637 srcs: ["foo.c"],
3638 min_sdk_version: "29",
3639 }`)
3640
3641 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3642 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3643}
3644
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003645func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3646 ctx := testCc(t, `
3647 cc_object {
3648 name: "crt_foo",
3649 srcs: ["foo.c"],
3650 crt: true,
3651 stl: "none",
3652 min_sdk_version: "28",
3653
3654 }`)
3655
3656 arch := "android_arm64_armv8-a"
3657 for _, v := range []string{"", "28", "29", "30", "current"} {
3658 var variant string
3659 if v == "" {
3660 variant = arch
3661 } else {
3662 variant = arch + "_sdk_" + v
3663 }
3664 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3665 vNum := v
3666 if v == "current" || v == "" {
3667 vNum = "10000"
3668 }
3669 expected := "-target aarch64-linux-android" + vNum + " "
3670 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3671 }
3672}
3673
3674func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3675 ctx := testCc(t, `
3676 cc_binary {
3677 name: "bin",
3678 srcs: ["foo.c"],
3679 stl: "none",
3680 min_sdk_version: "29",
3681 sdk_version: "current",
3682 }
3683 `)
3684
3685 // Sdk variant uses the crt object of the matching min_sdk_version
3686 variant := "android_arm64_armv8-a_sdk"
3687 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3688 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3689 variant+"_29/crtbegin_dynamic.o")
3690
3691 // platform variant uses the crt object built for platform
3692 variant = "android_arm64_armv8-a"
3693 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3694 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3695 variant+"/crtbegin_dynamic.o")
3696}
3697
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003698type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003699
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003700const (
3701 None MemtagNoteType = iota + 1
3702 Sync
3703 Async
3704)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003705
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003706func (t MemtagNoteType) str() string {
3707 switch t {
3708 case None:
3709 return "none"
3710 case Sync:
3711 return "sync"
3712 case Async:
3713 return "async"
3714 default:
3715 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003716 }
3717}
3718
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003719func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3720 note_async := "note_memtag_heap_async"
3721 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003722
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003723 found := None
3724 implicits := m.Rule("ld").Implicits
3725 for _, lib := range implicits {
3726 if strings.Contains(lib.Rel(), note_async) {
3727 found = Async
3728 break
3729 } else if strings.Contains(lib.Rel(), note_sync) {
3730 found = Sync
3731 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003732 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003733 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003734
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003735 if found != expected {
3736 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3737 }
3738}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003739
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003740var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3741 android.FixtureModifyMockFS(func(fs android.MockFS) {
3742 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003743 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003744 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003745 gtest: false,
3746 }
3747
3748 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003749 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003750 gtest: false,
3751 sanitize: { memtag_heap: false },
3752 }
3753
3754 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003755 name: "%[1]s_test_true",
3756 gtest: false,
3757 sanitize: { memtag_heap: true },
3758 }
3759
3760 cc_test {
3761 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003762 gtest: false,
3763 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3764 }
3765
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003766 cc_test {
3767 name: "%[1]s_test_true_diag",
3768 gtest: false,
3769 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3770 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003771
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003772 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003773 name: "%[1]s_binary",
3774 }
3775
3776 cc_binary {
3777 name: "%[1]s_binary_false",
3778 sanitize: { memtag_heap: false },
3779 }
3780
3781 cc_binary {
3782 name: "%[1]s_binary_true",
3783 sanitize: { memtag_heap: true },
3784 }
3785
3786 cc_binary {
3787 name: "%[1]s_binary_true_nodiag",
3788 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3789 }
3790
3791 cc_binary {
3792 name: "%[1]s_binary_true_diag",
3793 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003794 }
3795 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003796 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3797 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3798 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3799 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003800
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003801 fs.Merge(android.MockFS{
3802 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3803 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3804 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3805 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3806 })
3807 }),
3808 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3809 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3810 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3811 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3812 }),
3813)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003814
3815func TestSanitizeMemtagHeap(t *testing.T) {
3816 variant := "android_arm64_armv8-a"
3817
Paul Duffin8567f222021-03-23 00:02:06 +00003818 result := android.GroupFixturePreparers(
3819 prepareForCcTest,
3820 prepareForTestWithMemtagHeap,
3821 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003822 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003823
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003824 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3825 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3826 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3827 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3828 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3829
3830 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3833 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3834 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3835
3836 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3839 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3840 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3841
3842 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3845 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3846 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3847
3848 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3851 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3852 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3853
3854 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3857 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3858 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3859
3860 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3862 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3863 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3864 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3865
3866 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3868 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3869 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3870 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3871}
3872
3873func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003874 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003875
Paul Duffin8567f222021-03-23 00:02:06 +00003876 result := android.GroupFixturePreparers(
3877 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003878 prepareForTestWithMemtagHeap,
3879 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3880 variables.SanitizeDevice = []string{"memtag_heap"}
3881 }),
3882 ).RunTest(t)
3883 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003884
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003885 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3886 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3887 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3888 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3889 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003890
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003891 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3893 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3894 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3895 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3896
3897 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3899 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3900 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3901 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3902
3903 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3905 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3906 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3907 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3908
3909 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3911 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3912 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3913 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3914
3915 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3917 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3918 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3919 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3920
3921 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3923 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3924 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3925 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3926
3927 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3929 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3930 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3931 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3932}
3933
3934func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3935 variant := "android_arm64_armv8-a"
3936
Paul Duffin8567f222021-03-23 00:02:06 +00003937 result := android.GroupFixturePreparers(
3938 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003939 prepareForTestWithMemtagHeap,
3940 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3941 variables.SanitizeDevice = []string{"memtag_heap"}
3942 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3943 }),
3944 ).RunTest(t)
3945 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003946
3947 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3949 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3952
3953 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3955 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3956 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3958
3959 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3961 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3964
3965 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3967 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3970
3971 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3973 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3976
3977 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3979 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3982
3983 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3985 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3988
3989 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3991 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3992 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003994}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003995
3996func TestIncludeDirsExporting(t *testing.T) {
3997
3998 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3999 // embedded newline characters alone.
4000 trimIndentingSpaces := func(s string) string {
4001 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4002 }
4003
4004 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4005 t.Helper()
4006 expected = trimIndentingSpaces(expected)
4007 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4008 if expected != actual {
4009 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4010 }
4011 }
4012
4013 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4014
4015 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4016 t.Helper()
4017 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4018 name := module.Name()
4019
4020 for _, checker := range checkers {
4021 checker(t, name, exported)
4022 }
4023 }
4024
4025 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4026 return func(t *testing.T, name string, exported FlagExporterInfo) {
4027 t.Helper()
4028 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4029 }
4030 }
4031
4032 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4033 return func(t *testing.T, name string, exported FlagExporterInfo) {
4034 t.Helper()
4035 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4036 }
4037 }
4038
4039 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4040 return func(t *testing.T, name string, exported FlagExporterInfo) {
4041 t.Helper()
4042 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4043 }
4044 }
4045
4046 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4047 return func(t *testing.T, name string, exported FlagExporterInfo) {
4048 t.Helper()
4049 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4050 }
4051 }
4052
4053 genRuleModules := `
4054 genrule {
4055 name: "genrule_foo",
4056 cmd: "generate-foo",
4057 out: [
4058 "generated_headers/foo/generated_header.h",
4059 ],
4060 export_include_dirs: [
4061 "generated_headers",
4062 ],
4063 }
4064
4065 genrule {
4066 name: "genrule_bar",
4067 cmd: "generate-bar",
4068 out: [
4069 "generated_headers/bar/generated_header.h",
4070 ],
4071 export_include_dirs: [
4072 "generated_headers",
4073 ],
4074 }
4075 `
4076
4077 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4078 ctx := testCc(t, genRuleModules+`
4079 cc_library {
4080 name: "libfoo",
4081 srcs: ["foo.c"],
4082 export_include_dirs: ["foo/standard"],
4083 export_system_include_dirs: ["foo/system"],
4084 generated_headers: ["genrule_foo"],
4085 export_generated_headers: ["genrule_foo"],
4086 }
4087
4088 cc_library {
4089 name: "libbar",
4090 srcs: ["bar.c"],
4091 shared_libs: ["libfoo"],
4092 export_include_dirs: ["bar/standard"],
4093 export_system_include_dirs: ["bar/system"],
4094 generated_headers: ["genrule_bar"],
4095 export_generated_headers: ["genrule_bar"],
4096 }
4097 `)
4098 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4099 checkIncludeDirs(t, ctx, foo,
4100 expectedIncludeDirs(`
4101 foo/standard
4102 .intermediates/genrule_foo/gen/generated_headers
4103 `),
4104 expectedSystemIncludeDirs(`foo/system`),
4105 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4106 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4107 )
4108
4109 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4110 checkIncludeDirs(t, ctx, bar,
4111 expectedIncludeDirs(`
4112 bar/standard
4113 .intermediates/genrule_bar/gen/generated_headers
4114 `),
4115 expectedSystemIncludeDirs(`bar/system`),
4116 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4117 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4118 )
4119 })
4120
4121 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4122 ctx := testCc(t, genRuleModules+`
4123 cc_library {
4124 name: "libfoo",
4125 srcs: ["foo.c"],
4126 export_include_dirs: ["foo/standard"],
4127 export_system_include_dirs: ["foo/system"],
4128 generated_headers: ["genrule_foo"],
4129 export_generated_headers: ["genrule_foo"],
4130 }
4131
4132 cc_library {
4133 name: "libbar",
4134 srcs: ["bar.c"],
4135 whole_static_libs: ["libfoo"],
4136 export_include_dirs: ["bar/standard"],
4137 export_system_include_dirs: ["bar/system"],
4138 generated_headers: ["genrule_bar"],
4139 export_generated_headers: ["genrule_bar"],
4140 }
4141 `)
4142 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4143 checkIncludeDirs(t, ctx, foo,
4144 expectedIncludeDirs(`
4145 foo/standard
4146 .intermediates/genrule_foo/gen/generated_headers
4147 `),
4148 expectedSystemIncludeDirs(`foo/system`),
4149 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4150 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4151 )
4152
4153 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4154 checkIncludeDirs(t, ctx, bar,
4155 expectedIncludeDirs(`
4156 bar/standard
4157 foo/standard
4158 .intermediates/genrule_foo/gen/generated_headers
4159 .intermediates/genrule_bar/gen/generated_headers
4160 `),
4161 expectedSystemIncludeDirs(`
4162 bar/system
4163 foo/system
4164 `),
4165 expectedGeneratedHeaders(`
4166 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4167 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4168 `),
4169 expectedOrderOnlyDeps(`
4170 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4171 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4172 `),
4173 )
4174 })
4175
Paul Duffin3cb603e2021-02-19 13:57:10 +00004176 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4177 ctx := testCc(t, genRuleModules+`
4178 cc_library_shared {
4179 name: "libfoo",
4180 srcs: [
4181 "foo.c",
4182 "b.aidl",
4183 "a.proto",
4184 ],
4185 aidl: {
4186 export_aidl_headers: true,
4187 }
4188 }
4189 `)
4190 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4191 checkIncludeDirs(t, ctx, foo,
4192 expectedIncludeDirs(`
4193 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4194 `),
4195 expectedSystemIncludeDirs(``),
4196 expectedGeneratedHeaders(`
4197 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4198 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4199 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004200 `),
4201 expectedOrderOnlyDeps(`
4202 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4203 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4204 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004205 `),
4206 )
4207 })
4208
Paul Duffin3cb603e2021-02-19 13:57:10 +00004209 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4210 ctx := testCc(t, genRuleModules+`
4211 cc_library_shared {
4212 name: "libfoo",
4213 srcs: [
4214 "foo.c",
4215 "b.aidl",
4216 "a.proto",
4217 ],
4218 proto: {
4219 export_proto_headers: true,
4220 }
4221 }
4222 `)
4223 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4224 checkIncludeDirs(t, ctx, foo,
4225 expectedIncludeDirs(`
4226 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4227 `),
4228 expectedSystemIncludeDirs(``),
4229 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004230 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4231 `),
4232 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004233 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4234 `),
4235 )
4236 })
4237
Paul Duffin33056e82021-02-19 13:49:08 +00004238 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004239 ctx := testCc(t, genRuleModules+`
4240 cc_library_shared {
4241 name: "libfoo",
4242 srcs: [
4243 "foo.c",
4244 "a.sysprop",
4245 "b.aidl",
4246 "a.proto",
4247 ],
4248 }
4249 `)
4250 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4251 checkIncludeDirs(t, ctx, foo,
4252 expectedIncludeDirs(`
4253 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4254 `),
4255 expectedSystemIncludeDirs(``),
4256 expectedGeneratedHeaders(`
4257 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004258 `),
4259 expectedOrderOnlyDeps(`
4260 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4261 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004262 `),
4263 )
4264 })
4265}