blob: 205c71fa8d3d6c0c554b9da488540d69d519224f [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 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000023 "regexp"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_cc_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
54
Paul Duffin02a3d652021-02-24 18:51:54 +000055var ccFixtureFactory = android.NewFixtureFactory(
56 &buildDir,
57 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000058 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
59 variables.DeviceVndkVersion = StringPtr("current")
60 variables.ProductVndkVersion = StringPtr("current")
61 variables.Platform_vndk_version = StringPtr("VER")
62 }),
63)
64
65// testCcWithConfig runs tests using the ccFixtureFactory
66//
67// See testCc for an explanation as to how to stop using this deprecated method.
68//
69// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080070func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070071 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000072 result := ccFixtureFactory.RunTestWithConfig(t, config)
73 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090074}
75
Paul Duffin02a3d652021-02-24 18:51:54 +000076// testCc runs tests using the ccFixtureFactory
77//
78// Do not add any new usages of this, instead use the ccFixtureFactory directly as it makes it much
79// easier to customize the test behavior.
80//
81// If it is necessary to customize the behavior of an existing test that uses this then please first
82// convert the test to using ccFixtureFactory first and then in a following change add the
83// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
84// that it did not change the test behavior unexpectedly.
85//
86// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080087func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080088 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000089 result := ccFixtureFactory.RunTestWithBp(t, bp)
90 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080091}
92
Paul Duffin02a3d652021-02-24 18:51:54 +000093// testCcNoVndk runs tests using the ccFixtureFactory
94//
95// See testCc for an explanation as to how to stop using this deprecated method.
96//
97// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080098func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080099 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800100 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700101 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800102
Colin Cross98be1bb2019-12-13 20:41:13 -0800103 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800104}
105
Paul Duffin02a3d652021-02-24 18:51:54 +0000106// testCcNoProductVndk runs tests using the ccFixtureFactory
107//
108// See testCc for an explanation as to how to stop using this deprecated method.
109//
110// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900111func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
112 t.Helper()
113 config := TestConfig(buildDir, android.Android, nil, bp, nil)
114 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
115 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
116
117 return testCcWithConfig(t, config)
118}
119
Paul Duffin02a3d652021-02-24 18:51:54 +0000120// testCcErrorWithConfig runs tests using the ccFixtureFactory
121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900125func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800126 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800127
Paul Duffin02a3d652021-02-24 18:51:54 +0000128 ccFixtureFactory.Extend().
129 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
130 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800131}
132
Paul Duffin02a3d652021-02-24 18:51:54 +0000133// testCcError runs tests using the ccFixtureFactory
134//
135// See testCc for an explanation as to how to stop using this deprecated method.
136//
137// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900138func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900139 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900140 config := TestConfig(buildDir, android.Android, nil, bp, nil)
141 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
142 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
143 testCcErrorWithConfig(t, pattern, config)
144 return
145}
146
Paul Duffin02a3d652021-02-24 18:51:54 +0000147// testCcErrorProductVndk runs tests using the ccFixtureFactory
148//
149// See testCc for an explanation as to how to stop using this deprecated method.
150//
151// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900152func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900153 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900154 config := TestConfig(buildDir, android.Android, nil, bp, nil)
155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
156 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
157 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
158 testCcErrorWithConfig(t, pattern, config)
159 return
160}
161
Logan Chienf3511742017-10-31 18:04:35 +0800162const (
Colin Cross7113d202019-11-20 16:39:12 -0800163 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800164 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900165 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800166 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800167)
168
Paul Duffindb462dd2021-03-21 22:01:55 +0000169// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
170// running it in a fixture that requires all source files to exist.
171func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
172 android.GroupFixturePreparers(
173 PrepareForTestWithCcDefaultModules,
174 android.PrepareForTestDisallowNonExistentPaths,
175 ).RunTest(t)
176}
177
Doug Hornc32c6b02019-01-17 14:44:05 -0800178func TestFuchsiaDeps(t *testing.T) {
179 t.Helper()
180
181 bp := `
182 cc_library {
183 name: "libTest",
184 srcs: ["foo.c"],
185 target: {
186 fuchsia: {
187 srcs: ["bar.c"],
188 },
189 },
190 }`
191
Paul Duffinecdac8a2021-02-24 19:18:42 +0000192 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800193
194 rt := false
195 fb := false
196
Paul Duffinecdac8a2021-02-24 19:18:42 +0000197 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800198 implicits := ld.Implicits
199 for _, lib := range implicits {
200 if strings.Contains(lib.Rel(), "libcompiler_rt") {
201 rt = true
202 }
203
204 if strings.Contains(lib.Rel(), "libbioniccompat") {
205 fb = true
206 }
207 }
208
209 if !rt || !fb {
210 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
211 }
212}
213
214func TestFuchsiaTargetDecl(t *testing.T) {
215 t.Helper()
216
217 bp := `
218 cc_library {
219 name: "libTest",
220 srcs: ["foo.c"],
221 target: {
222 fuchsia: {
223 srcs: ["bar.c"],
224 },
225 },
226 }`
227
Paul Duffinecdac8a2021-02-24 19:18:42 +0000228 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
229 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800230 var objs []string
231 for _, o := range ld.Inputs {
232 objs = append(objs, o.Base())
233 }
Paul Duffine84b1332021-03-12 11:59:43 +0000234 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800235}
236
Jiyong Park6a43f042017-10-12 23:05:00 +0900237func TestVendorSrc(t *testing.T) {
238 ctx := testCc(t, `
239 cc_library {
240 name: "libTest",
241 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700242 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800243 nocrt: true,
244 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900245 vendor_available: true,
246 target: {
247 vendor: {
248 srcs: ["bar.c"],
249 },
250 },
251 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900252 `)
253
Logan Chienf3511742017-10-31 18:04:35 +0800254 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900255 var objs []string
256 for _, o := range ld.Inputs {
257 objs = append(objs, o.Base())
258 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800259 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900260 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
261 }
262}
263
Logan Chienf3511742017-10-31 18:04:35 +0800264func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900265 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800266
Logan Chiend3c59a22018-03-29 14:08:15 +0800267 t.Helper()
268
Justin Yun0ecf0b22020-02-28 15:07:59 +0900269 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800270
271 // Check library properties.
272 lib, ok := mod.compiler.(*libraryDecorator)
273 if !ok {
274 t.Errorf("%q must have libraryDecorator", name)
275 } else if lib.baseInstaller.subDir != subDir {
276 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
277 lib.baseInstaller.subDir)
278 }
279
280 // Check VNDK properties.
281 if mod.vndkdep == nil {
282 t.Fatalf("%q must have `vndkdep`", name)
283 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 if !mod.IsVndk() {
285 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800286 }
287 if mod.isVndkSp() != isVndkSp {
288 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
289 }
290
291 // Check VNDK extension properties.
292 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500293 if mod.IsVndkExt() != isVndkExt {
294 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800295 }
296
297 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
298 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
299 }
300}
301
Jose Galmes0a942a02021-02-03 14:23:15 -0800302func 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 -0700303 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000304 mod := ctx.ModuleForTests(moduleName, variant)
305 outputFiles := mod.OutputFiles(t, "")
306 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900307 t.Errorf("%q must have single output\n", moduleName)
308 return
309 }
310 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900311
Bill Peckham945441c2020-08-31 16:07:58 -0700312 if include {
313 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800314 if fake {
315 if out.Rule == nil {
316 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
317 }
318 } else {
319 if out.Input.String() != outputFiles[0].String() {
320 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
321 }
Bill Peckham945441c2020-08-31 16:07:58 -0700322 }
323 } else {
324 out := singleton.MaybeOutput(snapshotPath)
325 if out.Rule != nil {
326 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
327 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900328 }
329}
330
Bill Peckham945441c2020-08-31 16:07:58 -0700331func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000332 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800333 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700334}
335
336func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000337 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800338 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
339}
340
341func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000342 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800343 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700344}
345
Jooyung Han2216fb12019-11-06 16:46:15 +0900346func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
347 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800348 content := android.ContentFromFileRuleForTests(t, params)
349 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900350 assertArrayString(t, actual, expected)
351}
352
Jooyung Han097087b2019-10-22 19:32:18 +0900353func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
354 t.Helper()
355 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900356 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
357}
358
359func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
360 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800361 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
362 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900363}
364
Logan Chienf3511742017-10-31 18:04:35 +0800365func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800366 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800367 cc_library {
368 name: "libvndk",
369 vendor_available: true,
370 vndk: {
371 enabled: true,
372 },
373 nocrt: true,
374 }
375
376 cc_library {
377 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900378 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800379 vndk: {
380 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900381 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800382 },
383 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900384 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800385 }
386
387 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900388 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800389 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900390 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800391 vndk: {
392 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900393 },
394 nocrt: true,
395 target: {
396 vendor: {
397 cflags: ["-DTEST"],
398 },
399 product: {
400 cflags: ["-DTEST"],
401 },
402 },
403 }
404
405 cc_library {
406 name: "libvndk_sp",
407 vendor_available: true,
408 vndk: {
409 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800410 support_system_process: true,
411 },
412 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900413 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800414 }
415
416 cc_library {
417 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900418 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800419 vndk: {
420 enabled: true,
421 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900422 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800423 },
424 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900425 target: {
426 vendor: {
427 suffix: "-x",
428 },
429 },
Logan Chienf3511742017-10-31 18:04:35 +0800430 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900431
432 cc_library {
433 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900434 vendor_available: true,
435 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900436 vndk: {
437 enabled: true,
438 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900439 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900440 },
441 nocrt: true,
442 target: {
443 vendor: {
444 suffix: "-x",
445 },
446 product: {
447 suffix: "-x",
448 },
449 },
450 }
451
Colin Crosse4e44bc2020-12-28 13:50:21 -0800452 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900453 name: "llndk.libraries.txt",
454 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800455 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900456 name: "vndkcore.libraries.txt",
457 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800458 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900459 name: "vndksp.libraries.txt",
460 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800461 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900462 name: "vndkprivate.libraries.txt",
463 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800464 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900465 name: "vndkproduct.libraries.txt",
466 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800467 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900468 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800469 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900470 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800471 `
472
473 config := TestConfig(buildDir, android.Android, nil, bp, nil)
474 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900475 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800476 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
477
478 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800479
Jooyung Han261e1582020-10-20 18:54:21 +0900480 // subdir == "" because VNDK libs are not supposed to be installed separately.
481 // They are installed as part of VNDK APEX instead.
482 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
483 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900484 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900485 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
486 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900487 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900488
Justin Yun6977e8a2020-10-29 18:24:11 +0900489 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
490 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900491
Inseob Kim1f086e22019-05-09 13:29:15 +0900492 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900493 snapshotDir := "vndk-snapshot"
494 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
495
496 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
497 "arm64", "armv8-a"))
498 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
499 "arm", "armv7-a-neon"))
500
501 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
502 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
503 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
504 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
505
Colin Crossfb0c16e2019-11-20 17:12:35 -0800506 variant := "android_vendor.VER_arm64_armv8-a_shared"
507 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900508
Inseob Kim7f283f42020-06-01 21:53:49 +0900509 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
510
511 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
512 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900513 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
514 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900515 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
516 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900517
Jooyung Han39edb6c2019-11-06 16:53:07 +0900518 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900519 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
520 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
521 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
522 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900523 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900524
Jooyung Han097087b2019-10-22 19:32:18 +0900525 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
526 "LLNDK: libc.so",
527 "LLNDK: libdl.so",
528 "LLNDK: libft2.so",
529 "LLNDK: libm.so",
530 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900531 "VNDK-SP: libvndk_sp-x.so",
532 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900533 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900534 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900535 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900536 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900537 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900538 "VNDK-private: libvndk-private.so",
539 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900540 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900541 "VNDK-product: libc++.so",
542 "VNDK-product: libvndk_product.so",
543 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900544 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900545 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900546 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
547 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
548 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 +0900549 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 +0900550 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
551}
552
Yo Chiangbba545e2020-06-09 16:15:37 +0800553func TestVndkWithHostSupported(t *testing.T) {
554 ctx := testCc(t, `
555 cc_library {
556 name: "libvndk_host_supported",
557 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900558 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800559 vndk: {
560 enabled: true,
561 },
562 host_supported: true,
563 }
564
565 cc_library {
566 name: "libvndk_host_supported_but_disabled_on_device",
567 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900568 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800569 vndk: {
570 enabled: true,
571 },
572 host_supported: true,
573 enabled: false,
574 target: {
575 host: {
576 enabled: true,
577 }
578 }
579 }
580
Colin Crosse4e44bc2020-12-28 13:50:21 -0800581 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800582 name: "vndkcore.libraries.txt",
583 }
584 `)
585
586 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
587}
588
Jooyung Han2216fb12019-11-06 16:46:15 +0900589func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800590 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800591 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900592 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800593 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800594 }`
595 config := TestConfig(buildDir, android.Android, nil, bp, nil)
596 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
597 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
598 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900599
600 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700601 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900602 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900603}
604
605func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800606 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900607 cc_library {
608 name: "libvndk",
609 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900610 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900611 vndk: {
612 enabled: true,
613 },
614 nocrt: true,
615 }
616
617 cc_library {
618 name: "libvndk_sp",
619 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900620 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900621 vndk: {
622 enabled: true,
623 support_system_process: true,
624 },
625 nocrt: true,
626 }
627
628 cc_library {
629 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900630 vendor_available: true,
631 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900632 vndk: {
633 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900634 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900635 },
636 nocrt: true,
637 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900638
Colin Crosse4e44bc2020-12-28 13:50:21 -0800639 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900640 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800641 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900642 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800643 `
644
645 config := TestConfig(buildDir, android.Android, nil, bp, nil)
646 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
647 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
648 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
649
650 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
651
652 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900653
Jooyung Han2216fb12019-11-06 16:46:15 +0900654 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900655}
656
Chris Parsons79d66a52020-06-05 17:26:16 -0400657func TestDataLibs(t *testing.T) {
658 bp := `
659 cc_test_library {
660 name: "test_lib",
661 srcs: ["test_lib.cpp"],
662 gtest: false,
663 }
664
665 cc_test {
666 name: "main_test",
667 data_libs: ["test_lib"],
668 gtest: false,
669 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400670 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400671
672 config := TestConfig(buildDir, android.Android, nil, bp, nil)
673 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
674 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
675 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
676
677 ctx := testCcWithConfig(t, config)
678 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
679 testBinary := module.(*Module).linker.(*testBinary)
680 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
681 if err != nil {
682 t.Errorf("Expected cc_test to produce output files, error: %s", err)
683 return
684 }
685 if len(outputFiles) != 1 {
686 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
687 return
688 }
689 if len(testBinary.dataPaths()) != 1 {
690 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
691 return
692 }
693
694 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400695 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400696
697 if !strings.HasSuffix(outputPath, "/main_test") {
698 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
699 return
700 }
701 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
702 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
703 return
704 }
705}
706
Chris Parsons216e10a2020-07-09 17:12:52 -0400707func TestDataLibsRelativeInstallPath(t *testing.T) {
708 bp := `
709 cc_test_library {
710 name: "test_lib",
711 srcs: ["test_lib.cpp"],
712 relative_install_path: "foo/bar/baz",
713 gtest: false,
714 }
715
716 cc_test {
717 name: "main_test",
718 data_libs: ["test_lib"],
719 gtest: false,
720 }
721 `
722
723 config := TestConfig(buildDir, android.Android, nil, bp, nil)
724 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
725 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
726 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
727
728 ctx := testCcWithConfig(t, config)
729 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
730 testBinary := module.(*Module).linker.(*testBinary)
731 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
732 if err != nil {
733 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
734 }
735 if len(outputFiles) != 1 {
736 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
737 }
738 if len(testBinary.dataPaths()) != 1 {
739 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
740 }
741
742 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400743
744 if !strings.HasSuffix(outputPath, "/main_test") {
745 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
746 }
Colin Crossaa255532020-07-03 13:18:24 -0700747 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400748 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
749 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400750 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400751 }
752}
753
Jooyung Han0302a842019-10-30 18:43:49 +0900754func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900755 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900756 cc_library {
757 name: "libvndk",
758 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900759 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900760 vndk: {
761 enabled: true,
762 },
763 nocrt: true,
764 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900765 cc_library {
766 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900767 vendor_available: true,
768 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900769 vndk: {
770 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900771 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900772 },
773 nocrt: true,
774 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800775
776 cc_library {
777 name: "libllndk",
778 llndk_stubs: "libllndk.llndk",
779 }
780
781 llndk_library {
782 name: "libllndk.llndk",
783 symbol_file: "",
784 export_llndk_headers: ["libllndk_headers"],
785 }
786
787 llndk_headers {
788 name: "libllndk_headers",
789 export_include_dirs: ["include"],
790 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900791 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900792
793 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
794 "LLNDK: libc.so",
795 "LLNDK: libdl.so",
796 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800797 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900798 "LLNDK: libm.so",
799 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900800 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900801 "VNDK-core: libvndk.so",
802 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900803 "VNDK-private: libvndk-private.so",
804 "VNDK-product: libc++.so",
805 "VNDK-product: libvndk-private.so",
806 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900807 })
Logan Chienf3511742017-10-31 18:04:35 +0800808}
809
Justin Yun63e9ec72020-10-29 16:49:43 +0900810func TestVndkModuleError(t *testing.T) {
811 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900812 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900813 cc_library {
814 name: "libvndk",
815 vndk: {
816 enabled: true,
817 },
818 nocrt: true,
819 }
820 `)
821
Justin Yunc0d8c492021-01-07 17:45:31 +0900822 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900823 cc_library {
824 name: "libvndk",
825 product_available: true,
826 vndk: {
827 enabled: true,
828 },
829 nocrt: true,
830 }
831 `)
832
Justin Yun6977e8a2020-10-29 18:24:11 +0900833 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
834 cc_library {
835 name: "libvndkprop",
836 vendor_available: true,
837 product_available: true,
838 vndk: {
839 enabled: true,
840 },
841 nocrt: true,
842 target: {
843 vendor: {
844 cflags: ["-DTEST",],
845 },
846 },
847 }
848 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900849}
850
Logan Chiend3c59a22018-03-29 14:08:15 +0800851func TestVndkDepError(t *testing.T) {
852 // Check whether an error is emitted when a VNDK lib depends on a system lib.
853 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
854 cc_library {
855 name: "libvndk",
856 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900857 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800858 vndk: {
859 enabled: true,
860 },
861 shared_libs: ["libfwk"], // Cause error
862 nocrt: true,
863 }
864
865 cc_library {
866 name: "libfwk",
867 nocrt: true,
868 }
869 `)
870
871 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
872 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
873 cc_library {
874 name: "libvndk",
875 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900876 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800877 vndk: {
878 enabled: true,
879 },
880 shared_libs: ["libvendor"], // Cause error
881 nocrt: true,
882 }
883
884 cc_library {
885 name: "libvendor",
886 vendor: true,
887 nocrt: true,
888 }
889 `)
890
891 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
892 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
893 cc_library {
894 name: "libvndk_sp",
895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900896 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800897 vndk: {
898 enabled: true,
899 support_system_process: true,
900 },
901 shared_libs: ["libfwk"], // Cause error
902 nocrt: true,
903 }
904
905 cc_library {
906 name: "libfwk",
907 nocrt: true,
908 }
909 `)
910
911 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
912 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
913 cc_library {
914 name: "libvndk_sp",
915 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900916 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800917 vndk: {
918 enabled: true,
919 support_system_process: true,
920 },
921 shared_libs: ["libvendor"], // Cause error
922 nocrt: true,
923 }
924
925 cc_library {
926 name: "libvendor",
927 vendor: true,
928 nocrt: true,
929 }
930 `)
931
932 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
933 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
934 cc_library {
935 name: "libvndk_sp",
936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900937 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800938 vndk: {
939 enabled: true,
940 support_system_process: true,
941 },
942 shared_libs: ["libvndk"], // Cause error
943 nocrt: true,
944 }
945
946 cc_library {
947 name: "libvndk",
948 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900949 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800950 vndk: {
951 enabled: true,
952 },
953 nocrt: true,
954 }
955 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900956
957 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
958 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
959 cc_library {
960 name: "libvndk",
961 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900962 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900963 vndk: {
964 enabled: true,
965 },
966 shared_libs: ["libnonvndk"],
967 nocrt: true,
968 }
969
970 cc_library {
971 name: "libnonvndk",
972 vendor_available: true,
973 nocrt: true,
974 }
975 `)
976
977 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
978 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
979 cc_library {
980 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900981 vendor_available: true,
982 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900983 vndk: {
984 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900985 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900986 },
987 shared_libs: ["libnonvndk"],
988 nocrt: true,
989 }
990
991 cc_library {
992 name: "libnonvndk",
993 vendor_available: true,
994 nocrt: true,
995 }
996 `)
997
998 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
999 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1000 cc_library {
1001 name: "libvndksp",
1002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001003 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001004 vndk: {
1005 enabled: true,
1006 support_system_process: true,
1007 },
1008 shared_libs: ["libnonvndk"],
1009 nocrt: true,
1010 }
1011
1012 cc_library {
1013 name: "libnonvndk",
1014 vendor_available: true,
1015 nocrt: true,
1016 }
1017 `)
1018
1019 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1020 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1021 cc_library {
1022 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001023 vendor_available: true,
1024 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001025 vndk: {
1026 enabled: true,
1027 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001028 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001029 },
1030 shared_libs: ["libnonvndk"],
1031 nocrt: true,
1032 }
1033
1034 cc_library {
1035 name: "libnonvndk",
1036 vendor_available: true,
1037 nocrt: true,
1038 }
1039 `)
1040}
1041
1042func TestDoubleLoadbleDep(t *testing.T) {
1043 // okay to link : LLNDK -> double_loadable VNDK
1044 testCc(t, `
1045 cc_library {
1046 name: "libllndk",
1047 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001048 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001049 }
1050
1051 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001052 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001053 symbol_file: "",
1054 }
1055
1056 cc_library {
1057 name: "libdoubleloadable",
1058 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001059 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001060 vndk: {
1061 enabled: true,
1062 },
1063 double_loadable: true,
1064 }
1065 `)
1066 // okay to link : LLNDK -> VNDK-SP
1067 testCc(t, `
1068 cc_library {
1069 name: "libllndk",
1070 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001071 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001072 }
1073
1074 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001075 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001076 symbol_file: "",
1077 }
1078
1079 cc_library {
1080 name: "libvndksp",
1081 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001082 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001083 vndk: {
1084 enabled: true,
1085 support_system_process: true,
1086 },
1087 }
1088 `)
1089 // okay to link : double_loadable -> double_loadable
1090 testCc(t, `
1091 cc_library {
1092 name: "libdoubleloadable1",
1093 shared_libs: ["libdoubleloadable2"],
1094 vendor_available: true,
1095 double_loadable: true,
1096 }
1097
1098 cc_library {
1099 name: "libdoubleloadable2",
1100 vendor_available: true,
1101 double_loadable: true,
1102 }
1103 `)
1104 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1105 testCc(t, `
1106 cc_library {
1107 name: "libdoubleloadable",
1108 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001109 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001110 vndk: {
1111 enabled: true,
1112 },
1113 double_loadable: true,
1114 shared_libs: ["libnondoubleloadable"],
1115 }
1116
1117 cc_library {
1118 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001119 vendor_available: true,
1120 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001121 vndk: {
1122 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001123 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001124 },
1125 double_loadable: true,
1126 }
1127 `)
1128 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1129 testCc(t, `
1130 cc_library {
1131 name: "libllndk",
1132 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001133 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001134 }
1135
1136 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001137 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001138 symbol_file: "",
1139 }
1140
1141 cc_library {
1142 name: "libcoreonly",
1143 shared_libs: ["libvendoravailable"],
1144 }
1145
1146 // indirect dependency of LLNDK
1147 cc_library {
1148 name: "libvendoravailable",
1149 vendor_available: true,
1150 double_loadable: true,
1151 }
1152 `)
1153}
1154
1155func TestDoubleLoadableDepError(t *testing.T) {
1156 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1157 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1158 cc_library {
1159 name: "libllndk",
1160 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001161 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001162 }
1163
1164 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001165 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001166 symbol_file: "",
1167 }
1168
1169 cc_library {
1170 name: "libnondoubleloadable",
1171 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001172 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001173 vndk: {
1174 enabled: true,
1175 },
1176 }
1177 `)
1178
1179 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1180 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1181 cc_library {
1182 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001183 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001184 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001185 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001186 }
1187
1188 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001189 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001190 symbol_file: "",
1191 }
1192
1193 cc_library {
1194 name: "libnondoubleloadable",
1195 vendor_available: true,
1196 }
1197 `)
1198
Jooyung Hana70f0672019-01-18 15:20:43 +09001199 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1200 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1201 cc_library {
1202 name: "libllndk",
1203 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001204 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001205 }
1206
1207 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001208 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 symbol_file: "",
1210 }
1211
1212 cc_library {
1213 name: "libcoreonly",
1214 shared_libs: ["libvendoravailable"],
1215 }
1216
1217 // indirect dependency of LLNDK
1218 cc_library {
1219 name: "libvendoravailable",
1220 vendor_available: true,
1221 }
1222 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001223
1224 // The error is not from 'client' but from 'libllndk'
1225 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1226 cc_library {
1227 name: "client",
1228 vendor_available: true,
1229 double_loadable: true,
1230 shared_libs: ["libllndk"],
1231 }
1232 cc_library {
1233 name: "libllndk",
1234 shared_libs: ["libnondoubleloadable"],
1235 llndk_stubs: "libllndk.llndk",
1236 }
1237 llndk_library {
1238 name: "libllndk.llndk",
1239 symbol_file: "",
1240 }
1241 cc_library {
1242 name: "libnondoubleloadable",
1243 vendor_available: true,
1244 }
1245 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001246}
1247
Jooyung Han479ca172020-10-19 18:51:07 +09001248func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1249 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1250 cc_library {
1251 name: "libvndksp",
1252 shared_libs: ["libanothervndksp"],
1253 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001254 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001255 vndk: {
1256 enabled: true,
1257 support_system_process: true,
1258 }
1259 }
1260
1261 cc_library {
1262 name: "libllndk",
1263 shared_libs: ["libanothervndksp"],
1264 }
1265
1266 llndk_library {
1267 name: "libllndk",
1268 symbol_file: "",
1269 }
1270
1271 cc_library {
1272 name: "libanothervndksp",
1273 vendor_available: true,
1274 }
1275 `)
1276}
1277
Logan Chienf3511742017-10-31 18:04:35 +08001278func TestVndkExt(t *testing.T) {
1279 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001280 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001281 cc_library {
1282 name: "libvndk",
1283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001284 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001285 vndk: {
1286 enabled: true,
1287 },
1288 nocrt: true,
1289 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001290 cc_library {
1291 name: "libvndk2",
1292 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001293 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001294 vndk: {
1295 enabled: true,
1296 },
1297 target: {
1298 vendor: {
1299 suffix: "-suffix",
1300 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001301 product: {
1302 suffix: "-suffix",
1303 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001304 },
1305 nocrt: true,
1306 }
Logan Chienf3511742017-10-31 18:04:35 +08001307
1308 cc_library {
1309 name: "libvndk_ext",
1310 vendor: true,
1311 vndk: {
1312 enabled: true,
1313 extends: "libvndk",
1314 },
1315 nocrt: true,
1316 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001317
1318 cc_library {
1319 name: "libvndk2_ext",
1320 vendor: true,
1321 vndk: {
1322 enabled: true,
1323 extends: "libvndk2",
1324 },
1325 nocrt: true,
1326 }
Logan Chienf3511742017-10-31 18:04:35 +08001327
Justin Yun0ecf0b22020-02-28 15:07:59 +09001328 cc_library {
1329 name: "libvndk_ext_product",
1330 product_specific: true,
1331 vndk: {
1332 enabled: true,
1333 extends: "libvndk",
1334 },
1335 nocrt: true,
1336 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001337
Justin Yun0ecf0b22020-02-28 15:07:59 +09001338 cc_library {
1339 name: "libvndk2_ext_product",
1340 product_specific: true,
1341 vndk: {
1342 enabled: true,
1343 extends: "libvndk2",
1344 },
1345 nocrt: true,
1346 }
1347 `
1348 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1349 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1350 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1351 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1352
1353 ctx := testCcWithConfig(t, config)
1354
1355 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1356 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1357
1358 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1359 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1360
1361 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1362 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001363}
1364
Logan Chiend3c59a22018-03-29 14:08:15 +08001365func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001366 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1367 ctx := testCcNoVndk(t, `
1368 cc_library {
1369 name: "libvndk",
1370 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001371 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001372 vndk: {
1373 enabled: true,
1374 },
1375 nocrt: true,
1376 }
1377
1378 cc_library {
1379 name: "libvndk_ext",
1380 vendor: true,
1381 vndk: {
1382 enabled: true,
1383 extends: "libvndk",
1384 },
1385 nocrt: true,
1386 }
1387 `)
1388
1389 // Ensures that the core variant of "libvndk_ext" can be found.
1390 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1391 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1392 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1393 }
1394}
1395
Justin Yun0ecf0b22020-02-28 15:07:59 +09001396func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1397 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001398 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001399 cc_library {
1400 name: "libvndk",
1401 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001402 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001403 vndk: {
1404 enabled: true,
1405 },
1406 nocrt: true,
1407 }
1408
1409 cc_library {
1410 name: "libvndk_ext_product",
1411 product_specific: true,
1412 vndk: {
1413 enabled: true,
1414 extends: "libvndk",
1415 },
1416 nocrt: true,
1417 }
1418 `)
1419
1420 // Ensures that the core variant of "libvndk_ext_product" can be found.
1421 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1422 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1423 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1424 }
1425}
1426
Logan Chienf3511742017-10-31 18:04:35 +08001427func TestVndkExtError(t *testing.T) {
1428 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001429 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001430 cc_library {
1431 name: "libvndk",
1432 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001433 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001434 vndk: {
1435 enabled: true,
1436 },
1437 nocrt: true,
1438 }
1439
1440 cc_library {
1441 name: "libvndk_ext",
1442 vndk: {
1443 enabled: true,
1444 extends: "libvndk",
1445 },
1446 nocrt: true,
1447 }
1448 `)
1449
1450 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1451 cc_library {
1452 name: "libvndk",
1453 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001454 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001455 vndk: {
1456 enabled: true,
1457 },
1458 nocrt: true,
1459 }
1460
1461 cc_library {
1462 name: "libvndk_ext",
1463 vendor: true,
1464 vndk: {
1465 enabled: true,
1466 },
1467 nocrt: true,
1468 }
1469 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001470
1471 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1472 cc_library {
1473 name: "libvndk",
1474 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001475 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001476 vndk: {
1477 enabled: true,
1478 },
1479 nocrt: true,
1480 }
1481
1482 cc_library {
1483 name: "libvndk_ext_product",
1484 product_specific: true,
1485 vndk: {
1486 enabled: true,
1487 },
1488 nocrt: true,
1489 }
1490 `)
1491
1492 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1493 cc_library {
1494 name: "libvndk",
1495 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001496 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001497 vndk: {
1498 enabled: true,
1499 },
1500 nocrt: true,
1501 }
1502
1503 cc_library {
1504 name: "libvndk_ext_product",
1505 product_specific: true,
1506 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001507 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001508 vndk: {
1509 enabled: true,
1510 extends: "libvndk",
1511 },
1512 nocrt: true,
1513 }
1514 `)
Logan Chienf3511742017-10-31 18:04:35 +08001515}
1516
1517func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1518 // This test ensures an error is emitted for inconsistent support_system_process.
1519 testCcError(t, "module \".*\" with mismatched support_system_process", `
1520 cc_library {
1521 name: "libvndk",
1522 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001523 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001524 vndk: {
1525 enabled: true,
1526 },
1527 nocrt: true,
1528 }
1529
1530 cc_library {
1531 name: "libvndk_sp_ext",
1532 vendor: true,
1533 vndk: {
1534 enabled: true,
1535 extends: "libvndk",
1536 support_system_process: true,
1537 },
1538 nocrt: true,
1539 }
1540 `)
1541
1542 testCcError(t, "module \".*\" with mismatched support_system_process", `
1543 cc_library {
1544 name: "libvndk_sp",
1545 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001546 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001547 vndk: {
1548 enabled: true,
1549 support_system_process: true,
1550 },
1551 nocrt: true,
1552 }
1553
1554 cc_library {
1555 name: "libvndk_ext",
1556 vendor: true,
1557 vndk: {
1558 enabled: true,
1559 extends: "libvndk_sp",
1560 },
1561 nocrt: true,
1562 }
1563 `)
1564}
1565
1566func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001567 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001568 // with `private: true`.
1569 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001570 cc_library {
1571 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001572 vendor_available: true,
1573 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001574 vndk: {
1575 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001576 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001577 },
1578 nocrt: true,
1579 }
1580
1581 cc_library {
1582 name: "libvndk_ext",
1583 vendor: true,
1584 vndk: {
1585 enabled: true,
1586 extends: "libvndk",
1587 },
1588 nocrt: true,
1589 }
1590 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001591
Justin Yunfd9e8042020-12-23 18:23:14 +09001592 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001593 cc_library {
1594 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001595 vendor_available: true,
1596 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001597 vndk: {
1598 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001599 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001600 },
1601 nocrt: true,
1602 }
1603
1604 cc_library {
1605 name: "libvndk_ext_product",
1606 product_specific: true,
1607 vndk: {
1608 enabled: true,
1609 extends: "libvndk",
1610 },
1611 nocrt: true,
1612 }
1613 `)
Logan Chienf3511742017-10-31 18:04:35 +08001614}
1615
Logan Chiend3c59a22018-03-29 14:08:15 +08001616func TestVendorModuleUseVndkExt(t *testing.T) {
1617 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001618 testCc(t, `
1619 cc_library {
1620 name: "libvndk",
1621 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001622 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001623 vndk: {
1624 enabled: true,
1625 },
1626 nocrt: true,
1627 }
1628
1629 cc_library {
1630 name: "libvndk_ext",
1631 vendor: true,
1632 vndk: {
1633 enabled: true,
1634 extends: "libvndk",
1635 },
1636 nocrt: true,
1637 }
1638
1639 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001640 name: "libvndk_sp",
1641 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001642 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001643 vndk: {
1644 enabled: true,
1645 support_system_process: true,
1646 },
1647 nocrt: true,
1648 }
1649
1650 cc_library {
1651 name: "libvndk_sp_ext",
1652 vendor: true,
1653 vndk: {
1654 enabled: true,
1655 extends: "libvndk_sp",
1656 support_system_process: true,
1657 },
1658 nocrt: true,
1659 }
1660
1661 cc_library {
1662 name: "libvendor",
1663 vendor: true,
1664 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1665 nocrt: true,
1666 }
1667 `)
1668}
1669
Logan Chiend3c59a22018-03-29 14:08:15 +08001670func TestVndkExtUseVendorLib(t *testing.T) {
1671 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001672 testCc(t, `
1673 cc_library {
1674 name: "libvndk",
1675 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001676 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001677 vndk: {
1678 enabled: true,
1679 },
1680 nocrt: true,
1681 }
1682
1683 cc_library {
1684 name: "libvndk_ext",
1685 vendor: true,
1686 vndk: {
1687 enabled: true,
1688 extends: "libvndk",
1689 },
1690 shared_libs: ["libvendor"],
1691 nocrt: true,
1692 }
1693
1694 cc_library {
1695 name: "libvendor",
1696 vendor: true,
1697 nocrt: true,
1698 }
1699 `)
Logan Chienf3511742017-10-31 18:04:35 +08001700
Logan Chiend3c59a22018-03-29 14:08:15 +08001701 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1702 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001703 cc_library {
1704 name: "libvndk_sp",
1705 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001706 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001707 vndk: {
1708 enabled: true,
1709 support_system_process: true,
1710 },
1711 nocrt: true,
1712 }
1713
1714 cc_library {
1715 name: "libvndk_sp_ext",
1716 vendor: true,
1717 vndk: {
1718 enabled: true,
1719 extends: "libvndk_sp",
1720 support_system_process: true,
1721 },
1722 shared_libs: ["libvendor"], // Cause an error
1723 nocrt: true,
1724 }
1725
1726 cc_library {
1727 name: "libvendor",
1728 vendor: true,
1729 nocrt: true,
1730 }
1731 `)
1732}
1733
Justin Yun0ecf0b22020-02-28 15:07:59 +09001734func TestProductVndkExtDependency(t *testing.T) {
1735 bp := `
1736 cc_library {
1737 name: "libvndk",
1738 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001739 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001740 vndk: {
1741 enabled: true,
1742 },
1743 nocrt: true,
1744 }
1745
1746 cc_library {
1747 name: "libvndk_ext_product",
1748 product_specific: true,
1749 vndk: {
1750 enabled: true,
1751 extends: "libvndk",
1752 },
1753 shared_libs: ["libproduct_for_vndklibs"],
1754 nocrt: true,
1755 }
1756
1757 cc_library {
1758 name: "libvndk_sp",
1759 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001760 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001761 vndk: {
1762 enabled: true,
1763 support_system_process: true,
1764 },
1765 nocrt: true,
1766 }
1767
1768 cc_library {
1769 name: "libvndk_sp_ext_product",
1770 product_specific: true,
1771 vndk: {
1772 enabled: true,
1773 extends: "libvndk_sp",
1774 support_system_process: true,
1775 },
1776 shared_libs: ["libproduct_for_vndklibs"],
1777 nocrt: true,
1778 }
1779
1780 cc_library {
1781 name: "libproduct",
1782 product_specific: true,
1783 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1784 nocrt: true,
1785 }
1786
1787 cc_library {
1788 name: "libproduct_for_vndklibs",
1789 product_specific: true,
1790 nocrt: true,
1791 }
1792 `
1793 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1794 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1795 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1796 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1797
1798 testCcWithConfig(t, config)
1799}
1800
Logan Chiend3c59a22018-03-29 14:08:15 +08001801func TestVndkSpExtUseVndkError(t *testing.T) {
1802 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1803 // library.
1804 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1805 cc_library {
1806 name: "libvndk",
1807 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001808 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001809 vndk: {
1810 enabled: true,
1811 },
1812 nocrt: true,
1813 }
1814
1815 cc_library {
1816 name: "libvndk_sp",
1817 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001818 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001819 vndk: {
1820 enabled: true,
1821 support_system_process: true,
1822 },
1823 nocrt: true,
1824 }
1825
1826 cc_library {
1827 name: "libvndk_sp_ext",
1828 vendor: true,
1829 vndk: {
1830 enabled: true,
1831 extends: "libvndk_sp",
1832 support_system_process: true,
1833 },
1834 shared_libs: ["libvndk"], // Cause an error
1835 nocrt: true,
1836 }
1837 `)
1838
1839 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1840 // library.
1841 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1842 cc_library {
1843 name: "libvndk",
1844 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001845 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001846 vndk: {
1847 enabled: true,
1848 },
1849 nocrt: true,
1850 }
1851
1852 cc_library {
1853 name: "libvndk_ext",
1854 vendor: true,
1855 vndk: {
1856 enabled: true,
1857 extends: "libvndk",
1858 },
1859 nocrt: true,
1860 }
1861
1862 cc_library {
1863 name: "libvndk_sp",
1864 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001865 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001866 vndk: {
1867 enabled: true,
1868 support_system_process: true,
1869 },
1870 nocrt: true,
1871 }
1872
1873 cc_library {
1874 name: "libvndk_sp_ext",
1875 vendor: true,
1876 vndk: {
1877 enabled: true,
1878 extends: "libvndk_sp",
1879 support_system_process: true,
1880 },
1881 shared_libs: ["libvndk_ext"], // Cause an error
1882 nocrt: true,
1883 }
1884 `)
1885}
1886
1887func TestVndkUseVndkExtError(t *testing.T) {
1888 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1889 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001890 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1891 cc_library {
1892 name: "libvndk",
1893 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001894 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001895 vndk: {
1896 enabled: true,
1897 },
1898 nocrt: true,
1899 }
1900
1901 cc_library {
1902 name: "libvndk_ext",
1903 vendor: true,
1904 vndk: {
1905 enabled: true,
1906 extends: "libvndk",
1907 },
1908 nocrt: true,
1909 }
1910
1911 cc_library {
1912 name: "libvndk2",
1913 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001914 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001915 vndk: {
1916 enabled: true,
1917 },
1918 shared_libs: ["libvndk_ext"],
1919 nocrt: true,
1920 }
1921 `)
1922
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001923 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001924 cc_library {
1925 name: "libvndk",
1926 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001927 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001928 vndk: {
1929 enabled: true,
1930 },
1931 nocrt: true,
1932 }
1933
1934 cc_library {
1935 name: "libvndk_ext",
1936 vendor: true,
1937 vndk: {
1938 enabled: true,
1939 extends: "libvndk",
1940 },
1941 nocrt: true,
1942 }
1943
1944 cc_library {
1945 name: "libvndk2",
1946 vendor_available: true,
1947 vndk: {
1948 enabled: true,
1949 },
1950 target: {
1951 vendor: {
1952 shared_libs: ["libvndk_ext"],
1953 },
1954 },
1955 nocrt: true,
1956 }
1957 `)
1958
1959 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1960 cc_library {
1961 name: "libvndk_sp",
1962 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001963 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001964 vndk: {
1965 enabled: true,
1966 support_system_process: true,
1967 },
1968 nocrt: true,
1969 }
1970
1971 cc_library {
1972 name: "libvndk_sp_ext",
1973 vendor: true,
1974 vndk: {
1975 enabled: true,
1976 extends: "libvndk_sp",
1977 support_system_process: true,
1978 },
1979 nocrt: true,
1980 }
1981
1982 cc_library {
1983 name: "libvndk_sp_2",
1984 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001985 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001986 vndk: {
1987 enabled: true,
1988 support_system_process: true,
1989 },
1990 shared_libs: ["libvndk_sp_ext"],
1991 nocrt: true,
1992 }
1993 `)
1994
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001995 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001996 cc_library {
1997 name: "libvndk_sp",
1998 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001999 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002000 vndk: {
2001 enabled: true,
2002 },
2003 nocrt: true,
2004 }
2005
2006 cc_library {
2007 name: "libvndk_sp_ext",
2008 vendor: true,
2009 vndk: {
2010 enabled: true,
2011 extends: "libvndk_sp",
2012 },
2013 nocrt: true,
2014 }
2015
2016 cc_library {
2017 name: "libvndk_sp2",
2018 vendor_available: true,
2019 vndk: {
2020 enabled: true,
2021 },
2022 target: {
2023 vendor: {
2024 shared_libs: ["libvndk_sp_ext"],
2025 },
2026 },
2027 nocrt: true,
2028 }
2029 `)
2030}
2031
Justin Yun5f7f7e82019-11-18 19:52:14 +09002032func TestEnforceProductVndkVersion(t *testing.T) {
2033 bp := `
2034 cc_library {
2035 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002036 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002037 }
2038 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002039 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002040 symbol_file: "",
2041 }
2042 cc_library {
2043 name: "libvndk",
2044 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002045 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002046 vndk: {
2047 enabled: true,
2048 },
2049 nocrt: true,
2050 }
2051 cc_library {
2052 name: "libvndk_sp",
2053 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002054 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002055 vndk: {
2056 enabled: true,
2057 support_system_process: true,
2058 },
2059 nocrt: true,
2060 }
2061 cc_library {
2062 name: "libva",
2063 vendor_available: true,
2064 nocrt: true,
2065 }
2066 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002067 name: "libpa",
2068 product_available: true,
2069 nocrt: true,
2070 }
2071 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002072 name: "libboth_available",
2073 vendor_available: true,
2074 product_available: true,
2075 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002076 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002077 target: {
2078 vendor: {
2079 suffix: "-vendor",
2080 },
2081 product: {
2082 suffix: "-product",
2083 },
2084 }
2085 }
2086 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002087 name: "libproduct_va",
2088 product_specific: true,
2089 vendor_available: true,
2090 nocrt: true,
2091 }
2092 cc_library {
2093 name: "libprod",
2094 product_specific: true,
2095 shared_libs: [
2096 "libllndk",
2097 "libvndk",
2098 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002099 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002100 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002101 "libproduct_va",
2102 ],
2103 nocrt: true,
2104 }
2105 cc_library {
2106 name: "libvendor",
2107 vendor: true,
2108 shared_libs: [
2109 "libllndk",
2110 "libvndk",
2111 "libvndk_sp",
2112 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002113 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002114 "libproduct_va",
2115 ],
2116 nocrt: true,
2117 }
2118 `
2119
Justin Yun13decfb2021-03-08 19:25:55 +09002120 ctx := ccFixtureFactory.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002121
Jooyung Han261e1582020-10-20 18:54:21 +09002122 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2123 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002124
2125 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2126 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2127
2128 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2129 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002130
2131 ensureStringContains := func(t *testing.T, str string, substr string) {
2132 t.Helper()
2133 if !strings.Contains(str, substr) {
2134 t.Errorf("%q is not found in %v", substr, str)
2135 }
2136 }
2137 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2138 t.Helper()
2139 if strings.Contains(str, substr) {
2140 t.Errorf("%q is found in %v", substr, str)
2141 }
2142 }
2143
2144 // _static variant is used since _shared reuses *.o from the static variant
2145 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2146 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2147
2148 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2149 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2150 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2151 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2152
2153 product_cflags := product_static.Rule("cc").Args["cFlags"]
2154 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2155 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2156 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002157}
2158
2159func TestEnforceProductVndkVersionErrors(t *testing.T) {
2160 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2161 cc_library {
2162 name: "libprod",
2163 product_specific: true,
2164 shared_libs: [
2165 "libvendor",
2166 ],
2167 nocrt: true,
2168 }
2169 cc_library {
2170 name: "libvendor",
2171 vendor: true,
2172 nocrt: true,
2173 }
2174 `)
2175 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2176 cc_library {
2177 name: "libprod",
2178 product_specific: true,
2179 shared_libs: [
2180 "libsystem",
2181 ],
2182 nocrt: true,
2183 }
2184 cc_library {
2185 name: "libsystem",
2186 nocrt: true,
2187 }
2188 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002189 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2190 cc_library {
2191 name: "libprod",
2192 product_specific: true,
2193 shared_libs: [
2194 "libva",
2195 ],
2196 nocrt: true,
2197 }
2198 cc_library {
2199 name: "libva",
2200 vendor_available: true,
2201 nocrt: true,
2202 }
2203 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002204 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002205 cc_library {
2206 name: "libprod",
2207 product_specific: true,
2208 shared_libs: [
2209 "libvndk_private",
2210 ],
2211 nocrt: true,
2212 }
2213 cc_library {
2214 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002215 vendor_available: true,
2216 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002217 vndk: {
2218 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002219 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002220 },
2221 nocrt: true,
2222 }
2223 `)
2224 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2225 cc_library {
2226 name: "libprod",
2227 product_specific: true,
2228 shared_libs: [
2229 "libsystem_ext",
2230 ],
2231 nocrt: true,
2232 }
2233 cc_library {
2234 name: "libsystem_ext",
2235 system_ext_specific: true,
2236 nocrt: true,
2237 }
2238 `)
2239 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2240 cc_library {
2241 name: "libsystem",
2242 shared_libs: [
2243 "libproduct_va",
2244 ],
2245 nocrt: true,
2246 }
2247 cc_library {
2248 name: "libproduct_va",
2249 product_specific: true,
2250 vendor_available: true,
2251 nocrt: true,
2252 }
2253 `)
2254}
2255
Jooyung Han38002912019-05-16 04:01:54 +09002256func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002257 bp := `
2258 cc_library {
2259 name: "libvndk",
2260 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002261 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002262 vndk: {
2263 enabled: true,
2264 },
2265 }
2266 cc_library {
2267 name: "libvndksp",
2268 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002269 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002270 vndk: {
2271 enabled: true,
2272 support_system_process: true,
2273 },
2274 }
2275 cc_library {
2276 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002277 vendor_available: true,
2278 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002279 vndk: {
2280 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002281 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002282 },
2283 }
2284 cc_library {
2285 name: "libvendor",
2286 vendor: true,
2287 }
2288 cc_library {
2289 name: "libvndkext",
2290 vendor: true,
2291 vndk: {
2292 enabled: true,
2293 extends: "libvndk",
2294 },
2295 }
2296 vndk_prebuilt_shared {
2297 name: "prevndk",
2298 version: "27",
2299 target_arch: "arm",
2300 binder32bit: true,
2301 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002302 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002303 vndk: {
2304 enabled: true,
2305 },
2306 arch: {
2307 arm: {
2308 srcs: ["liba.so"],
2309 },
2310 },
2311 }
2312 cc_library {
2313 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002314 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002315 }
2316 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002317 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002318 symbol_file: "",
2319 }
2320 cc_library {
2321 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002322 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002323 }
2324 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002325 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002326 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002327 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002328 }
2329
2330 llndk_libraries_txt {
2331 name: "llndk.libraries.txt",
2332 }
2333 vndkcore_libraries_txt {
2334 name: "vndkcore.libraries.txt",
2335 }
2336 vndksp_libraries_txt {
2337 name: "vndksp.libraries.txt",
2338 }
2339 vndkprivate_libraries_txt {
2340 name: "vndkprivate.libraries.txt",
2341 }
2342 vndkcorevariant_libraries_txt {
2343 name: "vndkcorevariant.libraries.txt",
2344 insert_vndk_version: false,
2345 }
2346 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002347
2348 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002349 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2350 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2351 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002352 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002353
Colin Cross78212242021-01-06 14:51:30 -08002354 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2355 []string{"libvndk.so", "libvndkprivate.so"})
2356 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2357 []string{"libc++.so", "libvndksp.so"})
2358 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2359 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2360 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2361 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002362
Colin Crossfb0c16e2019-11-20 17:12:35 -08002363 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002364
Jooyung Han38002912019-05-16 04:01:54 +09002365 tests := []struct {
2366 variant string
2367 name string
2368 expected string
2369 }{
2370 {vendorVariant, "libvndk", "native:vndk"},
2371 {vendorVariant, "libvndksp", "native:vndk"},
2372 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2373 {vendorVariant, "libvendor", "native:vendor"},
2374 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002375 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002376 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002377 {coreVariant, "libvndk", "native:platform"},
2378 {coreVariant, "libvndkprivate", "native:platform"},
2379 {coreVariant, "libllndk", "native:platform"},
2380 }
2381 for _, test := range tests {
2382 t.Run(test.name, func(t *testing.T) {
2383 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2384 assertString(t, module.makeLinkType, test.expected)
2385 })
2386 }
2387}
2388
Jeff Gaston294356f2017-09-27 17:05:30 -07002389var staticLinkDepOrderTestCases = []struct {
2390 // This is a string representation of a map[moduleName][]moduleDependency .
2391 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002392 inStatic string
2393
2394 // This is a string representation of a map[moduleName][]moduleDependency .
2395 // It models the dependencies declared in an Android.bp file.
2396 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002397
2398 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2399 // The keys of allOrdered specify which modules we would like to check.
2400 // The values of allOrdered specify the expected result (of the transitive closure of all
2401 // dependencies) for each module to test
2402 allOrdered string
2403
2404 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2405 // The keys of outOrdered specify which modules we would like to check.
2406 // The values of outOrdered specify the expected result (of the ordered linker command line)
2407 // for each module to test.
2408 outOrdered string
2409}{
2410 // Simple tests
2411 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002412 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002413 outOrdered: "",
2414 },
2415 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002416 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002417 outOrdered: "a:",
2418 },
2419 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002420 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002421 outOrdered: "a:b; b:",
2422 },
2423 // Tests of reordering
2424 {
2425 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002426 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002427 outOrdered: "a:b,c,d; b:d; c:d; d:",
2428 },
2429 {
2430 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002431 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002432 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2433 },
2434 {
2435 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002436 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002437 outOrdered: "a:d,b,e,c; d:b; e:c",
2438 },
2439 {
2440 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002441 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002442 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2443 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2444 },
2445 {
2446 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002447 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 -07002448 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2449 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2450 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002451 // shared dependencies
2452 {
2453 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2454 // So, we don't actually have to check that a shared dependency of c will change the order
2455 // of a library that depends statically on b and on c. We only need to check that if c has
2456 // a shared dependency on b, that that shows up in allOrdered.
2457 inShared: "c:b",
2458 allOrdered: "c:b",
2459 outOrdered: "c:",
2460 },
2461 {
2462 // This test doesn't actually include any shared dependencies but it's a reminder of what
2463 // the second phase of the above test would look like
2464 inStatic: "a:b,c; c:b",
2465 allOrdered: "a:c,b; c:b",
2466 outOrdered: "a:c,b; c:b",
2467 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002468 // tiebreakers for when two modules specifying different orderings and there is no dependency
2469 // to dictate an order
2470 {
2471 // 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 -08002472 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002473 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2474 },
2475 {
2476 // 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 -08002477 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 -07002478 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2479 },
2480 // Tests involving duplicate dependencies
2481 {
2482 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002483 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002484 outOrdered: "a:c,b",
2485 },
2486 {
2487 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002488 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002489 outOrdered: "a:d,c,b",
2490 },
2491 // Tests to confirm the nonexistence of infinite loops.
2492 // These cases should never happen, so as long as the test terminates and the
2493 // result is deterministic then that should be fine.
2494 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002495 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002496 outOrdered: "a:a",
2497 },
2498 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002499 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002500 allOrdered: "a:b,c; b:c,a; c:a,b",
2501 outOrdered: "a:b; b:c; c:a",
2502 },
2503 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002504 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002505 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2506 outOrdered: "a:c,b; b:a,c; c:b,a",
2507 },
2508}
2509
2510// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2511func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2512 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2513 strippedText := strings.Replace(text, " ", "", -1)
2514 if len(strippedText) < 1 {
2515 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2516 }
2517 allDeps = make(map[android.Path][]android.Path, 0)
2518
2519 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2520 moduleTexts := strings.Split(strippedText, ";")
2521
2522 outputForModuleName := func(moduleName string) android.Path {
2523 return android.PathForTesting(moduleName)
2524 }
2525
2526 for _, moduleText := range moduleTexts {
2527 // convert from "a:b,c" to ["a", "b,c"]
2528 components := strings.Split(moduleText, ":")
2529 if len(components) != 2 {
2530 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2531 }
2532 moduleName := components[0]
2533 moduleOutput := outputForModuleName(moduleName)
2534 modulesInOrder = append(modulesInOrder, moduleOutput)
2535
2536 depString := components[1]
2537 // convert from "b,c" to ["b", "c"]
2538 depNames := strings.Split(depString, ",")
2539 if len(depString) < 1 {
2540 depNames = []string{}
2541 }
2542 var deps []android.Path
2543 for _, depName := range depNames {
2544 deps = append(deps, outputForModuleName(depName))
2545 }
2546 allDeps[moduleOutput] = deps
2547 }
2548 return modulesInOrder, allDeps
2549}
2550
Jeff Gaston294356f2017-09-27 17:05:30 -07002551func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2552 for _, moduleName := range moduleNames {
2553 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002554 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002555 paths = append(paths, output)
2556 }
2557 return paths
2558}
2559
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002560func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 ctx := testCc(t, `
2562 cc_library {
2563 name: "a",
2564 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002565 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002566 }
2567 cc_library {
2568 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002569 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002570 }
2571 cc_library {
2572 name: "c",
2573 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002574 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002575 }
2576 cc_library {
2577 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002578 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002579 }
2580
2581 `)
2582
Colin Cross7113d202019-11-20 16:39:12 -08002583 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002584 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002585 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2586 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002587 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002588
2589 if !reflect.DeepEqual(actual, expected) {
2590 t.Errorf("staticDeps orderings were not propagated correctly"+
2591 "\nactual: %v"+
2592 "\nexpected: %v",
2593 actual,
2594 expected,
2595 )
2596 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002597}
Jeff Gaston294356f2017-09-27 17:05:30 -07002598
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002599func TestStaticLibDepReorderingWithShared(t *testing.T) {
2600 ctx := testCc(t, `
2601 cc_library {
2602 name: "a",
2603 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002604 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002605 }
2606 cc_library {
2607 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002608 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002609 }
2610 cc_library {
2611 name: "c",
2612 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002613 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002614 }
2615
2616 `)
2617
Colin Cross7113d202019-11-20 16:39:12 -08002618 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002619 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002620 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2621 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002622 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002623
2624 if !reflect.DeepEqual(actual, expected) {
2625 t.Errorf("staticDeps orderings did not account for shared libs"+
2626 "\nactual: %v"+
2627 "\nexpected: %v",
2628 actual,
2629 expected,
2630 )
2631 }
2632}
2633
Jooyung Hanb04a4992020-03-13 18:57:35 +09002634func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002635 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002636 if !reflect.DeepEqual(actual, expected) {
2637 t.Errorf(message+
2638 "\nactual: %v"+
2639 "\nexpected: %v",
2640 actual,
2641 expected,
2642 )
2643 }
2644}
2645
Jooyung Han61b66e92020-03-21 14:21:46 +00002646func TestLlndkLibrary(t *testing.T) {
2647 ctx := testCc(t, `
2648 cc_library {
2649 name: "libllndk",
2650 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002651 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002652 }
2653 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002654 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002655 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002656
2657 cc_prebuilt_library_shared {
2658 name: "libllndkprebuilt",
2659 stubs: { versions: ["1", "2"] },
2660 llndk_stubs: "libllndkprebuilt.llndk",
2661 }
2662 llndk_library {
2663 name: "libllndkprebuilt.llndk",
2664 }
2665
2666 cc_library {
2667 name: "libllndk_with_external_headers",
2668 stubs: { versions: ["1", "2"] },
2669 llndk_stubs: "libllndk_with_external_headers.llndk",
2670 header_libs: ["libexternal_headers"],
2671 export_header_lib_headers: ["libexternal_headers"],
2672 }
2673 llndk_library {
2674 name: "libllndk_with_external_headers.llndk",
2675 }
2676 cc_library_headers {
2677 name: "libexternal_headers",
2678 export_include_dirs: ["include"],
2679 vendor_available: true,
2680 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002681 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002682 actual := ctx.ModuleVariantsForTests("libllndk")
2683 for i := 0; i < len(actual); i++ {
2684 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2685 actual = append(actual[:i], actual[i+1:]...)
2686 i--
2687 }
2688 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002689 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002690 "android_vendor.VER_arm64_armv8-a_shared_1",
2691 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002692 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002693 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2694 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002695 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002696 }
2697 checkEquals(t, "variants for llndk stubs", expected, actual)
2698
Colin Cross127bb8b2020-12-16 16:46:01 -08002699 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002700 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2701
Colin Cross127bb8b2020-12-16 16:46:01 -08002702 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002703 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2704}
2705
Jiyong Parka46a4d52017-12-14 19:54:34 +09002706func TestLlndkHeaders(t *testing.T) {
2707 ctx := testCc(t, `
2708 llndk_headers {
2709 name: "libllndk_headers",
2710 export_include_dirs: ["my_include"],
2711 }
2712 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002713 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002714 export_llndk_headers: ["libllndk_headers"],
2715 }
2716 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002717 name: "libllndk",
2718 llndk_stubs: "libllndk.llndk",
2719 }
2720
2721 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002722 name: "libvendor",
2723 shared_libs: ["libllndk"],
2724 vendor: true,
2725 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002726 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002727 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002728 }
2729 `)
2730
2731 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002732 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002733 cflags := cc.Args["cFlags"]
2734 if !strings.Contains(cflags, "-Imy_include") {
2735 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2736 }
2737}
2738
Logan Chien43d34c32017-12-20 01:17:32 +08002739func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2740 actual := module.Properties.AndroidMkRuntimeLibs
2741 if !reflect.DeepEqual(actual, expected) {
2742 t.Errorf("incorrect runtime_libs for shared libs"+
2743 "\nactual: %v"+
2744 "\nexpected: %v",
2745 actual,
2746 expected,
2747 )
2748 }
2749}
2750
2751const runtimeLibAndroidBp = `
2752 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002753 name: "liball_available",
2754 vendor_available: true,
2755 product_available: true,
2756 no_libcrt : true,
2757 nocrt : true,
2758 system_shared_libs : [],
2759 }
2760 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002761 name: "libvendor_available1",
2762 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002763 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002764 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002765 nocrt : true,
2766 system_shared_libs : [],
2767 }
2768 cc_library {
2769 name: "libvendor_available2",
2770 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002771 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002772 target: {
2773 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002774 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002775 }
2776 },
Yi Konge7fe9912019-06-02 00:53:50 -07002777 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002778 nocrt : true,
2779 system_shared_libs : [],
2780 }
2781 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002782 name: "libproduct_vendor",
2783 product_specific: true,
2784 vendor_available: true,
2785 no_libcrt : true,
2786 nocrt : true,
2787 system_shared_libs : [],
2788 }
2789 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002790 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002791 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002792 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002793 nocrt : true,
2794 system_shared_libs : [],
2795 }
2796 cc_library {
2797 name: "libvendor1",
2798 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002799 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002800 nocrt : true,
2801 system_shared_libs : [],
2802 }
2803 cc_library {
2804 name: "libvendor2",
2805 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002806 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002807 no_libcrt : true,
2808 nocrt : true,
2809 system_shared_libs : [],
2810 }
2811 cc_library {
2812 name: "libproduct_available1",
2813 product_available: true,
2814 runtime_libs: ["liball_available"],
2815 no_libcrt : true,
2816 nocrt : true,
2817 system_shared_libs : [],
2818 }
2819 cc_library {
2820 name: "libproduct1",
2821 product_specific: true,
2822 no_libcrt : true,
2823 nocrt : true,
2824 system_shared_libs : [],
2825 }
2826 cc_library {
2827 name: "libproduct2",
2828 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002829 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002830 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002831 nocrt : true,
2832 system_shared_libs : [],
2833 }
2834`
2835
2836func TestRuntimeLibs(t *testing.T) {
2837 ctx := testCc(t, runtimeLibAndroidBp)
2838
2839 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002840 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002841
Justin Yun8a2600c2020-12-07 12:44:03 +09002842 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2843 checkRuntimeLibs(t, []string{"liball_available"}, module)
2844
2845 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2846 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002847
2848 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002849 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002850
2851 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2852 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002853 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002854
Justin Yun8a2600c2020-12-07 12:44:03 +09002855 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2856 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002857
2858 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002859 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002860
2861 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2862 // and product variants.
2863 variant = "android_product.VER_arm64_armv8-a_shared"
2864
2865 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2866 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2867
2868 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002869 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002870}
2871
2872func TestExcludeRuntimeLibs(t *testing.T) {
2873 ctx := testCc(t, runtimeLibAndroidBp)
2874
Colin Cross7113d202019-11-20 16:39:12 -08002875 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002876 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2877 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002878
Colin Crossfb0c16e2019-11-20 17:12:35 -08002879 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002880 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002881 checkRuntimeLibs(t, nil, module)
2882}
2883
2884func TestRuntimeLibsNoVndk(t *testing.T) {
2885 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2886
2887 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2888
Colin Cross7113d202019-11-20 16:39:12 -08002889 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002890
Justin Yun8a2600c2020-12-07 12:44:03 +09002891 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2892 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002893
2894 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002895 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002896
2897 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002898 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002899}
2900
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002901func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002902 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002903 actual := module.Properties.AndroidMkStaticLibs
2904 if !reflect.DeepEqual(actual, expected) {
2905 t.Errorf("incorrect static_libs"+
2906 "\nactual: %v"+
2907 "\nexpected: %v",
2908 actual,
2909 expected,
2910 )
2911 }
2912}
2913
2914const staticLibAndroidBp = `
2915 cc_library {
2916 name: "lib1",
2917 }
2918 cc_library {
2919 name: "lib2",
2920 static_libs: ["lib1"],
2921 }
2922`
2923
2924func TestStaticLibDepExport(t *testing.T) {
2925 ctx := testCc(t, staticLibAndroidBp)
2926
2927 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002928 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002929 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002930 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002931
2932 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002933 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002934 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2935 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002936 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002937}
2938
Jiyong Parkd08b6972017-09-26 10:50:54 +09002939var compilerFlagsTestCases = []struct {
2940 in string
2941 out bool
2942}{
2943 {
2944 in: "a",
2945 out: false,
2946 },
2947 {
2948 in: "-a",
2949 out: true,
2950 },
2951 {
2952 in: "-Ipath/to/something",
2953 out: false,
2954 },
2955 {
2956 in: "-isystempath/to/something",
2957 out: false,
2958 },
2959 {
2960 in: "--coverage",
2961 out: false,
2962 },
2963 {
2964 in: "-include a/b",
2965 out: true,
2966 },
2967 {
2968 in: "-include a/b c/d",
2969 out: false,
2970 },
2971 {
2972 in: "-DMACRO",
2973 out: true,
2974 },
2975 {
2976 in: "-DMAC RO",
2977 out: false,
2978 },
2979 {
2980 in: "-a -b",
2981 out: false,
2982 },
2983 {
2984 in: "-DMACRO=definition",
2985 out: true,
2986 },
2987 {
2988 in: "-DMACRO=defi nition",
2989 out: true, // TODO(jiyong): this should be false
2990 },
2991 {
2992 in: "-DMACRO(x)=x + 1",
2993 out: true,
2994 },
2995 {
2996 in: "-DMACRO=\"defi nition\"",
2997 out: true,
2998 },
2999}
3000
3001type mockContext struct {
3002 BaseModuleContext
3003 result bool
3004}
3005
3006func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3007 // CheckBadCompilerFlags calls this function when the flag should be rejected
3008 ctx.result = false
3009}
3010
3011func TestCompilerFlags(t *testing.T) {
3012 for _, testCase := range compilerFlagsTestCases {
3013 ctx := &mockContext{result: true}
3014 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3015 if ctx.result != testCase.out {
3016 t.Errorf("incorrect output:")
3017 t.Errorf(" input: %#v", testCase.in)
3018 t.Errorf(" expected: %#v", testCase.out)
3019 t.Errorf(" got: %#v", ctx.result)
3020 }
3021 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003022}
Jiyong Park374510b2018-03-19 18:23:01 +09003023
3024func TestVendorPublicLibraries(t *testing.T) {
3025 ctx := testCc(t, `
3026 cc_library_headers {
3027 name: "libvendorpublic_headers",
3028 export_include_dirs: ["my_include"],
3029 }
3030 vendor_public_library {
3031 name: "libvendorpublic",
3032 symbol_file: "",
3033 export_public_headers: ["libvendorpublic_headers"],
3034 }
3035 cc_library {
3036 name: "libvendorpublic",
3037 srcs: ["foo.c"],
3038 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003039 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003040 nocrt: true,
3041 }
3042
3043 cc_library {
3044 name: "libsystem",
3045 shared_libs: ["libvendorpublic"],
3046 vendor: false,
3047 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003048 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003049 nocrt: true,
3050 }
3051 cc_library {
3052 name: "libvendor",
3053 shared_libs: ["libvendorpublic"],
3054 vendor: true,
3055 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003056 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003057 nocrt: true,
3058 }
3059 `)
3060
Colin Cross7113d202019-11-20 16:39:12 -08003061 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003062 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003063
3064 // test if header search paths are correctly added
3065 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003066 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003067 cflags := cc.Args["cFlags"]
3068 if !strings.Contains(cflags, "-Imy_include") {
3069 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3070 }
3071
3072 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003073 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003074 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003075 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003076 if !strings.Contains(libflags, stubPaths[0].String()) {
3077 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3078 }
3079
3080 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003081 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003082 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003083 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003084 if !strings.Contains(libflags, stubPaths[0].String()) {
3085 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3086 }
3087
3088}
Jiyong Park37b25202018-07-11 10:49:27 +09003089
3090func TestRecovery(t *testing.T) {
3091 ctx := testCc(t, `
3092 cc_library_shared {
3093 name: "librecovery",
3094 recovery: true,
3095 }
3096 cc_library_shared {
3097 name: "librecovery32",
3098 recovery: true,
3099 compile_multilib:"32",
3100 }
Jiyong Park5baac542018-08-28 09:55:37 +09003101 cc_library_shared {
3102 name: "libHalInRecovery",
3103 recovery_available: true,
3104 vendor: true,
3105 }
Jiyong Park37b25202018-07-11 10:49:27 +09003106 `)
3107
3108 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003109 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003110 if len(variants) != 1 || !android.InList(arm64, variants) {
3111 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3112 }
3113
3114 variants = ctx.ModuleVariantsForTests("librecovery32")
3115 if android.InList(arm64, variants) {
3116 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3117 }
Jiyong Park5baac542018-08-28 09:55:37 +09003118
3119 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3120 if !recoveryModule.Platform() {
3121 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3122 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003123}
Jiyong Park5baac542018-08-28 09:55:37 +09003124
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003125func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3126 bp := `
3127 cc_prebuilt_test_library_shared {
3128 name: "test_lib",
3129 relative_install_path: "foo/bar/baz",
3130 srcs: ["srcpath/dontusethispath/baz.so"],
3131 }
3132
3133 cc_test {
3134 name: "main_test",
3135 data_libs: ["test_lib"],
3136 gtest: false,
3137 }
3138 `
3139
3140 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3141 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3142 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3143 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3144
3145 ctx := testCcWithConfig(t, config)
3146 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3147 testBinary := module.(*Module).linker.(*testBinary)
3148 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3149 if err != nil {
3150 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3151 }
3152 if len(outputFiles) != 1 {
3153 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3154 }
3155 if len(testBinary.dataPaths()) != 1 {
3156 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3157 }
3158
3159 outputPath := outputFiles[0].String()
3160
3161 if !strings.HasSuffix(outputPath, "/main_test") {
3162 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3163 }
Colin Crossaa255532020-07-03 13:18:24 -07003164 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003165 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3166 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3167 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3168 }
3169}
3170
Jiyong Park7ed9de32018-10-15 22:25:07 +09003171func TestVersionedStubs(t *testing.T) {
3172 ctx := testCc(t, `
3173 cc_library_shared {
3174 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003175 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003176 stubs: {
3177 symbol_file: "foo.map.txt",
3178 versions: ["1", "2", "3"],
3179 },
3180 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003181
Jiyong Park7ed9de32018-10-15 22:25:07 +09003182 cc_library_shared {
3183 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003184 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003185 shared_libs: ["libFoo#1"],
3186 }`)
3187
3188 variants := ctx.ModuleVariantsForTests("libFoo")
3189 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003190 "android_arm64_armv8-a_shared",
3191 "android_arm64_armv8-a_shared_1",
3192 "android_arm64_armv8-a_shared_2",
3193 "android_arm64_armv8-a_shared_3",
3194 "android_arm_armv7-a-neon_shared",
3195 "android_arm_armv7-a-neon_shared_1",
3196 "android_arm_armv7-a-neon_shared_2",
3197 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003198 }
3199 variantsMismatch := false
3200 if len(variants) != len(expectedVariants) {
3201 variantsMismatch = true
3202 } else {
3203 for _, v := range expectedVariants {
3204 if !inList(v, variants) {
3205 variantsMismatch = false
3206 }
3207 }
3208 }
3209 if variantsMismatch {
3210 t.Errorf("variants of libFoo expected:\n")
3211 for _, v := range expectedVariants {
3212 t.Errorf("%q\n", v)
3213 }
3214 t.Errorf(", but got:\n")
3215 for _, v := range variants {
3216 t.Errorf("%q\n", v)
3217 }
3218 }
3219
Colin Cross7113d202019-11-20 16:39:12 -08003220 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003221 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003222 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003223 if !strings.Contains(libFlags, libFoo1StubPath) {
3224 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3225 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003226
Colin Cross7113d202019-11-20 16:39:12 -08003227 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003228 cFlags := libBarCompileRule.Args["cFlags"]
3229 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3230 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3231 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3232 }
Jiyong Park37b25202018-07-11 10:49:27 +09003233}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003234
Jooyung Hanb04a4992020-03-13 18:57:35 +09003235func TestVersioningMacro(t *testing.T) {
3236 for _, tc := range []struct{ moduleName, expected string }{
3237 {"libc", "__LIBC_API__"},
3238 {"libfoo", "__LIBFOO_API__"},
3239 {"libfoo@1", "__LIBFOO_1_API__"},
3240 {"libfoo-v1", "__LIBFOO_V1_API__"},
3241 {"libfoo.v1", "__LIBFOO_V1_API__"},
3242 } {
3243 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3244 }
3245}
3246
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003247func TestStaticExecutable(t *testing.T) {
3248 ctx := testCc(t, `
3249 cc_binary {
3250 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003251 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003252 static_executable: true,
3253 }`)
3254
Colin Cross7113d202019-11-20 16:39:12 -08003255 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003256 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3257 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003258 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003259 for _, lib := range systemStaticLibs {
3260 if !strings.Contains(libFlags, lib) {
3261 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3262 }
3263 }
3264 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3265 for _, lib := range systemSharedLibs {
3266 if strings.Contains(libFlags, lib) {
3267 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3268 }
3269 }
3270}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003271
3272func TestStaticDepsOrderWithStubs(t *testing.T) {
3273 ctx := testCc(t, `
3274 cc_binary {
3275 name: "mybin",
3276 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003277 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003278 static_executable: true,
3279 stl: "none",
3280 }
3281
3282 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003283 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003284 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003285 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003286 stl: "none",
3287 }
3288
3289 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003290 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003291 srcs: ["foo.c"],
3292 stl: "none",
3293 stubs: {
3294 versions: ["1"],
3295 },
3296 }`)
3297
Colin Cross0de8a1e2020-09-18 14:15:30 -07003298 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3299 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003300 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003301
3302 if !reflect.DeepEqual(actual, expected) {
3303 t.Errorf("staticDeps orderings were not propagated correctly"+
3304 "\nactual: %v"+
3305 "\nexpected: %v",
3306 actual,
3307 expected,
3308 )
3309 }
3310}
Jooyung Han38002912019-05-16 04:01:54 +09003311
Jooyung Hand48f3c32019-08-23 11:18:57 +09003312func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3313 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3314 cc_library {
3315 name: "libA",
3316 srcs: ["foo.c"],
3317 shared_libs: ["libB"],
3318 stl: "none",
3319 }
3320
3321 cc_library {
3322 name: "libB",
3323 srcs: ["foo.c"],
3324 enabled: false,
3325 stl: "none",
3326 }
3327 `)
3328}
3329
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003330// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3331// correctly.
3332func TestFuzzTarget(t *testing.T) {
3333 ctx := testCc(t, `
3334 cc_fuzz {
3335 name: "fuzz_smoke_test",
3336 srcs: ["foo.c"],
3337 }`)
3338
Paul Duffin075c4172019-12-19 19:06:13 +00003339 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003340 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3341}
3342
Jiyong Park29074592019-07-07 16:27:47 +09003343func TestAidl(t *testing.T) {
3344}
3345
Jooyung Han38002912019-05-16 04:01:54 +09003346func assertString(t *testing.T, got, expected string) {
3347 t.Helper()
3348 if got != expected {
3349 t.Errorf("expected %q got %q", expected, got)
3350 }
3351}
3352
3353func assertArrayString(t *testing.T, got, expected []string) {
3354 t.Helper()
3355 if len(got) != len(expected) {
3356 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3357 return
3358 }
3359 for i := range got {
3360 if got[i] != expected[i] {
3361 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3362 i, expected[i], expected, got[i], got)
3363 return
3364 }
3365 }
3366}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003367
Jooyung Han0302a842019-10-30 18:43:49 +09003368func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3369 t.Helper()
3370 assertArrayString(t, android.SortedStringKeys(m), expected)
3371}
3372
Colin Crosse1bb5d02019-09-24 14:55:04 -07003373func TestDefaults(t *testing.T) {
3374 ctx := testCc(t, `
3375 cc_defaults {
3376 name: "defaults",
3377 srcs: ["foo.c"],
3378 static: {
3379 srcs: ["bar.c"],
3380 },
3381 shared: {
3382 srcs: ["baz.c"],
3383 },
3384 }
3385
3386 cc_library_static {
3387 name: "libstatic",
3388 defaults: ["defaults"],
3389 }
3390
3391 cc_library_shared {
3392 name: "libshared",
3393 defaults: ["defaults"],
3394 }
3395
3396 cc_library {
3397 name: "libboth",
3398 defaults: ["defaults"],
3399 }
3400
3401 cc_binary {
3402 name: "binary",
3403 defaults: ["defaults"],
3404 }`)
3405
3406 pathsToBase := func(paths android.Paths) []string {
3407 var ret []string
3408 for _, p := range paths {
3409 ret = append(ret, p.Base())
3410 }
3411 return ret
3412 }
3413
Colin Cross7113d202019-11-20 16:39:12 -08003414 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003415 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3416 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3417 }
Colin Cross7113d202019-11-20 16:39:12 -08003418 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003419 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3420 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3421 }
Colin Cross7113d202019-11-20 16:39:12 -08003422 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003423 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3424 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3425 }
3426
Colin Cross7113d202019-11-20 16:39:12 -08003427 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003428 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3429 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3430 }
Colin Cross7113d202019-11-20 16:39:12 -08003431 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003432 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3433 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3434 }
3435}
Colin Crosseabaedd2020-02-06 17:01:55 -08003436
3437func TestProductVariableDefaults(t *testing.T) {
3438 bp := `
3439 cc_defaults {
3440 name: "libfoo_defaults",
3441 srcs: ["foo.c"],
3442 cppflags: ["-DFOO"],
3443 product_variables: {
3444 debuggable: {
3445 cppflags: ["-DBAR"],
3446 },
3447 },
3448 }
3449
3450 cc_library {
3451 name: "libfoo",
3452 defaults: ["libfoo_defaults"],
3453 }
3454 `
3455
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003456 result := ccFixtureFactory.Extend(
3457 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003458
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003459 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3460 variables.Debuggable = BoolPtr(true)
3461 }),
3462 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003463
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003464 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003465 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003466}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003467
3468func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3469 t.Parallel()
3470 bp := `
3471 cc_library_static {
3472 name: "libfoo",
3473 srcs: ["foo.c"],
3474 whole_static_libs: ["libbar"],
3475 }
3476
3477 cc_library_static {
3478 name: "libbar",
3479 whole_static_libs: ["libmissing"],
3480 }
3481 `
3482
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003483 result := ccFixtureFactory.Extend(
3484 android.PrepareForTestWithAllowMissingDependencies,
3485 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003486
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003487 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003488 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003489
Paul Duffine84b1332021-03-12 11:59:43 +00003490 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003491
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003492 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003493 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003494}
Colin Crosse9fe2942020-11-10 18:12:15 -08003495
3496func TestInstallSharedLibs(t *testing.T) {
3497 bp := `
3498 cc_binary {
3499 name: "bin",
3500 host_supported: true,
3501 shared_libs: ["libshared"],
3502 runtime_libs: ["libruntime"],
3503 srcs: [":gen"],
3504 }
3505
3506 cc_library_shared {
3507 name: "libshared",
3508 host_supported: true,
3509 shared_libs: ["libtransitive"],
3510 }
3511
3512 cc_library_shared {
3513 name: "libtransitive",
3514 host_supported: true,
3515 }
3516
3517 cc_library_shared {
3518 name: "libruntime",
3519 host_supported: true,
3520 }
3521
3522 cc_binary_host {
3523 name: "tool",
3524 srcs: ["foo.cpp"],
3525 }
3526
3527 genrule {
3528 name: "gen",
3529 tools: ["tool"],
3530 out: ["gen.cpp"],
3531 cmd: "$(location tool) $(out)",
3532 }
3533 `
3534
3535 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3536 ctx := testCcWithConfig(t, config)
3537
3538 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3539 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3540 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3541 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3542 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3543
3544 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3545 t.Errorf("expected host bin dependency %q, got %q", w, g)
3546 }
3547
3548 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3549 t.Errorf("expected host bin dependency %q, got %q", w, g)
3550 }
3551
3552 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3553 t.Errorf("expected host bin dependency %q, got %q", w, g)
3554 }
3555
3556 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3557 t.Errorf("expected host bin dependency %q, got %q", w, g)
3558 }
3559
3560 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3561 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3562 }
3563
3564 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3565 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3566 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3567 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3568
3569 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3570 t.Errorf("expected device bin dependency %q, got %q", w, g)
3571 }
3572
3573 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3574 t.Errorf("expected device bin dependency %q, got %q", w, g)
3575 }
3576
3577 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3578 t.Errorf("expected device bin dependency %q, got %q", w, g)
3579 }
3580
3581 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3582 t.Errorf("expected device bin dependency %q, got %q", w, g)
3583 }
3584
3585 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3586 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3587 }
3588
3589}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003590
3591func TestStubsLibReexportsHeaders(t *testing.T) {
3592 ctx := testCc(t, `
3593 cc_library_shared {
3594 name: "libclient",
3595 srcs: ["foo.c"],
3596 shared_libs: ["libfoo#1"],
3597 }
3598
3599 cc_library_shared {
3600 name: "libfoo",
3601 srcs: ["foo.c"],
3602 shared_libs: ["libbar"],
3603 export_shared_lib_headers: ["libbar"],
3604 stubs: {
3605 symbol_file: "foo.map.txt",
3606 versions: ["1", "2", "3"],
3607 },
3608 }
3609
3610 cc_library_shared {
3611 name: "libbar",
3612 export_include_dirs: ["include/libbar"],
3613 srcs: ["foo.c"],
3614 }`)
3615
3616 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3617
3618 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3619 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3620 }
3621}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003622
3623func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3624 ctx := testCc(t, `
3625 cc_library {
3626 name: "libfoo",
3627 srcs: ["a/Foo.aidl"],
3628 aidl: { flags: ["-Werror"], },
3629 }
3630 `)
3631
3632 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3633 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3634 aidlCommand := manifest.Commands[0].GetCommand()
3635 expectedAidlFlag := "-Werror"
3636 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3637 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3638 }
3639}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003640
Jiyong Parka008fb02021-03-16 17:15:53 +09003641func TestMinSdkVersionInClangTriple(t *testing.T) {
3642 ctx := testCc(t, `
3643 cc_library_shared {
3644 name: "libfoo",
3645 srcs: ["foo.c"],
3646 min_sdk_version: "29",
3647 }`)
3648
3649 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3650 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3651}
3652
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003653func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3654 ctx := testCc(t, `
3655 cc_object {
3656 name: "crt_foo",
3657 srcs: ["foo.c"],
3658 crt: true,
3659 stl: "none",
3660 min_sdk_version: "28",
3661
3662 }`)
3663
3664 arch := "android_arm64_armv8-a"
3665 for _, v := range []string{"", "28", "29", "30", "current"} {
3666 var variant string
3667 if v == "" {
3668 variant = arch
3669 } else {
3670 variant = arch + "_sdk_" + v
3671 }
3672 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3673 vNum := v
3674 if v == "current" || v == "" {
3675 vNum = "10000"
3676 }
3677 expected := "-target aarch64-linux-android" + vNum + " "
3678 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3679 }
3680}
3681
3682func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3683 ctx := testCc(t, `
3684 cc_binary {
3685 name: "bin",
3686 srcs: ["foo.c"],
3687 stl: "none",
3688 min_sdk_version: "29",
3689 sdk_version: "current",
3690 }
3691 `)
3692
3693 // Sdk variant uses the crt object of the matching min_sdk_version
3694 variant := "android_arm64_armv8-a_sdk"
3695 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3696 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3697 variant+"_29/crtbegin_dynamic.o")
3698
3699 // platform variant uses the crt object built for platform
3700 variant = "android_arm64_armv8-a"
3701 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3702 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3703 variant+"/crtbegin_dynamic.o")
3704}
3705
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003706type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003707
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003708const (
3709 None MemtagNoteType = iota + 1
3710 Sync
3711 Async
3712)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003713
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003714func (t MemtagNoteType) str() string {
3715 switch t {
3716 case None:
3717 return "none"
3718 case Sync:
3719 return "sync"
3720 case Async:
3721 return "async"
3722 default:
3723 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003724 }
3725}
3726
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003727func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3728 note_async := "note_memtag_heap_async"
3729 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003730
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003731 found := None
3732 implicits := m.Rule("ld").Implicits
3733 for _, lib := range implicits {
3734 if strings.Contains(lib.Rel(), note_async) {
3735 found = Async
3736 break
3737 } else if strings.Contains(lib.Rel(), note_sync) {
3738 found = Sync
3739 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003740 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003741 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003742
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003743 if found != expected {
3744 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3745 }
3746}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003747
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003748var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3749 android.FixtureModifyMockFS(func(fs android.MockFS) {
3750 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003751 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003752 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003753 gtest: false,
3754 }
3755
3756 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003757 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003758 gtest: false,
3759 sanitize: { memtag_heap: false },
3760 }
3761
3762 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003763 name: "%[1]s_test_true",
3764 gtest: false,
3765 sanitize: { memtag_heap: true },
3766 }
3767
3768 cc_test {
3769 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003770 gtest: false,
3771 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3772 }
3773
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003774 cc_test {
3775 name: "%[1]s_test_true_diag",
3776 gtest: false,
3777 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3778 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003779
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003780 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003781 name: "%[1]s_binary",
3782 }
3783
3784 cc_binary {
3785 name: "%[1]s_binary_false",
3786 sanitize: { memtag_heap: false },
3787 }
3788
3789 cc_binary {
3790 name: "%[1]s_binary_true",
3791 sanitize: { memtag_heap: true },
3792 }
3793
3794 cc_binary {
3795 name: "%[1]s_binary_true_nodiag",
3796 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3797 }
3798
3799 cc_binary {
3800 name: "%[1]s_binary_true_diag",
3801 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003802 }
3803 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003804 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3805 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3806 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3807 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003808
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003809 fs.Merge(android.MockFS{
3810 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3811 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3812 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3813 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3814 })
3815 }),
3816 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3817 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3818 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3819 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3820 }),
3821)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003822
3823func TestSanitizeMemtagHeap(t *testing.T) {
3824 variant := "android_arm64_armv8-a"
3825
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003826 result := ccFixtureFactory.Extend(prepareForTestWithMemtagHeap).RunTest(t)
3827 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003828
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003829 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3830 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3833 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3834
3835 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3839 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3840
3841 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3845 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3846
3847 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3851 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3852
3853 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3857 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3858
3859 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3862 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3863 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3864
3865 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3868 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3869 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3870
3871 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3874 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3875 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3876}
3877
3878func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003879 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003880
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003881 result := ccFixtureFactory.Extend(
3882 prepareForTestWithMemtagHeap,
3883 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3884 variables.SanitizeDevice = []string{"memtag_heap"}
3885 }),
3886 ).RunTest(t)
3887 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003888
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003889 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3893 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003894
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003895 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3896 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3899 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3900
3901 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3902 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3905 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3906
3907 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3911 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3912
3913 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3917 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3918
3919 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3923 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3924
3925 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3929 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3930
3931 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3932 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3934 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3935 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3936}
3937
3938func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3939 variant := "android_arm64_armv8-a"
3940
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003941 result := ccFixtureFactory.Extend(
3942 prepareForTestWithMemtagHeap,
3943 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3944 variables.SanitizeDevice = []string{"memtag_heap"}
3945 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3946 }),
3947 ).RunTest(t)
3948 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003949
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3955
3956 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3961
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3985
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3991
3992 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3995 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3996 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003997}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003998
3999func TestIncludeDirsExporting(t *testing.T) {
4000
4001 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4002 // embedded newline characters alone.
4003 trimIndentingSpaces := func(s string) string {
4004 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4005 }
4006
4007 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4008 t.Helper()
4009 expected = trimIndentingSpaces(expected)
4010 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4011 if expected != actual {
4012 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4013 }
4014 }
4015
4016 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4017
4018 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4019 t.Helper()
4020 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4021 name := module.Name()
4022
4023 for _, checker := range checkers {
4024 checker(t, name, exported)
4025 }
4026 }
4027
4028 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4029 return func(t *testing.T, name string, exported FlagExporterInfo) {
4030 t.Helper()
4031 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4032 }
4033 }
4034
4035 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4036 return func(t *testing.T, name string, exported FlagExporterInfo) {
4037 t.Helper()
4038 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4039 }
4040 }
4041
4042 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4043 return func(t *testing.T, name string, exported FlagExporterInfo) {
4044 t.Helper()
4045 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4046 }
4047 }
4048
4049 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4050 return func(t *testing.T, name string, exported FlagExporterInfo) {
4051 t.Helper()
4052 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4053 }
4054 }
4055
4056 genRuleModules := `
4057 genrule {
4058 name: "genrule_foo",
4059 cmd: "generate-foo",
4060 out: [
4061 "generated_headers/foo/generated_header.h",
4062 ],
4063 export_include_dirs: [
4064 "generated_headers",
4065 ],
4066 }
4067
4068 genrule {
4069 name: "genrule_bar",
4070 cmd: "generate-bar",
4071 out: [
4072 "generated_headers/bar/generated_header.h",
4073 ],
4074 export_include_dirs: [
4075 "generated_headers",
4076 ],
4077 }
4078 `
4079
4080 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4081 ctx := testCc(t, genRuleModules+`
4082 cc_library {
4083 name: "libfoo",
4084 srcs: ["foo.c"],
4085 export_include_dirs: ["foo/standard"],
4086 export_system_include_dirs: ["foo/system"],
4087 generated_headers: ["genrule_foo"],
4088 export_generated_headers: ["genrule_foo"],
4089 }
4090
4091 cc_library {
4092 name: "libbar",
4093 srcs: ["bar.c"],
4094 shared_libs: ["libfoo"],
4095 export_include_dirs: ["bar/standard"],
4096 export_system_include_dirs: ["bar/system"],
4097 generated_headers: ["genrule_bar"],
4098 export_generated_headers: ["genrule_bar"],
4099 }
4100 `)
4101 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4102 checkIncludeDirs(t, ctx, foo,
4103 expectedIncludeDirs(`
4104 foo/standard
4105 .intermediates/genrule_foo/gen/generated_headers
4106 `),
4107 expectedSystemIncludeDirs(`foo/system`),
4108 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4109 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4110 )
4111
4112 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4113 checkIncludeDirs(t, ctx, bar,
4114 expectedIncludeDirs(`
4115 bar/standard
4116 .intermediates/genrule_bar/gen/generated_headers
4117 `),
4118 expectedSystemIncludeDirs(`bar/system`),
4119 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4120 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4121 )
4122 })
4123
4124 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4125 ctx := testCc(t, genRuleModules+`
4126 cc_library {
4127 name: "libfoo",
4128 srcs: ["foo.c"],
4129 export_include_dirs: ["foo/standard"],
4130 export_system_include_dirs: ["foo/system"],
4131 generated_headers: ["genrule_foo"],
4132 export_generated_headers: ["genrule_foo"],
4133 }
4134
4135 cc_library {
4136 name: "libbar",
4137 srcs: ["bar.c"],
4138 whole_static_libs: ["libfoo"],
4139 export_include_dirs: ["bar/standard"],
4140 export_system_include_dirs: ["bar/system"],
4141 generated_headers: ["genrule_bar"],
4142 export_generated_headers: ["genrule_bar"],
4143 }
4144 `)
4145 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4146 checkIncludeDirs(t, ctx, foo,
4147 expectedIncludeDirs(`
4148 foo/standard
4149 .intermediates/genrule_foo/gen/generated_headers
4150 `),
4151 expectedSystemIncludeDirs(`foo/system`),
4152 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4153 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4154 )
4155
4156 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4157 checkIncludeDirs(t, ctx, bar,
4158 expectedIncludeDirs(`
4159 bar/standard
4160 foo/standard
4161 .intermediates/genrule_foo/gen/generated_headers
4162 .intermediates/genrule_bar/gen/generated_headers
4163 `),
4164 expectedSystemIncludeDirs(`
4165 bar/system
4166 foo/system
4167 `),
4168 expectedGeneratedHeaders(`
4169 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4170 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4171 `),
4172 expectedOrderOnlyDeps(`
4173 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4174 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4175 `),
4176 )
4177 })
4178
Paul Duffin3cb603e2021-02-19 13:57:10 +00004179 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4180 ctx := testCc(t, genRuleModules+`
4181 cc_library_shared {
4182 name: "libfoo",
4183 srcs: [
4184 "foo.c",
4185 "b.aidl",
4186 "a.proto",
4187 ],
4188 aidl: {
4189 export_aidl_headers: true,
4190 }
4191 }
4192 `)
4193 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4194 checkIncludeDirs(t, ctx, foo,
4195 expectedIncludeDirs(`
4196 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4197 `),
4198 expectedSystemIncludeDirs(``),
4199 expectedGeneratedHeaders(`
4200 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4201 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4202 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004203 `),
4204 expectedOrderOnlyDeps(`
4205 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4206 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4207 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004208 `),
4209 )
4210 })
4211
Paul Duffin3cb603e2021-02-19 13:57:10 +00004212 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4213 ctx := testCc(t, genRuleModules+`
4214 cc_library_shared {
4215 name: "libfoo",
4216 srcs: [
4217 "foo.c",
4218 "b.aidl",
4219 "a.proto",
4220 ],
4221 proto: {
4222 export_proto_headers: true,
4223 }
4224 }
4225 `)
4226 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4227 checkIncludeDirs(t, ctx, foo,
4228 expectedIncludeDirs(`
4229 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4230 `),
4231 expectedSystemIncludeDirs(``),
4232 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004233 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4234 `),
4235 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004236 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4237 `),
4238 )
4239 })
4240
Paul Duffin33056e82021-02-19 13:49:08 +00004241 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004242 ctx := testCc(t, genRuleModules+`
4243 cc_library_shared {
4244 name: "libfoo",
4245 srcs: [
4246 "foo.c",
4247 "a.sysprop",
4248 "b.aidl",
4249 "a.proto",
4250 ],
4251 }
4252 `)
4253 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4254 checkIncludeDirs(t, ctx, foo,
4255 expectedIncludeDirs(`
4256 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4257 `),
4258 expectedSystemIncludeDirs(``),
4259 expectedGeneratedHeaders(`
4260 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004261 `),
4262 expectedOrderOnlyDeps(`
4263 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4264 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004265 `),
4266 )
4267 })
4268}