blob: 19596c3f279dc1de8c710043e4470b544875efcc [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",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002675 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002676 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2677 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002678 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002679 }
2680 checkEquals(t, "variants for llndk stubs", expected, actual)
2681
Colin Cross127bb8b2020-12-16 16:46:01 -08002682 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002683 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2684
Colin Cross127bb8b2020-12-16 16:46:01 -08002685 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002686 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2687}
2688
Jiyong Parka46a4d52017-12-14 19:54:34 +09002689func TestLlndkHeaders(t *testing.T) {
2690 ctx := testCc(t, `
2691 llndk_headers {
2692 name: "libllndk_headers",
2693 export_include_dirs: ["my_include"],
2694 }
2695 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002696 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002697 export_llndk_headers: ["libllndk_headers"],
2698 }
2699 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002700 name: "libllndk",
2701 llndk_stubs: "libllndk.llndk",
2702 }
2703
2704 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002705 name: "libvendor",
2706 shared_libs: ["libllndk"],
2707 vendor: true,
2708 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002709 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002710 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002711 }
2712 `)
2713
2714 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002715 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002716 cflags := cc.Args["cFlags"]
2717 if !strings.Contains(cflags, "-Imy_include") {
2718 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2719 }
2720}
2721
Logan Chien43d34c32017-12-20 01:17:32 +08002722func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2723 actual := module.Properties.AndroidMkRuntimeLibs
2724 if !reflect.DeepEqual(actual, expected) {
2725 t.Errorf("incorrect runtime_libs for shared libs"+
2726 "\nactual: %v"+
2727 "\nexpected: %v",
2728 actual,
2729 expected,
2730 )
2731 }
2732}
2733
2734const runtimeLibAndroidBp = `
2735 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002736 name: "liball_available",
2737 vendor_available: true,
2738 product_available: true,
2739 no_libcrt : true,
2740 nocrt : true,
2741 system_shared_libs : [],
2742 }
2743 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002744 name: "libvendor_available1",
2745 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002746 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002747 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002748 nocrt : true,
2749 system_shared_libs : [],
2750 }
2751 cc_library {
2752 name: "libvendor_available2",
2753 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002754 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002755 target: {
2756 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002757 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002758 }
2759 },
Yi Konge7fe9912019-06-02 00:53:50 -07002760 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002761 nocrt : true,
2762 system_shared_libs : [],
2763 }
2764 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002765 name: "libproduct_vendor",
2766 product_specific: true,
2767 vendor_available: true,
2768 no_libcrt : true,
2769 nocrt : true,
2770 system_shared_libs : [],
2771 }
2772 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002773 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002774 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002775 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002776 nocrt : true,
2777 system_shared_libs : [],
2778 }
2779 cc_library {
2780 name: "libvendor1",
2781 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002782 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002783 nocrt : true,
2784 system_shared_libs : [],
2785 }
2786 cc_library {
2787 name: "libvendor2",
2788 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002789 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002790 no_libcrt : true,
2791 nocrt : true,
2792 system_shared_libs : [],
2793 }
2794 cc_library {
2795 name: "libproduct_available1",
2796 product_available: true,
2797 runtime_libs: ["liball_available"],
2798 no_libcrt : true,
2799 nocrt : true,
2800 system_shared_libs : [],
2801 }
2802 cc_library {
2803 name: "libproduct1",
2804 product_specific: true,
2805 no_libcrt : true,
2806 nocrt : true,
2807 system_shared_libs : [],
2808 }
2809 cc_library {
2810 name: "libproduct2",
2811 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002812 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002813 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002814 nocrt : true,
2815 system_shared_libs : [],
2816 }
2817`
2818
2819func TestRuntimeLibs(t *testing.T) {
2820 ctx := testCc(t, runtimeLibAndroidBp)
2821
2822 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002823 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002824
Justin Yun8a2600c2020-12-07 12:44:03 +09002825 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2826 checkRuntimeLibs(t, []string{"liball_available"}, module)
2827
2828 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2829 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002830
2831 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002832 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002833
2834 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2835 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002836 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002837
Justin Yun8a2600c2020-12-07 12:44:03 +09002838 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2839 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002840
2841 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002842 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002843
2844 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2845 // and product variants.
2846 variant = "android_product.VER_arm64_armv8-a_shared"
2847
2848 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2849 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2850
2851 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002852 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002853}
2854
2855func TestExcludeRuntimeLibs(t *testing.T) {
2856 ctx := testCc(t, runtimeLibAndroidBp)
2857
Colin Cross7113d202019-11-20 16:39:12 -08002858 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002859 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2860 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002861
Colin Crossfb0c16e2019-11-20 17:12:35 -08002862 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002863 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002864 checkRuntimeLibs(t, nil, module)
2865}
2866
2867func TestRuntimeLibsNoVndk(t *testing.T) {
2868 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2869
2870 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2871
Colin Cross7113d202019-11-20 16:39:12 -08002872 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002873
Justin Yun8a2600c2020-12-07 12:44:03 +09002874 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2875 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002876
2877 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002878 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002879
2880 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002881 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002882}
2883
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002884func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002885 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002886 actual := module.Properties.AndroidMkStaticLibs
2887 if !reflect.DeepEqual(actual, expected) {
2888 t.Errorf("incorrect static_libs"+
2889 "\nactual: %v"+
2890 "\nexpected: %v",
2891 actual,
2892 expected,
2893 )
2894 }
2895}
2896
2897const staticLibAndroidBp = `
2898 cc_library {
2899 name: "lib1",
2900 }
2901 cc_library {
2902 name: "lib2",
2903 static_libs: ["lib1"],
2904 }
2905`
2906
2907func TestStaticLibDepExport(t *testing.T) {
2908 ctx := testCc(t, staticLibAndroidBp)
2909
2910 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002911 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002912 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002913 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002914
2915 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002916 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002917 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2918 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002919 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002920}
2921
Jiyong Parkd08b6972017-09-26 10:50:54 +09002922var compilerFlagsTestCases = []struct {
2923 in string
2924 out bool
2925}{
2926 {
2927 in: "a",
2928 out: false,
2929 },
2930 {
2931 in: "-a",
2932 out: true,
2933 },
2934 {
2935 in: "-Ipath/to/something",
2936 out: false,
2937 },
2938 {
2939 in: "-isystempath/to/something",
2940 out: false,
2941 },
2942 {
2943 in: "--coverage",
2944 out: false,
2945 },
2946 {
2947 in: "-include a/b",
2948 out: true,
2949 },
2950 {
2951 in: "-include a/b c/d",
2952 out: false,
2953 },
2954 {
2955 in: "-DMACRO",
2956 out: true,
2957 },
2958 {
2959 in: "-DMAC RO",
2960 out: false,
2961 },
2962 {
2963 in: "-a -b",
2964 out: false,
2965 },
2966 {
2967 in: "-DMACRO=definition",
2968 out: true,
2969 },
2970 {
2971 in: "-DMACRO=defi nition",
2972 out: true, // TODO(jiyong): this should be false
2973 },
2974 {
2975 in: "-DMACRO(x)=x + 1",
2976 out: true,
2977 },
2978 {
2979 in: "-DMACRO=\"defi nition\"",
2980 out: true,
2981 },
2982}
2983
2984type mockContext struct {
2985 BaseModuleContext
2986 result bool
2987}
2988
2989func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2990 // CheckBadCompilerFlags calls this function when the flag should be rejected
2991 ctx.result = false
2992}
2993
2994func TestCompilerFlags(t *testing.T) {
2995 for _, testCase := range compilerFlagsTestCases {
2996 ctx := &mockContext{result: true}
2997 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2998 if ctx.result != testCase.out {
2999 t.Errorf("incorrect output:")
3000 t.Errorf(" input: %#v", testCase.in)
3001 t.Errorf(" expected: %#v", testCase.out)
3002 t.Errorf(" got: %#v", ctx.result)
3003 }
3004 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003005}
Jiyong Park374510b2018-03-19 18:23:01 +09003006
3007func TestVendorPublicLibraries(t *testing.T) {
3008 ctx := testCc(t, `
3009 cc_library_headers {
3010 name: "libvendorpublic_headers",
3011 export_include_dirs: ["my_include"],
3012 }
3013 vendor_public_library {
3014 name: "libvendorpublic",
3015 symbol_file: "",
3016 export_public_headers: ["libvendorpublic_headers"],
3017 }
3018 cc_library {
3019 name: "libvendorpublic",
3020 srcs: ["foo.c"],
3021 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003022 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003023 nocrt: true,
3024 }
3025
3026 cc_library {
3027 name: "libsystem",
3028 shared_libs: ["libvendorpublic"],
3029 vendor: false,
3030 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003031 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003032 nocrt: true,
3033 }
3034 cc_library {
3035 name: "libvendor",
3036 shared_libs: ["libvendorpublic"],
3037 vendor: true,
3038 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003039 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003040 nocrt: true,
3041 }
3042 `)
3043
Colin Cross7113d202019-11-20 16:39:12 -08003044 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003045 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003046
3047 // test if header search paths are correctly added
3048 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003049 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003050 cflags := cc.Args["cFlags"]
3051 if !strings.Contains(cflags, "-Imy_include") {
3052 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3053 }
3054
3055 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003056 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003057 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003058 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003059 if !strings.Contains(libflags, stubPaths[0].String()) {
3060 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3061 }
3062
3063 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003064 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003065 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003066 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003067 if !strings.Contains(libflags, stubPaths[0].String()) {
3068 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3069 }
3070
3071}
Jiyong Park37b25202018-07-11 10:49:27 +09003072
3073func TestRecovery(t *testing.T) {
3074 ctx := testCc(t, `
3075 cc_library_shared {
3076 name: "librecovery",
3077 recovery: true,
3078 }
3079 cc_library_shared {
3080 name: "librecovery32",
3081 recovery: true,
3082 compile_multilib:"32",
3083 }
Jiyong Park5baac542018-08-28 09:55:37 +09003084 cc_library_shared {
3085 name: "libHalInRecovery",
3086 recovery_available: true,
3087 vendor: true,
3088 }
Jiyong Park37b25202018-07-11 10:49:27 +09003089 `)
3090
3091 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003092 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003093 if len(variants) != 1 || !android.InList(arm64, variants) {
3094 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3095 }
3096
3097 variants = ctx.ModuleVariantsForTests("librecovery32")
3098 if android.InList(arm64, variants) {
3099 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3100 }
Jiyong Park5baac542018-08-28 09:55:37 +09003101
3102 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3103 if !recoveryModule.Platform() {
3104 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3105 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003106}
Jiyong Park5baac542018-08-28 09:55:37 +09003107
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003108func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3109 bp := `
3110 cc_prebuilt_test_library_shared {
3111 name: "test_lib",
3112 relative_install_path: "foo/bar/baz",
3113 srcs: ["srcpath/dontusethispath/baz.so"],
3114 }
3115
3116 cc_test {
3117 name: "main_test",
3118 data_libs: ["test_lib"],
3119 gtest: false,
3120 }
3121 `
3122
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003123 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003124 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3125 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3126 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3127
3128 ctx := testCcWithConfig(t, config)
3129 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3130 testBinary := module.(*Module).linker.(*testBinary)
3131 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3132 if err != nil {
3133 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3134 }
3135 if len(outputFiles) != 1 {
3136 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3137 }
3138 if len(testBinary.dataPaths()) != 1 {
3139 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3140 }
3141
3142 outputPath := outputFiles[0].String()
3143
3144 if !strings.HasSuffix(outputPath, "/main_test") {
3145 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3146 }
Colin Crossaa255532020-07-03 13:18:24 -07003147 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003148 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3149 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3150 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3151 }
3152}
3153
Jiyong Park7ed9de32018-10-15 22:25:07 +09003154func TestVersionedStubs(t *testing.T) {
3155 ctx := testCc(t, `
3156 cc_library_shared {
3157 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003158 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003159 stubs: {
3160 symbol_file: "foo.map.txt",
3161 versions: ["1", "2", "3"],
3162 },
3163 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003164
Jiyong Park7ed9de32018-10-15 22:25:07 +09003165 cc_library_shared {
3166 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003167 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003168 shared_libs: ["libFoo#1"],
3169 }`)
3170
3171 variants := ctx.ModuleVariantsForTests("libFoo")
3172 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003173 "android_arm64_armv8-a_shared",
3174 "android_arm64_armv8-a_shared_1",
3175 "android_arm64_armv8-a_shared_2",
3176 "android_arm64_armv8-a_shared_3",
3177 "android_arm_armv7-a-neon_shared",
3178 "android_arm_armv7-a-neon_shared_1",
3179 "android_arm_armv7-a-neon_shared_2",
3180 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003181 }
3182 variantsMismatch := false
3183 if len(variants) != len(expectedVariants) {
3184 variantsMismatch = true
3185 } else {
3186 for _, v := range expectedVariants {
3187 if !inList(v, variants) {
3188 variantsMismatch = false
3189 }
3190 }
3191 }
3192 if variantsMismatch {
3193 t.Errorf("variants of libFoo expected:\n")
3194 for _, v := range expectedVariants {
3195 t.Errorf("%q\n", v)
3196 }
3197 t.Errorf(", but got:\n")
3198 for _, v := range variants {
3199 t.Errorf("%q\n", v)
3200 }
3201 }
3202
Colin Cross7113d202019-11-20 16:39:12 -08003203 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003204 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003205 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003206 if !strings.Contains(libFlags, libFoo1StubPath) {
3207 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3208 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003209
Colin Cross7113d202019-11-20 16:39:12 -08003210 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003211 cFlags := libBarCompileRule.Args["cFlags"]
3212 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3213 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3214 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3215 }
Jiyong Park37b25202018-07-11 10:49:27 +09003216}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003217
Jooyung Hanb04a4992020-03-13 18:57:35 +09003218func TestVersioningMacro(t *testing.T) {
3219 for _, tc := range []struct{ moduleName, expected string }{
3220 {"libc", "__LIBC_API__"},
3221 {"libfoo", "__LIBFOO_API__"},
3222 {"libfoo@1", "__LIBFOO_1_API__"},
3223 {"libfoo-v1", "__LIBFOO_V1_API__"},
3224 {"libfoo.v1", "__LIBFOO_V1_API__"},
3225 } {
3226 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3227 }
3228}
3229
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003230func TestStaticExecutable(t *testing.T) {
3231 ctx := testCc(t, `
3232 cc_binary {
3233 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003234 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003235 static_executable: true,
3236 }`)
3237
Colin Cross7113d202019-11-20 16:39:12 -08003238 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003239 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3240 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003241 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003242 for _, lib := range systemStaticLibs {
3243 if !strings.Contains(libFlags, lib) {
3244 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3245 }
3246 }
3247 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3248 for _, lib := range systemSharedLibs {
3249 if strings.Contains(libFlags, lib) {
3250 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3251 }
3252 }
3253}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003254
3255func TestStaticDepsOrderWithStubs(t *testing.T) {
3256 ctx := testCc(t, `
3257 cc_binary {
3258 name: "mybin",
3259 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003260 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003261 static_executable: true,
3262 stl: "none",
3263 }
3264
3265 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003266 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003267 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003268 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003269 stl: "none",
3270 }
3271
3272 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003273 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003274 srcs: ["foo.c"],
3275 stl: "none",
3276 stubs: {
3277 versions: ["1"],
3278 },
3279 }`)
3280
Colin Cross0de8a1e2020-09-18 14:15:30 -07003281 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3282 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003283 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003284
3285 if !reflect.DeepEqual(actual, expected) {
3286 t.Errorf("staticDeps orderings were not propagated correctly"+
3287 "\nactual: %v"+
3288 "\nexpected: %v",
3289 actual,
3290 expected,
3291 )
3292 }
3293}
Jooyung Han38002912019-05-16 04:01:54 +09003294
Jooyung Hand48f3c32019-08-23 11:18:57 +09003295func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3296 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3297 cc_library {
3298 name: "libA",
3299 srcs: ["foo.c"],
3300 shared_libs: ["libB"],
3301 stl: "none",
3302 }
3303
3304 cc_library {
3305 name: "libB",
3306 srcs: ["foo.c"],
3307 enabled: false,
3308 stl: "none",
3309 }
3310 `)
3311}
3312
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003313// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3314// correctly.
3315func TestFuzzTarget(t *testing.T) {
3316 ctx := testCc(t, `
3317 cc_fuzz {
3318 name: "fuzz_smoke_test",
3319 srcs: ["foo.c"],
3320 }`)
3321
Paul Duffin075c4172019-12-19 19:06:13 +00003322 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003323 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3324}
3325
Jiyong Park29074592019-07-07 16:27:47 +09003326func TestAidl(t *testing.T) {
3327}
3328
Jooyung Han38002912019-05-16 04:01:54 +09003329func assertString(t *testing.T, got, expected string) {
3330 t.Helper()
3331 if got != expected {
3332 t.Errorf("expected %q got %q", expected, got)
3333 }
3334}
3335
3336func assertArrayString(t *testing.T, got, expected []string) {
3337 t.Helper()
3338 if len(got) != len(expected) {
3339 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3340 return
3341 }
3342 for i := range got {
3343 if got[i] != expected[i] {
3344 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3345 i, expected[i], expected, got[i], got)
3346 return
3347 }
3348 }
3349}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003350
Jooyung Han0302a842019-10-30 18:43:49 +09003351func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3352 t.Helper()
3353 assertArrayString(t, android.SortedStringKeys(m), expected)
3354}
3355
Colin Crosse1bb5d02019-09-24 14:55:04 -07003356func TestDefaults(t *testing.T) {
3357 ctx := testCc(t, `
3358 cc_defaults {
3359 name: "defaults",
3360 srcs: ["foo.c"],
3361 static: {
3362 srcs: ["bar.c"],
3363 },
3364 shared: {
3365 srcs: ["baz.c"],
3366 },
3367 }
3368
3369 cc_library_static {
3370 name: "libstatic",
3371 defaults: ["defaults"],
3372 }
3373
3374 cc_library_shared {
3375 name: "libshared",
3376 defaults: ["defaults"],
3377 }
3378
3379 cc_library {
3380 name: "libboth",
3381 defaults: ["defaults"],
3382 }
3383
3384 cc_binary {
3385 name: "binary",
3386 defaults: ["defaults"],
3387 }`)
3388
3389 pathsToBase := func(paths android.Paths) []string {
3390 var ret []string
3391 for _, p := range paths {
3392 ret = append(ret, p.Base())
3393 }
3394 return ret
3395 }
3396
Colin Cross7113d202019-11-20 16:39:12 -08003397 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003398 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3399 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3400 }
Colin Cross7113d202019-11-20 16:39:12 -08003401 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003402 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3403 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3404 }
Colin Cross7113d202019-11-20 16:39:12 -08003405 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003406 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3407 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3408 }
3409
Colin Cross7113d202019-11-20 16:39:12 -08003410 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003411 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3412 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3413 }
Colin Cross7113d202019-11-20 16:39:12 -08003414 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003415 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3416 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3417 }
3418}
Colin Crosseabaedd2020-02-06 17:01:55 -08003419
3420func TestProductVariableDefaults(t *testing.T) {
3421 bp := `
3422 cc_defaults {
3423 name: "libfoo_defaults",
3424 srcs: ["foo.c"],
3425 cppflags: ["-DFOO"],
3426 product_variables: {
3427 debuggable: {
3428 cppflags: ["-DBAR"],
3429 },
3430 },
3431 }
3432
3433 cc_library {
3434 name: "libfoo",
3435 defaults: ["libfoo_defaults"],
3436 }
3437 `
3438
Paul Duffin8567f222021-03-23 00:02:06 +00003439 result := android.GroupFixturePreparers(
3440 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003441 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003442
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003443 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3444 variables.Debuggable = BoolPtr(true)
3445 }),
3446 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003447
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003448 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003449 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003450}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003451
3452func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3453 t.Parallel()
3454 bp := `
3455 cc_library_static {
3456 name: "libfoo",
3457 srcs: ["foo.c"],
3458 whole_static_libs: ["libbar"],
3459 }
3460
3461 cc_library_static {
3462 name: "libbar",
3463 whole_static_libs: ["libmissing"],
3464 }
3465 `
3466
Paul Duffin8567f222021-03-23 00:02:06 +00003467 result := android.GroupFixturePreparers(
3468 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003469 android.PrepareForTestWithAllowMissingDependencies,
3470 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003471
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003472 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003473 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003474
Paul Duffine84b1332021-03-12 11:59:43 +00003475 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003476
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003477 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003478 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003479}
Colin Crosse9fe2942020-11-10 18:12:15 -08003480
3481func TestInstallSharedLibs(t *testing.T) {
3482 bp := `
3483 cc_binary {
3484 name: "bin",
3485 host_supported: true,
3486 shared_libs: ["libshared"],
3487 runtime_libs: ["libruntime"],
3488 srcs: [":gen"],
3489 }
3490
3491 cc_library_shared {
3492 name: "libshared",
3493 host_supported: true,
3494 shared_libs: ["libtransitive"],
3495 }
3496
3497 cc_library_shared {
3498 name: "libtransitive",
3499 host_supported: true,
3500 }
3501
3502 cc_library_shared {
3503 name: "libruntime",
3504 host_supported: true,
3505 }
3506
3507 cc_binary_host {
3508 name: "tool",
3509 srcs: ["foo.cpp"],
3510 }
3511
3512 genrule {
3513 name: "gen",
3514 tools: ["tool"],
3515 out: ["gen.cpp"],
3516 cmd: "$(location tool) $(out)",
3517 }
3518 `
3519
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003520 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003521 ctx := testCcWithConfig(t, config)
3522
3523 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3524 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3525 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3526 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3527 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3528
3529 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3530 t.Errorf("expected host bin dependency %q, got %q", w, g)
3531 }
3532
3533 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3534 t.Errorf("expected host bin dependency %q, got %q", w, g)
3535 }
3536
3537 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3538 t.Errorf("expected host bin dependency %q, got %q", w, g)
3539 }
3540
3541 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3542 t.Errorf("expected host bin dependency %q, got %q", w, g)
3543 }
3544
3545 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3546 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3547 }
3548
3549 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3550 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3551 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3552 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3553
3554 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3555 t.Errorf("expected device bin dependency %q, got %q", w, g)
3556 }
3557
3558 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3559 t.Errorf("expected device bin dependency %q, got %q", w, g)
3560 }
3561
3562 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3563 t.Errorf("expected device bin dependency %q, got %q", w, g)
3564 }
3565
3566 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3567 t.Errorf("expected device bin dependency %q, got %q", w, g)
3568 }
3569
3570 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3571 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3572 }
3573
3574}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003575
3576func TestStubsLibReexportsHeaders(t *testing.T) {
3577 ctx := testCc(t, `
3578 cc_library_shared {
3579 name: "libclient",
3580 srcs: ["foo.c"],
3581 shared_libs: ["libfoo#1"],
3582 }
3583
3584 cc_library_shared {
3585 name: "libfoo",
3586 srcs: ["foo.c"],
3587 shared_libs: ["libbar"],
3588 export_shared_lib_headers: ["libbar"],
3589 stubs: {
3590 symbol_file: "foo.map.txt",
3591 versions: ["1", "2", "3"],
3592 },
3593 }
3594
3595 cc_library_shared {
3596 name: "libbar",
3597 export_include_dirs: ["include/libbar"],
3598 srcs: ["foo.c"],
3599 }`)
3600
3601 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3602
3603 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3604 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3605 }
3606}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003607
3608func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3609 ctx := testCc(t, `
3610 cc_library {
3611 name: "libfoo",
3612 srcs: ["a/Foo.aidl"],
3613 aidl: { flags: ["-Werror"], },
3614 }
3615 `)
3616
3617 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3618 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3619 aidlCommand := manifest.Commands[0].GetCommand()
3620 expectedAidlFlag := "-Werror"
3621 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3622 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3623 }
3624}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003625
Jiyong Parka008fb02021-03-16 17:15:53 +09003626func TestMinSdkVersionInClangTriple(t *testing.T) {
3627 ctx := testCc(t, `
3628 cc_library_shared {
3629 name: "libfoo",
3630 srcs: ["foo.c"],
3631 min_sdk_version: "29",
3632 }`)
3633
3634 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3635 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3636}
3637
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003638func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3639 ctx := testCc(t, `
3640 cc_object {
3641 name: "crt_foo",
3642 srcs: ["foo.c"],
3643 crt: true,
3644 stl: "none",
3645 min_sdk_version: "28",
3646
3647 }`)
3648
3649 arch := "android_arm64_armv8-a"
3650 for _, v := range []string{"", "28", "29", "30", "current"} {
3651 var variant string
3652 if v == "" {
3653 variant = arch
3654 } else {
3655 variant = arch + "_sdk_" + v
3656 }
3657 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3658 vNum := v
3659 if v == "current" || v == "" {
3660 vNum = "10000"
3661 }
3662 expected := "-target aarch64-linux-android" + vNum + " "
3663 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3664 }
3665}
3666
3667func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3668 ctx := testCc(t, `
3669 cc_binary {
3670 name: "bin",
3671 srcs: ["foo.c"],
3672 stl: "none",
3673 min_sdk_version: "29",
3674 sdk_version: "current",
3675 }
3676 `)
3677
3678 // Sdk variant uses the crt object of the matching min_sdk_version
3679 variant := "android_arm64_armv8-a_sdk"
3680 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3681 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3682 variant+"_29/crtbegin_dynamic.o")
3683
3684 // platform variant uses the crt object built for platform
3685 variant = "android_arm64_armv8-a"
3686 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3687 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3688 variant+"/crtbegin_dynamic.o")
3689}
3690
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003691type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003692
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003693const (
3694 None MemtagNoteType = iota + 1
3695 Sync
3696 Async
3697)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003698
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003699func (t MemtagNoteType) str() string {
3700 switch t {
3701 case None:
3702 return "none"
3703 case Sync:
3704 return "sync"
3705 case Async:
3706 return "async"
3707 default:
3708 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003709 }
3710}
3711
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003712func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3713 note_async := "note_memtag_heap_async"
3714 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003715
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003716 found := None
3717 implicits := m.Rule("ld").Implicits
3718 for _, lib := range implicits {
3719 if strings.Contains(lib.Rel(), note_async) {
3720 found = Async
3721 break
3722 } else if strings.Contains(lib.Rel(), note_sync) {
3723 found = Sync
3724 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003725 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003726 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003727
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003728 if found != expected {
3729 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3730 }
3731}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003732
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003733var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3734 android.FixtureModifyMockFS(func(fs android.MockFS) {
3735 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003736 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003737 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003738 gtest: false,
3739 }
3740
3741 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003742 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003743 gtest: false,
3744 sanitize: { memtag_heap: false },
3745 }
3746
3747 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003748 name: "%[1]s_test_true",
3749 gtest: false,
3750 sanitize: { memtag_heap: true },
3751 }
3752
3753 cc_test {
3754 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003755 gtest: false,
3756 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3757 }
3758
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003759 cc_test {
3760 name: "%[1]s_test_true_diag",
3761 gtest: false,
3762 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3763 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003764
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003765 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003766 name: "%[1]s_binary",
3767 }
3768
3769 cc_binary {
3770 name: "%[1]s_binary_false",
3771 sanitize: { memtag_heap: false },
3772 }
3773
3774 cc_binary {
3775 name: "%[1]s_binary_true",
3776 sanitize: { memtag_heap: true },
3777 }
3778
3779 cc_binary {
3780 name: "%[1]s_binary_true_nodiag",
3781 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3782 }
3783
3784 cc_binary {
3785 name: "%[1]s_binary_true_diag",
3786 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003787 }
3788 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003789 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3790 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3791 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3792 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003793
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003794 fs.Merge(android.MockFS{
3795 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3796 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3797 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3798 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3799 })
3800 }),
3801 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3802 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3803 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3804 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3805 }),
3806)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003807
3808func TestSanitizeMemtagHeap(t *testing.T) {
3809 variant := "android_arm64_armv8-a"
3810
Paul Duffin8567f222021-03-23 00:02:06 +00003811 result := android.GroupFixturePreparers(
3812 prepareForCcTest,
3813 prepareForTestWithMemtagHeap,
3814 ).RunTest(t)
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003815 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003816
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003817 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3818 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3819 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3820 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3821 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3822
3823 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3824 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3825 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3826 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3827 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3828
3829 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3830 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3833 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3834
3835 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3839 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3840
3841 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3845 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3846
3847 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3851 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3852
3853 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3857 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3858
3859 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3862 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3863 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3864}
3865
3866func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003867 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003868
Paul Duffin8567f222021-03-23 00:02:06 +00003869 result := android.GroupFixturePreparers(
3870 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003871 prepareForTestWithMemtagHeap,
3872 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3873 variables.SanitizeDevice = []string{"memtag_heap"}
3874 }),
3875 ).RunTest(t)
3876 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003877
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003878 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3879 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3880 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3881 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3882 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003883
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003884 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3885 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3886 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3887 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3888 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3889
3890 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3893 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3894 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3895
3896 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3899 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3900 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3901
3902 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3905 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3906 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3907
3908 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3911 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3912 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3913
3914 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3917 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3918 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3919
3920 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3923 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3924 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3925}
3926
3927func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3928 variant := "android_arm64_armv8-a"
3929
Paul Duffin8567f222021-03-23 00:02:06 +00003930 result := android.GroupFixturePreparers(
3931 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003932 prepareForTestWithMemtagHeap,
3933 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3934 variables.SanitizeDevice = []string{"memtag_heap"}
3935 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3936 }),
3937 ).RunTest(t)
3938 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003939
3940 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3941 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3942 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3943 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3944 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3945
3946 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3947 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3949 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3951
3952 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3955 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3956 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3957
3958 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3961 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3963
3964 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3967 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3969
3970 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3973 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3975
3976 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3979 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3981
3982 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3985 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003987}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003988
3989func TestIncludeDirsExporting(t *testing.T) {
3990
3991 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3992 // embedded newline characters alone.
3993 trimIndentingSpaces := func(s string) string {
3994 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3995 }
3996
3997 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
3998 t.Helper()
3999 expected = trimIndentingSpaces(expected)
4000 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4001 if expected != actual {
4002 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4003 }
4004 }
4005
4006 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4007
4008 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4009 t.Helper()
4010 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4011 name := module.Name()
4012
4013 for _, checker := range checkers {
4014 checker(t, name, exported)
4015 }
4016 }
4017
4018 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4019 return func(t *testing.T, name string, exported FlagExporterInfo) {
4020 t.Helper()
4021 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4022 }
4023 }
4024
4025 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4026 return func(t *testing.T, name string, exported FlagExporterInfo) {
4027 t.Helper()
4028 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4029 }
4030 }
4031
4032 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4033 return func(t *testing.T, name string, exported FlagExporterInfo) {
4034 t.Helper()
4035 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4036 }
4037 }
4038
4039 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4040 return func(t *testing.T, name string, exported FlagExporterInfo) {
4041 t.Helper()
4042 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4043 }
4044 }
4045
4046 genRuleModules := `
4047 genrule {
4048 name: "genrule_foo",
4049 cmd: "generate-foo",
4050 out: [
4051 "generated_headers/foo/generated_header.h",
4052 ],
4053 export_include_dirs: [
4054 "generated_headers",
4055 ],
4056 }
4057
4058 genrule {
4059 name: "genrule_bar",
4060 cmd: "generate-bar",
4061 out: [
4062 "generated_headers/bar/generated_header.h",
4063 ],
4064 export_include_dirs: [
4065 "generated_headers",
4066 ],
4067 }
4068 `
4069
4070 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4071 ctx := testCc(t, genRuleModules+`
4072 cc_library {
4073 name: "libfoo",
4074 srcs: ["foo.c"],
4075 export_include_dirs: ["foo/standard"],
4076 export_system_include_dirs: ["foo/system"],
4077 generated_headers: ["genrule_foo"],
4078 export_generated_headers: ["genrule_foo"],
4079 }
4080
4081 cc_library {
4082 name: "libbar",
4083 srcs: ["bar.c"],
4084 shared_libs: ["libfoo"],
4085 export_include_dirs: ["bar/standard"],
4086 export_system_include_dirs: ["bar/system"],
4087 generated_headers: ["genrule_bar"],
4088 export_generated_headers: ["genrule_bar"],
4089 }
4090 `)
4091 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4092 checkIncludeDirs(t, ctx, foo,
4093 expectedIncludeDirs(`
4094 foo/standard
4095 .intermediates/genrule_foo/gen/generated_headers
4096 `),
4097 expectedSystemIncludeDirs(`foo/system`),
4098 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4099 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4100 )
4101
4102 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4103 checkIncludeDirs(t, ctx, bar,
4104 expectedIncludeDirs(`
4105 bar/standard
4106 .intermediates/genrule_bar/gen/generated_headers
4107 `),
4108 expectedSystemIncludeDirs(`bar/system`),
4109 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4110 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4111 )
4112 })
4113
4114 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4115 ctx := testCc(t, genRuleModules+`
4116 cc_library {
4117 name: "libfoo",
4118 srcs: ["foo.c"],
4119 export_include_dirs: ["foo/standard"],
4120 export_system_include_dirs: ["foo/system"],
4121 generated_headers: ["genrule_foo"],
4122 export_generated_headers: ["genrule_foo"],
4123 }
4124
4125 cc_library {
4126 name: "libbar",
4127 srcs: ["bar.c"],
4128 whole_static_libs: ["libfoo"],
4129 export_include_dirs: ["bar/standard"],
4130 export_system_include_dirs: ["bar/system"],
4131 generated_headers: ["genrule_bar"],
4132 export_generated_headers: ["genrule_bar"],
4133 }
4134 `)
4135 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4136 checkIncludeDirs(t, ctx, foo,
4137 expectedIncludeDirs(`
4138 foo/standard
4139 .intermediates/genrule_foo/gen/generated_headers
4140 `),
4141 expectedSystemIncludeDirs(`foo/system`),
4142 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4143 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4144 )
4145
4146 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4147 checkIncludeDirs(t, ctx, bar,
4148 expectedIncludeDirs(`
4149 bar/standard
4150 foo/standard
4151 .intermediates/genrule_foo/gen/generated_headers
4152 .intermediates/genrule_bar/gen/generated_headers
4153 `),
4154 expectedSystemIncludeDirs(`
4155 bar/system
4156 foo/system
4157 `),
4158 expectedGeneratedHeaders(`
4159 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4160 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4161 `),
4162 expectedOrderOnlyDeps(`
4163 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4164 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4165 `),
4166 )
4167 })
4168
Paul Duffin3cb603e2021-02-19 13:57:10 +00004169 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4170 ctx := testCc(t, genRuleModules+`
4171 cc_library_shared {
4172 name: "libfoo",
4173 srcs: [
4174 "foo.c",
4175 "b.aidl",
4176 "a.proto",
4177 ],
4178 aidl: {
4179 export_aidl_headers: true,
4180 }
4181 }
4182 `)
4183 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4184 checkIncludeDirs(t, ctx, foo,
4185 expectedIncludeDirs(`
4186 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4187 `),
4188 expectedSystemIncludeDirs(``),
4189 expectedGeneratedHeaders(`
4190 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4191 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4192 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004193 `),
4194 expectedOrderOnlyDeps(`
4195 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4196 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4197 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004198 `),
4199 )
4200 })
4201
Paul Duffin3cb603e2021-02-19 13:57:10 +00004202 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4203 ctx := testCc(t, genRuleModules+`
4204 cc_library_shared {
4205 name: "libfoo",
4206 srcs: [
4207 "foo.c",
4208 "b.aidl",
4209 "a.proto",
4210 ],
4211 proto: {
4212 export_proto_headers: true,
4213 }
4214 }
4215 `)
4216 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4217 checkIncludeDirs(t, ctx, foo,
4218 expectedIncludeDirs(`
4219 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4220 `),
4221 expectedSystemIncludeDirs(``),
4222 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004223 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4224 `),
4225 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004226 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4227 `),
4228 )
4229 })
4230
Paul Duffin33056e82021-02-19 13:49:08 +00004231 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004232 ctx := testCc(t, genRuleModules+`
4233 cc_library_shared {
4234 name: "libfoo",
4235 srcs: [
4236 "foo.c",
4237 "a.sysprop",
4238 "b.aidl",
4239 "a.proto",
4240 ],
4241 }
4242 `)
4243 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4244 checkIncludeDirs(t, ctx, foo,
4245 expectedIncludeDirs(`
4246 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4247 `),
4248 expectedSystemIncludeDirs(``),
4249 expectedGeneratedHeaders(`
4250 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004251 `),
4252 expectedOrderOnlyDeps(`
4253 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4254 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004255 `),
4256 )
4257 })
4258}