blob: ab4ef6cffb1cc81d47af15b4a45724478a811535 [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,
Paul Duffin2e6f90e2021-03-22 23:20:25 +000057 prepareForCcTest,
58)
59
60var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000061 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000062 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
63 variables.DeviceVndkVersion = StringPtr("current")
64 variables.ProductVndkVersion = StringPtr("current")
65 variables.Platform_vndk_version = StringPtr("VER")
66 }),
67)
68
69// testCcWithConfig runs tests using the ccFixtureFactory
70//
71// See testCc for an explanation as to how to stop using this deprecated method.
72//
73// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080074func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070075 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000076 result := ccFixtureFactory.RunTestWithConfig(t, config)
77 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090078}
79
Paul Duffin02a3d652021-02-24 18:51:54 +000080// testCc runs tests using the ccFixtureFactory
81//
82// Do not add any new usages of this, instead use the ccFixtureFactory directly as it makes it much
83// easier to customize the test behavior.
84//
85// If it is necessary to customize the behavior of an existing test that uses this then please first
86// convert the test to using ccFixtureFactory first and then in a following change add the
87// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
88// that it did not change the test behavior unexpectedly.
89//
90// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080091func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080092 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000093 result := ccFixtureFactory.RunTestWithBp(t, bp)
94 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080095}
96
Paul Duffin02a3d652021-02-24 18:51:54 +000097// testCcNoVndk runs tests using the ccFixtureFactory
98//
99// See testCc for an explanation as to how to stop using this deprecated method.
100//
101// deprecated
Logan Chienf3511742017-10-31 18:04:35 +0800102func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800104 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700105 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800106
Colin Cross98be1bb2019-12-13 20:41:13 -0800107 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800108}
109
Paul Duffin02a3d652021-02-24 18:51:54 +0000110// testCcNoProductVndk runs tests using the ccFixtureFactory
111//
112// See testCc for an explanation as to how to stop using this deprecated method.
113//
114// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900115func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
116 t.Helper()
117 config := TestConfig(buildDir, android.Android, nil, bp, nil)
118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120
121 return testCcWithConfig(t, config)
122}
123
Paul Duffin02a3d652021-02-24 18:51:54 +0000124// testCcErrorWithConfig runs tests using the ccFixtureFactory
125//
126// See testCc for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800130 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800131
Paul Duffin02a3d652021-02-24 18:51:54 +0000132 ccFixtureFactory.Extend().
133 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
134 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800135}
136
Paul Duffin02a3d652021-02-24 18:51:54 +0000137// testCcError runs tests using the ccFixtureFactory
138//
139// See testCc for an explanation as to how to stop using this deprecated method.
140//
141// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900142func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900143 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900144 config := TestConfig(buildDir, android.Android, nil, bp, nil)
145 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
146 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
147 testCcErrorWithConfig(t, pattern, config)
148 return
149}
150
Paul Duffin02a3d652021-02-24 18:51:54 +0000151// testCcErrorProductVndk runs tests using the ccFixtureFactory
152//
153// See testCc for an explanation as to how to stop using this deprecated method.
154//
155// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900156func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900157 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900158 config := TestConfig(buildDir, android.Android, nil, bp, nil)
159 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
160 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
161 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
162 testCcErrorWithConfig(t, pattern, config)
163 return
164}
165
Logan Chienf3511742017-10-31 18:04:35 +0800166const (
Colin Cross7113d202019-11-20 16:39:12 -0800167 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800168 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900169 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800170 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800171)
172
Paul Duffindb462dd2021-03-21 22:01:55 +0000173// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
174// running it in a fixture that requires all source files to exist.
175func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
176 android.GroupFixturePreparers(
177 PrepareForTestWithCcDefaultModules,
178 android.PrepareForTestDisallowNonExistentPaths,
179 ).RunTest(t)
180}
181
Doug Hornc32c6b02019-01-17 14:44:05 -0800182func TestFuchsiaDeps(t *testing.T) {
183 t.Helper()
184
185 bp := `
186 cc_library {
187 name: "libTest",
188 srcs: ["foo.c"],
189 target: {
190 fuchsia: {
191 srcs: ["bar.c"],
192 },
193 },
194 }`
195
Paul Duffinecdac8a2021-02-24 19:18:42 +0000196 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800197
198 rt := false
199 fb := false
200
Paul Duffinecdac8a2021-02-24 19:18:42 +0000201 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800202 implicits := ld.Implicits
203 for _, lib := range implicits {
204 if strings.Contains(lib.Rel(), "libcompiler_rt") {
205 rt = true
206 }
207
208 if strings.Contains(lib.Rel(), "libbioniccompat") {
209 fb = true
210 }
211 }
212
213 if !rt || !fb {
214 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
215 }
216}
217
218func TestFuchsiaTargetDecl(t *testing.T) {
219 t.Helper()
220
221 bp := `
222 cc_library {
223 name: "libTest",
224 srcs: ["foo.c"],
225 target: {
226 fuchsia: {
227 srcs: ["bar.c"],
228 },
229 },
230 }`
231
Paul Duffinecdac8a2021-02-24 19:18:42 +0000232 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
233 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800234 var objs []string
235 for _, o := range ld.Inputs {
236 objs = append(objs, o.Base())
237 }
Paul Duffine84b1332021-03-12 11:59:43 +0000238 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800239}
240
Jiyong Park6a43f042017-10-12 23:05:00 +0900241func TestVendorSrc(t *testing.T) {
242 ctx := testCc(t, `
243 cc_library {
244 name: "libTest",
245 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700246 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800247 nocrt: true,
248 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900249 vendor_available: true,
250 target: {
251 vendor: {
252 srcs: ["bar.c"],
253 },
254 },
255 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900256 `)
257
Logan Chienf3511742017-10-31 18:04:35 +0800258 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900259 var objs []string
260 for _, o := range ld.Inputs {
261 objs = append(objs, o.Base())
262 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800263 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900264 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
265 }
266}
267
Logan Chienf3511742017-10-31 18:04:35 +0800268func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900269 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800270
Logan Chiend3c59a22018-03-29 14:08:15 +0800271 t.Helper()
272
Justin Yun0ecf0b22020-02-28 15:07:59 +0900273 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800274
275 // Check library properties.
276 lib, ok := mod.compiler.(*libraryDecorator)
277 if !ok {
278 t.Errorf("%q must have libraryDecorator", name)
279 } else if lib.baseInstaller.subDir != subDir {
280 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
281 lib.baseInstaller.subDir)
282 }
283
284 // Check VNDK properties.
285 if mod.vndkdep == nil {
286 t.Fatalf("%q must have `vndkdep`", name)
287 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700288 if !mod.IsVndk() {
289 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800290 }
291 if mod.isVndkSp() != isVndkSp {
292 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
293 }
294
295 // Check VNDK extension properties.
296 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500297 if mod.IsVndkExt() != isVndkExt {
298 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800299 }
300
301 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
302 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
303 }
304}
305
Jose Galmes0a942a02021-02-03 14:23:15 -0800306func 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 -0700307 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000308 mod := ctx.ModuleForTests(moduleName, variant)
309 outputFiles := mod.OutputFiles(t, "")
310 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900311 t.Errorf("%q must have single output\n", moduleName)
312 return
313 }
314 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900315
Bill Peckham945441c2020-08-31 16:07:58 -0700316 if include {
317 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800318 if fake {
319 if out.Rule == nil {
320 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
321 }
322 } else {
323 if out.Input.String() != outputFiles[0].String() {
324 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
325 }
Bill Peckham945441c2020-08-31 16:07:58 -0700326 }
327 } else {
328 out := singleton.MaybeOutput(snapshotPath)
329 if out.Rule != nil {
330 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
331 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900332 }
333}
334
Bill Peckham945441c2020-08-31 16:07:58 -0700335func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000336 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800337 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700338}
339
340func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000341 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800342 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
343}
344
345func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000346 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800347 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700348}
349
Jooyung Han2216fb12019-11-06 16:46:15 +0900350func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
351 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800352 content := android.ContentFromFileRuleForTests(t, params)
353 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900354 assertArrayString(t, actual, expected)
355}
356
Jooyung Han097087b2019-10-22 19:32:18 +0900357func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
358 t.Helper()
359 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900360 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
361}
362
363func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
364 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800365 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
366 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900367}
368
Logan Chienf3511742017-10-31 18:04:35 +0800369func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800370 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800371 cc_library {
372 name: "libvndk",
373 vendor_available: true,
374 vndk: {
375 enabled: true,
376 },
377 nocrt: true,
378 }
379
380 cc_library {
381 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900382 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800383 vndk: {
384 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900385 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800386 },
387 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900388 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800389 }
390
391 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900392 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800393 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900394 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800395 vndk: {
396 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900397 },
398 nocrt: true,
399 target: {
400 vendor: {
401 cflags: ["-DTEST"],
402 },
403 product: {
404 cflags: ["-DTEST"],
405 },
406 },
407 }
408
409 cc_library {
410 name: "libvndk_sp",
411 vendor_available: true,
412 vndk: {
413 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800414 support_system_process: true,
415 },
416 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900417 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800418 }
419
420 cc_library {
421 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900422 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800423 vndk: {
424 enabled: true,
425 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900426 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800427 },
428 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900429 target: {
430 vendor: {
431 suffix: "-x",
432 },
433 },
Logan Chienf3511742017-10-31 18:04:35 +0800434 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900435
436 cc_library {
437 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900438 vendor_available: true,
439 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900440 vndk: {
441 enabled: true,
442 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900443 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900444 },
445 nocrt: true,
446 target: {
447 vendor: {
448 suffix: "-x",
449 },
450 product: {
451 suffix: "-x",
452 },
453 },
454 }
455
Colin Crosse4e44bc2020-12-28 13:50:21 -0800456 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900457 name: "llndk.libraries.txt",
458 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800459 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900460 name: "vndkcore.libraries.txt",
461 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800462 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900463 name: "vndksp.libraries.txt",
464 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800465 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900466 name: "vndkprivate.libraries.txt",
467 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800468 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900469 name: "vndkproduct.libraries.txt",
470 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800471 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900472 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800473 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900474 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800475 `
476
477 config := TestConfig(buildDir, android.Android, nil, bp, nil)
478 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900479 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800480 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
481
482 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800483
Jooyung Han261e1582020-10-20 18:54:21 +0900484 // subdir == "" because VNDK libs are not supposed to be installed separately.
485 // They are installed as part of VNDK APEX instead.
486 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
487 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900488 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900489 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
490 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900491 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900492
Justin Yun6977e8a2020-10-29 18:24:11 +0900493 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
494 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900495
Inseob Kim1f086e22019-05-09 13:29:15 +0900496 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900497 snapshotDir := "vndk-snapshot"
498 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
499
500 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
501 "arm64", "armv8-a"))
502 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
503 "arm", "armv7-a-neon"))
504
505 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
506 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
507 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
508 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
509
Colin Crossfb0c16e2019-11-20 17:12:35 -0800510 variant := "android_vendor.VER_arm64_armv8-a_shared"
511 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900512
Inseob Kim7f283f42020-06-01 21:53:49 +0900513 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
514
515 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
516 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900517 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
518 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900519 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
520 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900521
Jooyung Han39edb6c2019-11-06 16:53:07 +0900522 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900523 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
524 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
525 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
526 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900527 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900528
Jooyung Han097087b2019-10-22 19:32:18 +0900529 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
530 "LLNDK: libc.so",
531 "LLNDK: libdl.so",
532 "LLNDK: libft2.so",
533 "LLNDK: libm.so",
534 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900535 "VNDK-SP: libvndk_sp-x.so",
536 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900538 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900539 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900540 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900541 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900542 "VNDK-private: libvndk-private.so",
543 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900544 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900545 "VNDK-product: libc++.so",
546 "VNDK-product: libvndk_product.so",
547 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900548 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900549 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900550 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
551 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
552 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 +0900553 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 +0900554 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
555}
556
Yo Chiangbba545e2020-06-09 16:15:37 +0800557func TestVndkWithHostSupported(t *testing.T) {
558 ctx := testCc(t, `
559 cc_library {
560 name: "libvndk_host_supported",
561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900562 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800563 vndk: {
564 enabled: true,
565 },
566 host_supported: true,
567 }
568
569 cc_library {
570 name: "libvndk_host_supported_but_disabled_on_device",
571 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900572 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800573 vndk: {
574 enabled: true,
575 },
576 host_supported: true,
577 enabled: false,
578 target: {
579 host: {
580 enabled: true,
581 }
582 }
583 }
584
Colin Crosse4e44bc2020-12-28 13:50:21 -0800585 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800586 name: "vndkcore.libraries.txt",
587 }
588 `)
589
590 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
591}
592
Jooyung Han2216fb12019-11-06 16:46:15 +0900593func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800594 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800595 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900596 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800597 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800598 }`
599 config := TestConfig(buildDir, android.Android, nil, bp, nil)
600 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
601 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
602 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900603
604 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700605 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900606 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900607}
608
609func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800610 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900611 cc_library {
612 name: "libvndk",
613 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900614 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900615 vndk: {
616 enabled: true,
617 },
618 nocrt: true,
619 }
620
621 cc_library {
622 name: "libvndk_sp",
623 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900624 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900625 vndk: {
626 enabled: true,
627 support_system_process: true,
628 },
629 nocrt: true,
630 }
631
632 cc_library {
633 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900634 vendor_available: true,
635 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900636 vndk: {
637 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900638 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900639 },
640 nocrt: true,
641 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900642
Colin Crosse4e44bc2020-12-28 13:50:21 -0800643 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900644 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800645 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900646 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800647 `
648
649 config := TestConfig(buildDir, android.Android, nil, bp, nil)
650 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
651 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
652 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
653
654 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
655
656 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900657
Jooyung Han2216fb12019-11-06 16:46:15 +0900658 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900659}
660
Chris Parsons79d66a52020-06-05 17:26:16 -0400661func TestDataLibs(t *testing.T) {
662 bp := `
663 cc_test_library {
664 name: "test_lib",
665 srcs: ["test_lib.cpp"],
666 gtest: false,
667 }
668
669 cc_test {
670 name: "main_test",
671 data_libs: ["test_lib"],
672 gtest: false,
673 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400674 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400675
676 config := TestConfig(buildDir, android.Android, nil, bp, nil)
677 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
678 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
679 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
680
681 ctx := testCcWithConfig(t, config)
682 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
683 testBinary := module.(*Module).linker.(*testBinary)
684 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
685 if err != nil {
686 t.Errorf("Expected cc_test to produce output files, error: %s", err)
687 return
688 }
689 if len(outputFiles) != 1 {
690 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
691 return
692 }
693 if len(testBinary.dataPaths()) != 1 {
694 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
695 return
696 }
697
698 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400699 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400700
701 if !strings.HasSuffix(outputPath, "/main_test") {
702 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
703 return
704 }
705 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
706 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
707 return
708 }
709}
710
Chris Parsons216e10a2020-07-09 17:12:52 -0400711func TestDataLibsRelativeInstallPath(t *testing.T) {
712 bp := `
713 cc_test_library {
714 name: "test_lib",
715 srcs: ["test_lib.cpp"],
716 relative_install_path: "foo/bar/baz",
717 gtest: false,
718 }
719
720 cc_test {
721 name: "main_test",
722 data_libs: ["test_lib"],
723 gtest: false,
724 }
725 `
726
727 config := TestConfig(buildDir, android.Android, nil, bp, nil)
728 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
729 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
730 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
731
732 ctx := testCcWithConfig(t, config)
733 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
734 testBinary := module.(*Module).linker.(*testBinary)
735 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
736 if err != nil {
737 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
738 }
739 if len(outputFiles) != 1 {
740 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
741 }
742 if len(testBinary.dataPaths()) != 1 {
743 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
744 }
745
746 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400747
748 if !strings.HasSuffix(outputPath, "/main_test") {
749 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
750 }
Colin Crossaa255532020-07-03 13:18:24 -0700751 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400752 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
753 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400754 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400755 }
756}
757
Jooyung Han0302a842019-10-30 18:43:49 +0900758func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900759 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900760 cc_library {
761 name: "libvndk",
762 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900763 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900764 vndk: {
765 enabled: true,
766 },
767 nocrt: true,
768 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900769 cc_library {
770 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900771 vendor_available: true,
772 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900773 vndk: {
774 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900775 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900776 },
777 nocrt: true,
778 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800779
780 cc_library {
781 name: "libllndk",
782 llndk_stubs: "libllndk.llndk",
783 }
784
785 llndk_library {
786 name: "libllndk.llndk",
787 symbol_file: "",
788 export_llndk_headers: ["libllndk_headers"],
789 }
790
791 llndk_headers {
792 name: "libllndk_headers",
793 export_include_dirs: ["include"],
794 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900795 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900796
797 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
798 "LLNDK: libc.so",
799 "LLNDK: libdl.so",
800 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800801 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900802 "LLNDK: libm.so",
803 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900804 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900805 "VNDK-core: libvndk.so",
806 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900807 "VNDK-private: libvndk-private.so",
808 "VNDK-product: libc++.so",
809 "VNDK-product: libvndk-private.so",
810 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900811 })
Logan Chienf3511742017-10-31 18:04:35 +0800812}
813
Justin Yun63e9ec72020-10-29 16:49:43 +0900814func TestVndkModuleError(t *testing.T) {
815 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900816 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900817 cc_library {
818 name: "libvndk",
819 vndk: {
820 enabled: true,
821 },
822 nocrt: true,
823 }
824 `)
825
Justin Yunc0d8c492021-01-07 17:45:31 +0900826 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900827 cc_library {
828 name: "libvndk",
829 product_available: true,
830 vndk: {
831 enabled: true,
832 },
833 nocrt: true,
834 }
835 `)
836
Justin Yun6977e8a2020-10-29 18:24:11 +0900837 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
838 cc_library {
839 name: "libvndkprop",
840 vendor_available: true,
841 product_available: true,
842 vndk: {
843 enabled: true,
844 },
845 nocrt: true,
846 target: {
847 vendor: {
848 cflags: ["-DTEST",],
849 },
850 },
851 }
852 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900853}
854
Logan Chiend3c59a22018-03-29 14:08:15 +0800855func TestVndkDepError(t *testing.T) {
856 // Check whether an error is emitted when a VNDK lib depends on a system lib.
857 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
858 cc_library {
859 name: "libvndk",
860 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900861 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800862 vndk: {
863 enabled: true,
864 },
865 shared_libs: ["libfwk"], // Cause error
866 nocrt: true,
867 }
868
869 cc_library {
870 name: "libfwk",
871 nocrt: true,
872 }
873 `)
874
875 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
876 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
877 cc_library {
878 name: "libvndk",
879 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900880 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800881 vndk: {
882 enabled: true,
883 },
884 shared_libs: ["libvendor"], // Cause error
885 nocrt: true,
886 }
887
888 cc_library {
889 name: "libvendor",
890 vendor: true,
891 nocrt: true,
892 }
893 `)
894
895 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
896 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
897 cc_library {
898 name: "libvndk_sp",
899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900900 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800901 vndk: {
902 enabled: true,
903 support_system_process: true,
904 },
905 shared_libs: ["libfwk"], // Cause error
906 nocrt: true,
907 }
908
909 cc_library {
910 name: "libfwk",
911 nocrt: true,
912 }
913 `)
914
915 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
916 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
917 cc_library {
918 name: "libvndk_sp",
919 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900920 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800921 vndk: {
922 enabled: true,
923 support_system_process: true,
924 },
925 shared_libs: ["libvendor"], // Cause error
926 nocrt: true,
927 }
928
929 cc_library {
930 name: "libvendor",
931 vendor: true,
932 nocrt: true,
933 }
934 `)
935
936 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
937 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
938 cc_library {
939 name: "libvndk_sp",
940 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900941 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800942 vndk: {
943 enabled: true,
944 support_system_process: true,
945 },
946 shared_libs: ["libvndk"], // Cause error
947 nocrt: true,
948 }
949
950 cc_library {
951 name: "libvndk",
952 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900953 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800954 vndk: {
955 enabled: true,
956 },
957 nocrt: true,
958 }
959 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900960
961 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
962 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
963 cc_library {
964 name: "libvndk",
965 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900966 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900967 vndk: {
968 enabled: true,
969 },
970 shared_libs: ["libnonvndk"],
971 nocrt: true,
972 }
973
974 cc_library {
975 name: "libnonvndk",
976 vendor_available: true,
977 nocrt: true,
978 }
979 `)
980
981 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
982 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
983 cc_library {
984 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900985 vendor_available: true,
986 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900987 vndk: {
988 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900989 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900990 },
991 shared_libs: ["libnonvndk"],
992 nocrt: true,
993 }
994
995 cc_library {
996 name: "libnonvndk",
997 vendor_available: true,
998 nocrt: true,
999 }
1000 `)
1001
1002 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1003 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1004 cc_library {
1005 name: "libvndksp",
1006 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001007 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001008 vndk: {
1009 enabled: true,
1010 support_system_process: true,
1011 },
1012 shared_libs: ["libnonvndk"],
1013 nocrt: true,
1014 }
1015
1016 cc_library {
1017 name: "libnonvndk",
1018 vendor_available: true,
1019 nocrt: true,
1020 }
1021 `)
1022
1023 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1024 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1025 cc_library {
1026 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001027 vendor_available: true,
1028 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001029 vndk: {
1030 enabled: true,
1031 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001032 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001033 },
1034 shared_libs: ["libnonvndk"],
1035 nocrt: true,
1036 }
1037
1038 cc_library {
1039 name: "libnonvndk",
1040 vendor_available: true,
1041 nocrt: true,
1042 }
1043 `)
1044}
1045
1046func TestDoubleLoadbleDep(t *testing.T) {
1047 // okay to link : LLNDK -> double_loadable VNDK
1048 testCc(t, `
1049 cc_library {
1050 name: "libllndk",
1051 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001052 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001053 }
1054
1055 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001056 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001057 symbol_file: "",
1058 }
1059
1060 cc_library {
1061 name: "libdoubleloadable",
1062 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001063 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001064 vndk: {
1065 enabled: true,
1066 },
1067 double_loadable: true,
1068 }
1069 `)
1070 // okay to link : LLNDK -> VNDK-SP
1071 testCc(t, `
1072 cc_library {
1073 name: "libllndk",
1074 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001075 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001076 }
1077
1078 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001079 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001080 symbol_file: "",
1081 }
1082
1083 cc_library {
1084 name: "libvndksp",
1085 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001086 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001087 vndk: {
1088 enabled: true,
1089 support_system_process: true,
1090 },
1091 }
1092 `)
1093 // okay to link : double_loadable -> double_loadable
1094 testCc(t, `
1095 cc_library {
1096 name: "libdoubleloadable1",
1097 shared_libs: ["libdoubleloadable2"],
1098 vendor_available: true,
1099 double_loadable: true,
1100 }
1101
1102 cc_library {
1103 name: "libdoubleloadable2",
1104 vendor_available: true,
1105 double_loadable: true,
1106 }
1107 `)
1108 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1109 testCc(t, `
1110 cc_library {
1111 name: "libdoubleloadable",
1112 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001113 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001114 vndk: {
1115 enabled: true,
1116 },
1117 double_loadable: true,
1118 shared_libs: ["libnondoubleloadable"],
1119 }
1120
1121 cc_library {
1122 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001123 vendor_available: true,
1124 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001125 vndk: {
1126 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001127 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001128 },
1129 double_loadable: true,
1130 }
1131 `)
1132 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1133 testCc(t, `
1134 cc_library {
1135 name: "libllndk",
1136 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001137 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001138 }
1139
1140 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001141 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001142 symbol_file: "",
1143 }
1144
1145 cc_library {
1146 name: "libcoreonly",
1147 shared_libs: ["libvendoravailable"],
1148 }
1149
1150 // indirect dependency of LLNDK
1151 cc_library {
1152 name: "libvendoravailable",
1153 vendor_available: true,
1154 double_loadable: true,
1155 }
1156 `)
1157}
1158
1159func TestDoubleLoadableDepError(t *testing.T) {
1160 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1161 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1162 cc_library {
1163 name: "libllndk",
1164 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001165 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001166 }
1167
1168 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001169 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001170 symbol_file: "",
1171 }
1172
1173 cc_library {
1174 name: "libnondoubleloadable",
1175 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001176 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001177 vndk: {
1178 enabled: true,
1179 },
1180 }
1181 `)
1182
1183 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1184 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1185 cc_library {
1186 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001187 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001188 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001189 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001190 }
1191
1192 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001193 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001194 symbol_file: "",
1195 }
1196
1197 cc_library {
1198 name: "libnondoubleloadable",
1199 vendor_available: true,
1200 }
1201 `)
1202
Jooyung Hana70f0672019-01-18 15:20:43 +09001203 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1204 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1205 cc_library {
1206 name: "libllndk",
1207 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001208 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 }
1210
1211 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001212 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001213 symbol_file: "",
1214 }
1215
1216 cc_library {
1217 name: "libcoreonly",
1218 shared_libs: ["libvendoravailable"],
1219 }
1220
1221 // indirect dependency of LLNDK
1222 cc_library {
1223 name: "libvendoravailable",
1224 vendor_available: true,
1225 }
1226 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001227
1228 // The error is not from 'client' but from 'libllndk'
1229 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1230 cc_library {
1231 name: "client",
1232 vendor_available: true,
1233 double_loadable: true,
1234 shared_libs: ["libllndk"],
1235 }
1236 cc_library {
1237 name: "libllndk",
1238 shared_libs: ["libnondoubleloadable"],
1239 llndk_stubs: "libllndk.llndk",
1240 }
1241 llndk_library {
1242 name: "libllndk.llndk",
1243 symbol_file: "",
1244 }
1245 cc_library {
1246 name: "libnondoubleloadable",
1247 vendor_available: true,
1248 }
1249 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001250}
1251
Jooyung Han479ca172020-10-19 18:51:07 +09001252func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1253 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1254 cc_library {
1255 name: "libvndksp",
1256 shared_libs: ["libanothervndksp"],
1257 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001258 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001259 vndk: {
1260 enabled: true,
1261 support_system_process: true,
1262 }
1263 }
1264
1265 cc_library {
1266 name: "libllndk",
1267 shared_libs: ["libanothervndksp"],
1268 }
1269
1270 llndk_library {
1271 name: "libllndk",
1272 symbol_file: "",
1273 }
1274
1275 cc_library {
1276 name: "libanothervndksp",
1277 vendor_available: true,
1278 }
1279 `)
1280}
1281
Logan Chienf3511742017-10-31 18:04:35 +08001282func TestVndkExt(t *testing.T) {
1283 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001284 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001285 cc_library {
1286 name: "libvndk",
1287 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001288 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001289 vndk: {
1290 enabled: true,
1291 },
1292 nocrt: true,
1293 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001294 cc_library {
1295 name: "libvndk2",
1296 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001297 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001298 vndk: {
1299 enabled: true,
1300 },
1301 target: {
1302 vendor: {
1303 suffix: "-suffix",
1304 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001305 product: {
1306 suffix: "-suffix",
1307 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001308 },
1309 nocrt: true,
1310 }
Logan Chienf3511742017-10-31 18:04:35 +08001311
1312 cc_library {
1313 name: "libvndk_ext",
1314 vendor: true,
1315 vndk: {
1316 enabled: true,
1317 extends: "libvndk",
1318 },
1319 nocrt: true,
1320 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001321
1322 cc_library {
1323 name: "libvndk2_ext",
1324 vendor: true,
1325 vndk: {
1326 enabled: true,
1327 extends: "libvndk2",
1328 },
1329 nocrt: true,
1330 }
Logan Chienf3511742017-10-31 18:04:35 +08001331
Justin Yun0ecf0b22020-02-28 15:07:59 +09001332 cc_library {
1333 name: "libvndk_ext_product",
1334 product_specific: true,
1335 vndk: {
1336 enabled: true,
1337 extends: "libvndk",
1338 },
1339 nocrt: true,
1340 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001341
Justin Yun0ecf0b22020-02-28 15:07:59 +09001342 cc_library {
1343 name: "libvndk2_ext_product",
1344 product_specific: true,
1345 vndk: {
1346 enabled: true,
1347 extends: "libvndk2",
1348 },
1349 nocrt: true,
1350 }
1351 `
1352 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1353 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1354 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1355 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1356
1357 ctx := testCcWithConfig(t, config)
1358
1359 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1360 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1361
1362 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1363 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1364
1365 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1366 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001367}
1368
Logan Chiend3c59a22018-03-29 14:08:15 +08001369func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001370 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1371 ctx := testCcNoVndk(t, `
1372 cc_library {
1373 name: "libvndk",
1374 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001375 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001376 vndk: {
1377 enabled: true,
1378 },
1379 nocrt: true,
1380 }
1381
1382 cc_library {
1383 name: "libvndk_ext",
1384 vendor: true,
1385 vndk: {
1386 enabled: true,
1387 extends: "libvndk",
1388 },
1389 nocrt: true,
1390 }
1391 `)
1392
1393 // Ensures that the core variant of "libvndk_ext" can be found.
1394 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1395 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1396 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1397 }
1398}
1399
Justin Yun0ecf0b22020-02-28 15:07:59 +09001400func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1401 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001402 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001403 cc_library {
1404 name: "libvndk",
1405 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001406 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001407 vndk: {
1408 enabled: true,
1409 },
1410 nocrt: true,
1411 }
1412
1413 cc_library {
1414 name: "libvndk_ext_product",
1415 product_specific: true,
1416 vndk: {
1417 enabled: true,
1418 extends: "libvndk",
1419 },
1420 nocrt: true,
1421 }
1422 `)
1423
1424 // Ensures that the core variant of "libvndk_ext_product" can be found.
1425 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1426 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1427 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1428 }
1429}
1430
Logan Chienf3511742017-10-31 18:04:35 +08001431func TestVndkExtError(t *testing.T) {
1432 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001433 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001434 cc_library {
1435 name: "libvndk",
1436 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001437 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001438 vndk: {
1439 enabled: true,
1440 },
1441 nocrt: true,
1442 }
1443
1444 cc_library {
1445 name: "libvndk_ext",
1446 vndk: {
1447 enabled: true,
1448 extends: "libvndk",
1449 },
1450 nocrt: true,
1451 }
1452 `)
1453
1454 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1455 cc_library {
1456 name: "libvndk",
1457 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001458 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001459 vndk: {
1460 enabled: true,
1461 },
1462 nocrt: true,
1463 }
1464
1465 cc_library {
1466 name: "libvndk_ext",
1467 vendor: true,
1468 vndk: {
1469 enabled: true,
1470 },
1471 nocrt: true,
1472 }
1473 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001474
1475 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1476 cc_library {
1477 name: "libvndk",
1478 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001479 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001480 vndk: {
1481 enabled: true,
1482 },
1483 nocrt: true,
1484 }
1485
1486 cc_library {
1487 name: "libvndk_ext_product",
1488 product_specific: true,
1489 vndk: {
1490 enabled: true,
1491 },
1492 nocrt: true,
1493 }
1494 `)
1495
1496 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1497 cc_library {
1498 name: "libvndk",
1499 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001500 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001501 vndk: {
1502 enabled: true,
1503 },
1504 nocrt: true,
1505 }
1506
1507 cc_library {
1508 name: "libvndk_ext_product",
1509 product_specific: true,
1510 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001511 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001512 vndk: {
1513 enabled: true,
1514 extends: "libvndk",
1515 },
1516 nocrt: true,
1517 }
1518 `)
Logan Chienf3511742017-10-31 18:04:35 +08001519}
1520
1521func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1522 // This test ensures an error is emitted for inconsistent support_system_process.
1523 testCcError(t, "module \".*\" with mismatched support_system_process", `
1524 cc_library {
1525 name: "libvndk",
1526 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001527 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001528 vndk: {
1529 enabled: true,
1530 },
1531 nocrt: true,
1532 }
1533
1534 cc_library {
1535 name: "libvndk_sp_ext",
1536 vendor: true,
1537 vndk: {
1538 enabled: true,
1539 extends: "libvndk",
1540 support_system_process: true,
1541 },
1542 nocrt: true,
1543 }
1544 `)
1545
1546 testCcError(t, "module \".*\" with mismatched support_system_process", `
1547 cc_library {
1548 name: "libvndk_sp",
1549 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001550 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001551 vndk: {
1552 enabled: true,
1553 support_system_process: true,
1554 },
1555 nocrt: true,
1556 }
1557
1558 cc_library {
1559 name: "libvndk_ext",
1560 vendor: true,
1561 vndk: {
1562 enabled: true,
1563 extends: "libvndk_sp",
1564 },
1565 nocrt: true,
1566 }
1567 `)
1568}
1569
1570func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001571 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001572 // with `private: true`.
1573 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001574 cc_library {
1575 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001576 vendor_available: true,
1577 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001578 vndk: {
1579 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001580 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001581 },
1582 nocrt: true,
1583 }
1584
1585 cc_library {
1586 name: "libvndk_ext",
1587 vendor: true,
1588 vndk: {
1589 enabled: true,
1590 extends: "libvndk",
1591 },
1592 nocrt: true,
1593 }
1594 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001595
Justin Yunfd9e8042020-12-23 18:23:14 +09001596 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001597 cc_library {
1598 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001599 vendor_available: true,
1600 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001601 vndk: {
1602 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001603 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001604 },
1605 nocrt: true,
1606 }
1607
1608 cc_library {
1609 name: "libvndk_ext_product",
1610 product_specific: true,
1611 vndk: {
1612 enabled: true,
1613 extends: "libvndk",
1614 },
1615 nocrt: true,
1616 }
1617 `)
Logan Chienf3511742017-10-31 18:04:35 +08001618}
1619
Logan Chiend3c59a22018-03-29 14:08:15 +08001620func TestVendorModuleUseVndkExt(t *testing.T) {
1621 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001622 testCc(t, `
1623 cc_library {
1624 name: "libvndk",
1625 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001626 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001627 vndk: {
1628 enabled: true,
1629 },
1630 nocrt: true,
1631 }
1632
1633 cc_library {
1634 name: "libvndk_ext",
1635 vendor: true,
1636 vndk: {
1637 enabled: true,
1638 extends: "libvndk",
1639 },
1640 nocrt: true,
1641 }
1642
1643 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001644 name: "libvndk_sp",
1645 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001646 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001647 vndk: {
1648 enabled: true,
1649 support_system_process: true,
1650 },
1651 nocrt: true,
1652 }
1653
1654 cc_library {
1655 name: "libvndk_sp_ext",
1656 vendor: true,
1657 vndk: {
1658 enabled: true,
1659 extends: "libvndk_sp",
1660 support_system_process: true,
1661 },
1662 nocrt: true,
1663 }
1664
1665 cc_library {
1666 name: "libvendor",
1667 vendor: true,
1668 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1669 nocrt: true,
1670 }
1671 `)
1672}
1673
Logan Chiend3c59a22018-03-29 14:08:15 +08001674func TestVndkExtUseVendorLib(t *testing.T) {
1675 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001676 testCc(t, `
1677 cc_library {
1678 name: "libvndk",
1679 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001680 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001681 vndk: {
1682 enabled: true,
1683 },
1684 nocrt: true,
1685 }
1686
1687 cc_library {
1688 name: "libvndk_ext",
1689 vendor: true,
1690 vndk: {
1691 enabled: true,
1692 extends: "libvndk",
1693 },
1694 shared_libs: ["libvendor"],
1695 nocrt: true,
1696 }
1697
1698 cc_library {
1699 name: "libvendor",
1700 vendor: true,
1701 nocrt: true,
1702 }
1703 `)
Logan Chienf3511742017-10-31 18:04:35 +08001704
Logan Chiend3c59a22018-03-29 14:08:15 +08001705 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1706 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001707 cc_library {
1708 name: "libvndk_sp",
1709 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001710 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001711 vndk: {
1712 enabled: true,
1713 support_system_process: true,
1714 },
1715 nocrt: true,
1716 }
1717
1718 cc_library {
1719 name: "libvndk_sp_ext",
1720 vendor: true,
1721 vndk: {
1722 enabled: true,
1723 extends: "libvndk_sp",
1724 support_system_process: true,
1725 },
1726 shared_libs: ["libvendor"], // Cause an error
1727 nocrt: true,
1728 }
1729
1730 cc_library {
1731 name: "libvendor",
1732 vendor: true,
1733 nocrt: true,
1734 }
1735 `)
1736}
1737
Justin Yun0ecf0b22020-02-28 15:07:59 +09001738func TestProductVndkExtDependency(t *testing.T) {
1739 bp := `
1740 cc_library {
1741 name: "libvndk",
1742 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001743 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001744 vndk: {
1745 enabled: true,
1746 },
1747 nocrt: true,
1748 }
1749
1750 cc_library {
1751 name: "libvndk_ext_product",
1752 product_specific: true,
1753 vndk: {
1754 enabled: true,
1755 extends: "libvndk",
1756 },
1757 shared_libs: ["libproduct_for_vndklibs"],
1758 nocrt: true,
1759 }
1760
1761 cc_library {
1762 name: "libvndk_sp",
1763 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001764 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001765 vndk: {
1766 enabled: true,
1767 support_system_process: true,
1768 },
1769 nocrt: true,
1770 }
1771
1772 cc_library {
1773 name: "libvndk_sp_ext_product",
1774 product_specific: true,
1775 vndk: {
1776 enabled: true,
1777 extends: "libvndk_sp",
1778 support_system_process: true,
1779 },
1780 shared_libs: ["libproduct_for_vndklibs"],
1781 nocrt: true,
1782 }
1783
1784 cc_library {
1785 name: "libproduct",
1786 product_specific: true,
1787 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1788 nocrt: true,
1789 }
1790
1791 cc_library {
1792 name: "libproduct_for_vndklibs",
1793 product_specific: true,
1794 nocrt: true,
1795 }
1796 `
1797 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1798 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1799 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1800 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1801
1802 testCcWithConfig(t, config)
1803}
1804
Logan Chiend3c59a22018-03-29 14:08:15 +08001805func TestVndkSpExtUseVndkError(t *testing.T) {
1806 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1807 // library.
1808 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1809 cc_library {
1810 name: "libvndk",
1811 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001812 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001813 vndk: {
1814 enabled: true,
1815 },
1816 nocrt: true,
1817 }
1818
1819 cc_library {
1820 name: "libvndk_sp",
1821 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001822 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001823 vndk: {
1824 enabled: true,
1825 support_system_process: true,
1826 },
1827 nocrt: true,
1828 }
1829
1830 cc_library {
1831 name: "libvndk_sp_ext",
1832 vendor: true,
1833 vndk: {
1834 enabled: true,
1835 extends: "libvndk_sp",
1836 support_system_process: true,
1837 },
1838 shared_libs: ["libvndk"], // Cause an error
1839 nocrt: true,
1840 }
1841 `)
1842
1843 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1844 // library.
1845 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1846 cc_library {
1847 name: "libvndk",
1848 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001849 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001850 vndk: {
1851 enabled: true,
1852 },
1853 nocrt: true,
1854 }
1855
1856 cc_library {
1857 name: "libvndk_ext",
1858 vendor: true,
1859 vndk: {
1860 enabled: true,
1861 extends: "libvndk",
1862 },
1863 nocrt: true,
1864 }
1865
1866 cc_library {
1867 name: "libvndk_sp",
1868 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001869 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001870 vndk: {
1871 enabled: true,
1872 support_system_process: true,
1873 },
1874 nocrt: true,
1875 }
1876
1877 cc_library {
1878 name: "libvndk_sp_ext",
1879 vendor: true,
1880 vndk: {
1881 enabled: true,
1882 extends: "libvndk_sp",
1883 support_system_process: true,
1884 },
1885 shared_libs: ["libvndk_ext"], // Cause an error
1886 nocrt: true,
1887 }
1888 `)
1889}
1890
1891func TestVndkUseVndkExtError(t *testing.T) {
1892 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1893 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001894 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1895 cc_library {
1896 name: "libvndk",
1897 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001898 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001899 vndk: {
1900 enabled: true,
1901 },
1902 nocrt: true,
1903 }
1904
1905 cc_library {
1906 name: "libvndk_ext",
1907 vendor: true,
1908 vndk: {
1909 enabled: true,
1910 extends: "libvndk",
1911 },
1912 nocrt: true,
1913 }
1914
1915 cc_library {
1916 name: "libvndk2",
1917 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001918 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001919 vndk: {
1920 enabled: true,
1921 },
1922 shared_libs: ["libvndk_ext"],
1923 nocrt: true,
1924 }
1925 `)
1926
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001927 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001928 cc_library {
1929 name: "libvndk",
1930 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001931 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001932 vndk: {
1933 enabled: true,
1934 },
1935 nocrt: true,
1936 }
1937
1938 cc_library {
1939 name: "libvndk_ext",
1940 vendor: true,
1941 vndk: {
1942 enabled: true,
1943 extends: "libvndk",
1944 },
1945 nocrt: true,
1946 }
1947
1948 cc_library {
1949 name: "libvndk2",
1950 vendor_available: true,
1951 vndk: {
1952 enabled: true,
1953 },
1954 target: {
1955 vendor: {
1956 shared_libs: ["libvndk_ext"],
1957 },
1958 },
1959 nocrt: true,
1960 }
1961 `)
1962
1963 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1964 cc_library {
1965 name: "libvndk_sp",
1966 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001967 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001968 vndk: {
1969 enabled: true,
1970 support_system_process: true,
1971 },
1972 nocrt: true,
1973 }
1974
1975 cc_library {
1976 name: "libvndk_sp_ext",
1977 vendor: true,
1978 vndk: {
1979 enabled: true,
1980 extends: "libvndk_sp",
1981 support_system_process: true,
1982 },
1983 nocrt: true,
1984 }
1985
1986 cc_library {
1987 name: "libvndk_sp_2",
1988 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001989 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001990 vndk: {
1991 enabled: true,
1992 support_system_process: true,
1993 },
1994 shared_libs: ["libvndk_sp_ext"],
1995 nocrt: true,
1996 }
1997 `)
1998
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001999 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002000 cc_library {
2001 name: "libvndk_sp",
2002 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002003 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002004 vndk: {
2005 enabled: true,
2006 },
2007 nocrt: true,
2008 }
2009
2010 cc_library {
2011 name: "libvndk_sp_ext",
2012 vendor: true,
2013 vndk: {
2014 enabled: true,
2015 extends: "libvndk_sp",
2016 },
2017 nocrt: true,
2018 }
2019
2020 cc_library {
2021 name: "libvndk_sp2",
2022 vendor_available: true,
2023 vndk: {
2024 enabled: true,
2025 },
2026 target: {
2027 vendor: {
2028 shared_libs: ["libvndk_sp_ext"],
2029 },
2030 },
2031 nocrt: true,
2032 }
2033 `)
2034}
2035
Justin Yun5f7f7e82019-11-18 19:52:14 +09002036func TestEnforceProductVndkVersion(t *testing.T) {
2037 bp := `
2038 cc_library {
2039 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002040 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002041 }
2042 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002043 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002044 symbol_file: "",
2045 }
2046 cc_library {
2047 name: "libvndk",
2048 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002049 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002050 vndk: {
2051 enabled: true,
2052 },
2053 nocrt: true,
2054 }
2055 cc_library {
2056 name: "libvndk_sp",
2057 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002058 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002059 vndk: {
2060 enabled: true,
2061 support_system_process: true,
2062 },
2063 nocrt: true,
2064 }
2065 cc_library {
2066 name: "libva",
2067 vendor_available: true,
2068 nocrt: true,
2069 }
2070 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002071 name: "libpa",
2072 product_available: true,
2073 nocrt: true,
2074 }
2075 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002076 name: "libboth_available",
2077 vendor_available: true,
2078 product_available: true,
2079 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002080 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002081 target: {
2082 vendor: {
2083 suffix: "-vendor",
2084 },
2085 product: {
2086 suffix: "-product",
2087 },
2088 }
2089 }
2090 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002091 name: "libproduct_va",
2092 product_specific: true,
2093 vendor_available: true,
2094 nocrt: true,
2095 }
2096 cc_library {
2097 name: "libprod",
2098 product_specific: true,
2099 shared_libs: [
2100 "libllndk",
2101 "libvndk",
2102 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002103 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002104 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002105 "libproduct_va",
2106 ],
2107 nocrt: true,
2108 }
2109 cc_library {
2110 name: "libvendor",
2111 vendor: true,
2112 shared_libs: [
2113 "libllndk",
2114 "libvndk",
2115 "libvndk_sp",
2116 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002117 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002118 "libproduct_va",
2119 ],
2120 nocrt: true,
2121 }
2122 `
2123
Justin Yun13decfb2021-03-08 19:25:55 +09002124 ctx := ccFixtureFactory.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002125
Jooyung Han261e1582020-10-20 18:54:21 +09002126 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2127 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002128
2129 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2130 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2131
2132 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2133 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002134
2135 ensureStringContains := func(t *testing.T, str string, substr string) {
2136 t.Helper()
2137 if !strings.Contains(str, substr) {
2138 t.Errorf("%q is not found in %v", substr, str)
2139 }
2140 }
2141 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2142 t.Helper()
2143 if strings.Contains(str, substr) {
2144 t.Errorf("%q is found in %v", substr, str)
2145 }
2146 }
2147
2148 // _static variant is used since _shared reuses *.o from the static variant
2149 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2150 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2151
2152 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2153 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2154 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2155 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2156
2157 product_cflags := product_static.Rule("cc").Args["cFlags"]
2158 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2159 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2160 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002161}
2162
2163func TestEnforceProductVndkVersionErrors(t *testing.T) {
2164 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2165 cc_library {
2166 name: "libprod",
2167 product_specific: true,
2168 shared_libs: [
2169 "libvendor",
2170 ],
2171 nocrt: true,
2172 }
2173 cc_library {
2174 name: "libvendor",
2175 vendor: true,
2176 nocrt: true,
2177 }
2178 `)
2179 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2180 cc_library {
2181 name: "libprod",
2182 product_specific: true,
2183 shared_libs: [
2184 "libsystem",
2185 ],
2186 nocrt: true,
2187 }
2188 cc_library {
2189 name: "libsystem",
2190 nocrt: true,
2191 }
2192 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002193 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2194 cc_library {
2195 name: "libprod",
2196 product_specific: true,
2197 shared_libs: [
2198 "libva",
2199 ],
2200 nocrt: true,
2201 }
2202 cc_library {
2203 name: "libva",
2204 vendor_available: true,
2205 nocrt: true,
2206 }
2207 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002208 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002209 cc_library {
2210 name: "libprod",
2211 product_specific: true,
2212 shared_libs: [
2213 "libvndk_private",
2214 ],
2215 nocrt: true,
2216 }
2217 cc_library {
2218 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002219 vendor_available: true,
2220 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002221 vndk: {
2222 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002223 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002224 },
2225 nocrt: true,
2226 }
2227 `)
2228 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2229 cc_library {
2230 name: "libprod",
2231 product_specific: true,
2232 shared_libs: [
2233 "libsystem_ext",
2234 ],
2235 nocrt: true,
2236 }
2237 cc_library {
2238 name: "libsystem_ext",
2239 system_ext_specific: true,
2240 nocrt: true,
2241 }
2242 `)
2243 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2244 cc_library {
2245 name: "libsystem",
2246 shared_libs: [
2247 "libproduct_va",
2248 ],
2249 nocrt: true,
2250 }
2251 cc_library {
2252 name: "libproduct_va",
2253 product_specific: true,
2254 vendor_available: true,
2255 nocrt: true,
2256 }
2257 `)
2258}
2259
Jooyung Han38002912019-05-16 04:01:54 +09002260func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002261 bp := `
2262 cc_library {
2263 name: "libvndk",
2264 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002265 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002266 vndk: {
2267 enabled: true,
2268 },
2269 }
2270 cc_library {
2271 name: "libvndksp",
2272 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002273 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002274 vndk: {
2275 enabled: true,
2276 support_system_process: true,
2277 },
2278 }
2279 cc_library {
2280 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002281 vendor_available: true,
2282 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002283 vndk: {
2284 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002285 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002286 },
2287 }
2288 cc_library {
2289 name: "libvendor",
2290 vendor: true,
2291 }
2292 cc_library {
2293 name: "libvndkext",
2294 vendor: true,
2295 vndk: {
2296 enabled: true,
2297 extends: "libvndk",
2298 },
2299 }
2300 vndk_prebuilt_shared {
2301 name: "prevndk",
2302 version: "27",
2303 target_arch: "arm",
2304 binder32bit: true,
2305 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002306 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002307 vndk: {
2308 enabled: true,
2309 },
2310 arch: {
2311 arm: {
2312 srcs: ["liba.so"],
2313 },
2314 },
2315 }
2316 cc_library {
2317 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002318 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002319 }
2320 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002321 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002322 symbol_file: "",
2323 }
2324 cc_library {
2325 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002326 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002327 }
2328 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002329 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002330 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002331 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002332 }
2333
2334 llndk_libraries_txt {
2335 name: "llndk.libraries.txt",
2336 }
2337 vndkcore_libraries_txt {
2338 name: "vndkcore.libraries.txt",
2339 }
2340 vndksp_libraries_txt {
2341 name: "vndksp.libraries.txt",
2342 }
2343 vndkprivate_libraries_txt {
2344 name: "vndkprivate.libraries.txt",
2345 }
2346 vndkcorevariant_libraries_txt {
2347 name: "vndkcorevariant.libraries.txt",
2348 insert_vndk_version: false,
2349 }
2350 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002351
2352 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002353 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2354 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2355 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002356 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002357
Colin Cross78212242021-01-06 14:51:30 -08002358 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2359 []string{"libvndk.so", "libvndkprivate.so"})
2360 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2361 []string{"libc++.so", "libvndksp.so"})
2362 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2363 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2364 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2365 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002366
Colin Crossfb0c16e2019-11-20 17:12:35 -08002367 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002368
Jooyung Han38002912019-05-16 04:01:54 +09002369 tests := []struct {
2370 variant string
2371 name string
2372 expected string
2373 }{
2374 {vendorVariant, "libvndk", "native:vndk"},
2375 {vendorVariant, "libvndksp", "native:vndk"},
2376 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2377 {vendorVariant, "libvendor", "native:vendor"},
2378 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002379 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002380 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002381 {coreVariant, "libvndk", "native:platform"},
2382 {coreVariant, "libvndkprivate", "native:platform"},
2383 {coreVariant, "libllndk", "native:platform"},
2384 }
2385 for _, test := range tests {
2386 t.Run(test.name, func(t *testing.T) {
2387 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2388 assertString(t, module.makeLinkType, test.expected)
2389 })
2390 }
2391}
2392
Jeff Gaston294356f2017-09-27 17:05:30 -07002393var staticLinkDepOrderTestCases = []struct {
2394 // This is a string representation of a map[moduleName][]moduleDependency .
2395 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002396 inStatic string
2397
2398 // This is a string representation of a map[moduleName][]moduleDependency .
2399 // It models the dependencies declared in an Android.bp file.
2400 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002401
2402 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2403 // The keys of allOrdered specify which modules we would like to check.
2404 // The values of allOrdered specify the expected result (of the transitive closure of all
2405 // dependencies) for each module to test
2406 allOrdered string
2407
2408 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2409 // The keys of outOrdered specify which modules we would like to check.
2410 // The values of outOrdered specify the expected result (of the ordered linker command line)
2411 // for each module to test.
2412 outOrdered string
2413}{
2414 // Simple tests
2415 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002416 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002417 outOrdered: "",
2418 },
2419 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002420 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002421 outOrdered: "a:",
2422 },
2423 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002424 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002425 outOrdered: "a:b; b:",
2426 },
2427 // Tests of reordering
2428 {
2429 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002430 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002431 outOrdered: "a:b,c,d; b:d; c:d; d:",
2432 },
2433 {
2434 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002435 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002436 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2437 },
2438 {
2439 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002440 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002441 outOrdered: "a:d,b,e,c; d:b; e:c",
2442 },
2443 {
2444 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002445 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002446 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2447 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2448 },
2449 {
2450 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002451 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 -07002452 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2453 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2454 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002455 // shared dependencies
2456 {
2457 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2458 // So, we don't actually have to check that a shared dependency of c will change the order
2459 // of a library that depends statically on b and on c. We only need to check that if c has
2460 // a shared dependency on b, that that shows up in allOrdered.
2461 inShared: "c:b",
2462 allOrdered: "c:b",
2463 outOrdered: "c:",
2464 },
2465 {
2466 // This test doesn't actually include any shared dependencies but it's a reminder of what
2467 // the second phase of the above test would look like
2468 inStatic: "a:b,c; c:b",
2469 allOrdered: "a:c,b; c:b",
2470 outOrdered: "a:c,b; c:b",
2471 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002472 // tiebreakers for when two modules specifying different orderings and there is no dependency
2473 // to dictate an order
2474 {
2475 // 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 -08002476 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002477 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2478 },
2479 {
2480 // 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 -08002481 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 -07002482 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2483 },
2484 // Tests involving duplicate dependencies
2485 {
2486 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002487 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002488 outOrdered: "a:c,b",
2489 },
2490 {
2491 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002492 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002493 outOrdered: "a:d,c,b",
2494 },
2495 // Tests to confirm the nonexistence of infinite loops.
2496 // These cases should never happen, so as long as the test terminates and the
2497 // result is deterministic then that should be fine.
2498 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002499 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002500 outOrdered: "a:a",
2501 },
2502 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002503 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002504 allOrdered: "a:b,c; b:c,a; c:a,b",
2505 outOrdered: "a:b; b:c; c:a",
2506 },
2507 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002508 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002509 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2510 outOrdered: "a:c,b; b:a,c; c:b,a",
2511 },
2512}
2513
2514// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2515func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2516 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2517 strippedText := strings.Replace(text, " ", "", -1)
2518 if len(strippedText) < 1 {
2519 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2520 }
2521 allDeps = make(map[android.Path][]android.Path, 0)
2522
2523 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2524 moduleTexts := strings.Split(strippedText, ";")
2525
2526 outputForModuleName := func(moduleName string) android.Path {
2527 return android.PathForTesting(moduleName)
2528 }
2529
2530 for _, moduleText := range moduleTexts {
2531 // convert from "a:b,c" to ["a", "b,c"]
2532 components := strings.Split(moduleText, ":")
2533 if len(components) != 2 {
2534 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2535 }
2536 moduleName := components[0]
2537 moduleOutput := outputForModuleName(moduleName)
2538 modulesInOrder = append(modulesInOrder, moduleOutput)
2539
2540 depString := components[1]
2541 // convert from "b,c" to ["b", "c"]
2542 depNames := strings.Split(depString, ",")
2543 if len(depString) < 1 {
2544 depNames = []string{}
2545 }
2546 var deps []android.Path
2547 for _, depName := range depNames {
2548 deps = append(deps, outputForModuleName(depName))
2549 }
2550 allDeps[moduleOutput] = deps
2551 }
2552 return modulesInOrder, allDeps
2553}
2554
Jeff Gaston294356f2017-09-27 17:05:30 -07002555func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2556 for _, moduleName := range moduleNames {
2557 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002558 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002559 paths = append(paths, output)
2560 }
2561 return paths
2562}
2563
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002564func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002565 ctx := testCc(t, `
2566 cc_library {
2567 name: "a",
2568 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002569 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002570 }
2571 cc_library {
2572 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002573 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002574 }
2575 cc_library {
2576 name: "c",
2577 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002578 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002579 }
2580 cc_library {
2581 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002582 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002583 }
2584
2585 `)
2586
Colin Cross7113d202019-11-20 16:39:12 -08002587 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002588 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002589 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2590 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002591 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002592
2593 if !reflect.DeepEqual(actual, expected) {
2594 t.Errorf("staticDeps orderings were not propagated correctly"+
2595 "\nactual: %v"+
2596 "\nexpected: %v",
2597 actual,
2598 expected,
2599 )
2600 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002601}
Jeff Gaston294356f2017-09-27 17:05:30 -07002602
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002603func TestStaticLibDepReorderingWithShared(t *testing.T) {
2604 ctx := testCc(t, `
2605 cc_library {
2606 name: "a",
2607 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002608 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002609 }
2610 cc_library {
2611 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002612 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002613 }
2614 cc_library {
2615 name: "c",
2616 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002617 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002618 }
2619
2620 `)
2621
Colin Cross7113d202019-11-20 16:39:12 -08002622 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002623 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002624 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2625 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002626 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002627
2628 if !reflect.DeepEqual(actual, expected) {
2629 t.Errorf("staticDeps orderings did not account for shared libs"+
2630 "\nactual: %v"+
2631 "\nexpected: %v",
2632 actual,
2633 expected,
2634 )
2635 }
2636}
2637
Jooyung Hanb04a4992020-03-13 18:57:35 +09002638func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002639 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002640 if !reflect.DeepEqual(actual, expected) {
2641 t.Errorf(message+
2642 "\nactual: %v"+
2643 "\nexpected: %v",
2644 actual,
2645 expected,
2646 )
2647 }
2648}
2649
Jooyung Han61b66e92020-03-21 14:21:46 +00002650func TestLlndkLibrary(t *testing.T) {
2651 ctx := testCc(t, `
2652 cc_library {
2653 name: "libllndk",
2654 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002655 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002656 }
2657 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002658 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002659 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002660
2661 cc_prebuilt_library_shared {
2662 name: "libllndkprebuilt",
2663 stubs: { versions: ["1", "2"] },
2664 llndk_stubs: "libllndkprebuilt.llndk",
2665 }
2666 llndk_library {
2667 name: "libllndkprebuilt.llndk",
2668 }
2669
2670 cc_library {
2671 name: "libllndk_with_external_headers",
2672 stubs: { versions: ["1", "2"] },
2673 llndk_stubs: "libllndk_with_external_headers.llndk",
2674 header_libs: ["libexternal_headers"],
2675 export_header_lib_headers: ["libexternal_headers"],
2676 }
2677 llndk_library {
2678 name: "libllndk_with_external_headers.llndk",
2679 }
2680 cc_library_headers {
2681 name: "libexternal_headers",
2682 export_include_dirs: ["include"],
2683 vendor_available: true,
2684 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002685 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002686 actual := ctx.ModuleVariantsForTests("libllndk")
2687 for i := 0; i < len(actual); i++ {
2688 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2689 actual = append(actual[:i], actual[i+1:]...)
2690 i--
2691 }
2692 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002693 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002694 "android_vendor.VER_arm64_armv8-a_shared_1",
2695 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002696 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002697 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2698 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002699 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002700 }
2701 checkEquals(t, "variants for llndk stubs", expected, actual)
2702
Colin Cross127bb8b2020-12-16 16:46:01 -08002703 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002704 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2705
Colin Cross127bb8b2020-12-16 16:46:01 -08002706 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002707 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2708}
2709
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710func TestLlndkHeaders(t *testing.T) {
2711 ctx := testCc(t, `
2712 llndk_headers {
2713 name: "libllndk_headers",
2714 export_include_dirs: ["my_include"],
2715 }
2716 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002717 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002718 export_llndk_headers: ["libllndk_headers"],
2719 }
2720 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002721 name: "libllndk",
2722 llndk_stubs: "libllndk.llndk",
2723 }
2724
2725 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002726 name: "libvendor",
2727 shared_libs: ["libllndk"],
2728 vendor: true,
2729 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002730 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002731 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002732 }
2733 `)
2734
2735 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002736 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002737 cflags := cc.Args["cFlags"]
2738 if !strings.Contains(cflags, "-Imy_include") {
2739 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2740 }
2741}
2742
Logan Chien43d34c32017-12-20 01:17:32 +08002743func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2744 actual := module.Properties.AndroidMkRuntimeLibs
2745 if !reflect.DeepEqual(actual, expected) {
2746 t.Errorf("incorrect runtime_libs for shared libs"+
2747 "\nactual: %v"+
2748 "\nexpected: %v",
2749 actual,
2750 expected,
2751 )
2752 }
2753}
2754
2755const runtimeLibAndroidBp = `
2756 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002757 name: "liball_available",
2758 vendor_available: true,
2759 product_available: true,
2760 no_libcrt : true,
2761 nocrt : true,
2762 system_shared_libs : [],
2763 }
2764 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002765 name: "libvendor_available1",
2766 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002767 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002768 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002769 nocrt : true,
2770 system_shared_libs : [],
2771 }
2772 cc_library {
2773 name: "libvendor_available2",
2774 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002775 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002776 target: {
2777 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002778 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002779 }
2780 },
Yi Konge7fe9912019-06-02 00:53:50 -07002781 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002782 nocrt : true,
2783 system_shared_libs : [],
2784 }
2785 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002786 name: "libproduct_vendor",
2787 product_specific: true,
2788 vendor_available: true,
2789 no_libcrt : true,
2790 nocrt : true,
2791 system_shared_libs : [],
2792 }
2793 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002794 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002795 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002796 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002797 nocrt : true,
2798 system_shared_libs : [],
2799 }
2800 cc_library {
2801 name: "libvendor1",
2802 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002803 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002804 nocrt : true,
2805 system_shared_libs : [],
2806 }
2807 cc_library {
2808 name: "libvendor2",
2809 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002810 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002811 no_libcrt : true,
2812 nocrt : true,
2813 system_shared_libs : [],
2814 }
2815 cc_library {
2816 name: "libproduct_available1",
2817 product_available: true,
2818 runtime_libs: ["liball_available"],
2819 no_libcrt : true,
2820 nocrt : true,
2821 system_shared_libs : [],
2822 }
2823 cc_library {
2824 name: "libproduct1",
2825 product_specific: true,
2826 no_libcrt : true,
2827 nocrt : true,
2828 system_shared_libs : [],
2829 }
2830 cc_library {
2831 name: "libproduct2",
2832 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002833 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002834 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002835 nocrt : true,
2836 system_shared_libs : [],
2837 }
2838`
2839
2840func TestRuntimeLibs(t *testing.T) {
2841 ctx := testCc(t, runtimeLibAndroidBp)
2842
2843 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002844 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002845
Justin Yun8a2600c2020-12-07 12:44:03 +09002846 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2847 checkRuntimeLibs(t, []string{"liball_available"}, module)
2848
2849 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2850 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002851
2852 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002853 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002854
2855 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2856 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002857 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002858
Justin Yun8a2600c2020-12-07 12:44:03 +09002859 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2860 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002861
2862 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002863 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002864
2865 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2866 // and product variants.
2867 variant = "android_product.VER_arm64_armv8-a_shared"
2868
2869 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2870 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2871
2872 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002873 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002874}
2875
2876func TestExcludeRuntimeLibs(t *testing.T) {
2877 ctx := testCc(t, runtimeLibAndroidBp)
2878
Colin Cross7113d202019-11-20 16:39:12 -08002879 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002880 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2881 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002882
Colin Crossfb0c16e2019-11-20 17:12:35 -08002883 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002884 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002885 checkRuntimeLibs(t, nil, module)
2886}
2887
2888func TestRuntimeLibsNoVndk(t *testing.T) {
2889 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2890
2891 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2892
Colin Cross7113d202019-11-20 16:39:12 -08002893 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002894
Justin Yun8a2600c2020-12-07 12:44:03 +09002895 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2896 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002897
2898 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002899 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002900
2901 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002902 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002903}
2904
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002905func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002906 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002907 actual := module.Properties.AndroidMkStaticLibs
2908 if !reflect.DeepEqual(actual, expected) {
2909 t.Errorf("incorrect static_libs"+
2910 "\nactual: %v"+
2911 "\nexpected: %v",
2912 actual,
2913 expected,
2914 )
2915 }
2916}
2917
2918const staticLibAndroidBp = `
2919 cc_library {
2920 name: "lib1",
2921 }
2922 cc_library {
2923 name: "lib2",
2924 static_libs: ["lib1"],
2925 }
2926`
2927
2928func TestStaticLibDepExport(t *testing.T) {
2929 ctx := testCc(t, staticLibAndroidBp)
2930
2931 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002932 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002933 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002934 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002935
2936 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002937 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002938 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2939 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002940 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002941}
2942
Jiyong Parkd08b6972017-09-26 10:50:54 +09002943var compilerFlagsTestCases = []struct {
2944 in string
2945 out bool
2946}{
2947 {
2948 in: "a",
2949 out: false,
2950 },
2951 {
2952 in: "-a",
2953 out: true,
2954 },
2955 {
2956 in: "-Ipath/to/something",
2957 out: false,
2958 },
2959 {
2960 in: "-isystempath/to/something",
2961 out: false,
2962 },
2963 {
2964 in: "--coverage",
2965 out: false,
2966 },
2967 {
2968 in: "-include a/b",
2969 out: true,
2970 },
2971 {
2972 in: "-include a/b c/d",
2973 out: false,
2974 },
2975 {
2976 in: "-DMACRO",
2977 out: true,
2978 },
2979 {
2980 in: "-DMAC RO",
2981 out: false,
2982 },
2983 {
2984 in: "-a -b",
2985 out: false,
2986 },
2987 {
2988 in: "-DMACRO=definition",
2989 out: true,
2990 },
2991 {
2992 in: "-DMACRO=defi nition",
2993 out: true, // TODO(jiyong): this should be false
2994 },
2995 {
2996 in: "-DMACRO(x)=x + 1",
2997 out: true,
2998 },
2999 {
3000 in: "-DMACRO=\"defi nition\"",
3001 out: true,
3002 },
3003}
3004
3005type mockContext struct {
3006 BaseModuleContext
3007 result bool
3008}
3009
3010func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3011 // CheckBadCompilerFlags calls this function when the flag should be rejected
3012 ctx.result = false
3013}
3014
3015func TestCompilerFlags(t *testing.T) {
3016 for _, testCase := range compilerFlagsTestCases {
3017 ctx := &mockContext{result: true}
3018 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3019 if ctx.result != testCase.out {
3020 t.Errorf("incorrect output:")
3021 t.Errorf(" input: %#v", testCase.in)
3022 t.Errorf(" expected: %#v", testCase.out)
3023 t.Errorf(" got: %#v", ctx.result)
3024 }
3025 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003026}
Jiyong Park374510b2018-03-19 18:23:01 +09003027
3028func TestVendorPublicLibraries(t *testing.T) {
3029 ctx := testCc(t, `
3030 cc_library_headers {
3031 name: "libvendorpublic_headers",
3032 export_include_dirs: ["my_include"],
3033 }
3034 vendor_public_library {
3035 name: "libvendorpublic",
3036 symbol_file: "",
3037 export_public_headers: ["libvendorpublic_headers"],
3038 }
3039 cc_library {
3040 name: "libvendorpublic",
3041 srcs: ["foo.c"],
3042 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003043 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003044 nocrt: true,
3045 }
3046
3047 cc_library {
3048 name: "libsystem",
3049 shared_libs: ["libvendorpublic"],
3050 vendor: false,
3051 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003052 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003053 nocrt: true,
3054 }
3055 cc_library {
3056 name: "libvendor",
3057 shared_libs: ["libvendorpublic"],
3058 vendor: true,
3059 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003060 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003061 nocrt: true,
3062 }
3063 `)
3064
Colin Cross7113d202019-11-20 16:39:12 -08003065 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003066 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003067
3068 // test if header search paths are correctly added
3069 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003070 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003071 cflags := cc.Args["cFlags"]
3072 if !strings.Contains(cflags, "-Imy_include") {
3073 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3074 }
3075
3076 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003077 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003078 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003079 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003080 if !strings.Contains(libflags, stubPaths[0].String()) {
3081 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3082 }
3083
3084 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003085 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003086 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003087 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003088 if !strings.Contains(libflags, stubPaths[0].String()) {
3089 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3090 }
3091
3092}
Jiyong Park37b25202018-07-11 10:49:27 +09003093
3094func TestRecovery(t *testing.T) {
3095 ctx := testCc(t, `
3096 cc_library_shared {
3097 name: "librecovery",
3098 recovery: true,
3099 }
3100 cc_library_shared {
3101 name: "librecovery32",
3102 recovery: true,
3103 compile_multilib:"32",
3104 }
Jiyong Park5baac542018-08-28 09:55:37 +09003105 cc_library_shared {
3106 name: "libHalInRecovery",
3107 recovery_available: true,
3108 vendor: true,
3109 }
Jiyong Park37b25202018-07-11 10:49:27 +09003110 `)
3111
3112 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003113 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003114 if len(variants) != 1 || !android.InList(arm64, variants) {
3115 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3116 }
3117
3118 variants = ctx.ModuleVariantsForTests("librecovery32")
3119 if android.InList(arm64, variants) {
3120 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3121 }
Jiyong Park5baac542018-08-28 09:55:37 +09003122
3123 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3124 if !recoveryModule.Platform() {
3125 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3126 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003127}
Jiyong Park5baac542018-08-28 09:55:37 +09003128
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003129func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3130 bp := `
3131 cc_prebuilt_test_library_shared {
3132 name: "test_lib",
3133 relative_install_path: "foo/bar/baz",
3134 srcs: ["srcpath/dontusethispath/baz.so"],
3135 }
3136
3137 cc_test {
3138 name: "main_test",
3139 data_libs: ["test_lib"],
3140 gtest: false,
3141 }
3142 `
3143
3144 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3145 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3146 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3147 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3148
3149 ctx := testCcWithConfig(t, config)
3150 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3151 testBinary := module.(*Module).linker.(*testBinary)
3152 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3153 if err != nil {
3154 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3155 }
3156 if len(outputFiles) != 1 {
3157 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3158 }
3159 if len(testBinary.dataPaths()) != 1 {
3160 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3161 }
3162
3163 outputPath := outputFiles[0].String()
3164
3165 if !strings.HasSuffix(outputPath, "/main_test") {
3166 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3167 }
Colin Crossaa255532020-07-03 13:18:24 -07003168 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003169 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3170 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3171 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3172 }
3173}
3174
Jiyong Park7ed9de32018-10-15 22:25:07 +09003175func TestVersionedStubs(t *testing.T) {
3176 ctx := testCc(t, `
3177 cc_library_shared {
3178 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003179 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003180 stubs: {
3181 symbol_file: "foo.map.txt",
3182 versions: ["1", "2", "3"],
3183 },
3184 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003185
Jiyong Park7ed9de32018-10-15 22:25:07 +09003186 cc_library_shared {
3187 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003188 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003189 shared_libs: ["libFoo#1"],
3190 }`)
3191
3192 variants := ctx.ModuleVariantsForTests("libFoo")
3193 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003194 "android_arm64_armv8-a_shared",
3195 "android_arm64_armv8-a_shared_1",
3196 "android_arm64_armv8-a_shared_2",
3197 "android_arm64_armv8-a_shared_3",
3198 "android_arm_armv7-a-neon_shared",
3199 "android_arm_armv7-a-neon_shared_1",
3200 "android_arm_armv7-a-neon_shared_2",
3201 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003202 }
3203 variantsMismatch := false
3204 if len(variants) != len(expectedVariants) {
3205 variantsMismatch = true
3206 } else {
3207 for _, v := range expectedVariants {
3208 if !inList(v, variants) {
3209 variantsMismatch = false
3210 }
3211 }
3212 }
3213 if variantsMismatch {
3214 t.Errorf("variants of libFoo expected:\n")
3215 for _, v := range expectedVariants {
3216 t.Errorf("%q\n", v)
3217 }
3218 t.Errorf(", but got:\n")
3219 for _, v := range variants {
3220 t.Errorf("%q\n", v)
3221 }
3222 }
3223
Colin Cross7113d202019-11-20 16:39:12 -08003224 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003225 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003226 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003227 if !strings.Contains(libFlags, libFoo1StubPath) {
3228 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3229 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003230
Colin Cross7113d202019-11-20 16:39:12 -08003231 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003232 cFlags := libBarCompileRule.Args["cFlags"]
3233 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3234 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3235 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3236 }
Jiyong Park37b25202018-07-11 10:49:27 +09003237}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003238
Jooyung Hanb04a4992020-03-13 18:57:35 +09003239func TestVersioningMacro(t *testing.T) {
3240 for _, tc := range []struct{ moduleName, expected string }{
3241 {"libc", "__LIBC_API__"},
3242 {"libfoo", "__LIBFOO_API__"},
3243 {"libfoo@1", "__LIBFOO_1_API__"},
3244 {"libfoo-v1", "__LIBFOO_V1_API__"},
3245 {"libfoo.v1", "__LIBFOO_V1_API__"},
3246 } {
3247 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3248 }
3249}
3250
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003251func TestStaticExecutable(t *testing.T) {
3252 ctx := testCc(t, `
3253 cc_binary {
3254 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003255 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003256 static_executable: true,
3257 }`)
3258
Colin Cross7113d202019-11-20 16:39:12 -08003259 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003260 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3261 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003262 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003263 for _, lib := range systemStaticLibs {
3264 if !strings.Contains(libFlags, lib) {
3265 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3266 }
3267 }
3268 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3269 for _, lib := range systemSharedLibs {
3270 if strings.Contains(libFlags, lib) {
3271 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3272 }
3273 }
3274}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003275
3276func TestStaticDepsOrderWithStubs(t *testing.T) {
3277 ctx := testCc(t, `
3278 cc_binary {
3279 name: "mybin",
3280 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003281 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003282 static_executable: true,
3283 stl: "none",
3284 }
3285
3286 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003287 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003288 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003289 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003290 stl: "none",
3291 }
3292
3293 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003294 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003295 srcs: ["foo.c"],
3296 stl: "none",
3297 stubs: {
3298 versions: ["1"],
3299 },
3300 }`)
3301
Colin Cross0de8a1e2020-09-18 14:15:30 -07003302 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3303 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003304 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003305
3306 if !reflect.DeepEqual(actual, expected) {
3307 t.Errorf("staticDeps orderings were not propagated correctly"+
3308 "\nactual: %v"+
3309 "\nexpected: %v",
3310 actual,
3311 expected,
3312 )
3313 }
3314}
Jooyung Han38002912019-05-16 04:01:54 +09003315
Jooyung Hand48f3c32019-08-23 11:18:57 +09003316func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3317 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3318 cc_library {
3319 name: "libA",
3320 srcs: ["foo.c"],
3321 shared_libs: ["libB"],
3322 stl: "none",
3323 }
3324
3325 cc_library {
3326 name: "libB",
3327 srcs: ["foo.c"],
3328 enabled: false,
3329 stl: "none",
3330 }
3331 `)
3332}
3333
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003334// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3335// correctly.
3336func TestFuzzTarget(t *testing.T) {
3337 ctx := testCc(t, `
3338 cc_fuzz {
3339 name: "fuzz_smoke_test",
3340 srcs: ["foo.c"],
3341 }`)
3342
Paul Duffin075c4172019-12-19 19:06:13 +00003343 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003344 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3345}
3346
Jiyong Park29074592019-07-07 16:27:47 +09003347func TestAidl(t *testing.T) {
3348}
3349
Jooyung Han38002912019-05-16 04:01:54 +09003350func assertString(t *testing.T, got, expected string) {
3351 t.Helper()
3352 if got != expected {
3353 t.Errorf("expected %q got %q", expected, got)
3354 }
3355}
3356
3357func assertArrayString(t *testing.T, got, expected []string) {
3358 t.Helper()
3359 if len(got) != len(expected) {
3360 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3361 return
3362 }
3363 for i := range got {
3364 if got[i] != expected[i] {
3365 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3366 i, expected[i], expected, got[i], got)
3367 return
3368 }
3369 }
3370}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003371
Jooyung Han0302a842019-10-30 18:43:49 +09003372func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3373 t.Helper()
3374 assertArrayString(t, android.SortedStringKeys(m), expected)
3375}
3376
Colin Crosse1bb5d02019-09-24 14:55:04 -07003377func TestDefaults(t *testing.T) {
3378 ctx := testCc(t, `
3379 cc_defaults {
3380 name: "defaults",
3381 srcs: ["foo.c"],
3382 static: {
3383 srcs: ["bar.c"],
3384 },
3385 shared: {
3386 srcs: ["baz.c"],
3387 },
3388 }
3389
3390 cc_library_static {
3391 name: "libstatic",
3392 defaults: ["defaults"],
3393 }
3394
3395 cc_library_shared {
3396 name: "libshared",
3397 defaults: ["defaults"],
3398 }
3399
3400 cc_library {
3401 name: "libboth",
3402 defaults: ["defaults"],
3403 }
3404
3405 cc_binary {
3406 name: "binary",
3407 defaults: ["defaults"],
3408 }`)
3409
3410 pathsToBase := func(paths android.Paths) []string {
3411 var ret []string
3412 for _, p := range paths {
3413 ret = append(ret, p.Base())
3414 }
3415 return ret
3416 }
3417
Colin Cross7113d202019-11-20 16:39:12 -08003418 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003419 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3420 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3421 }
Colin Cross7113d202019-11-20 16:39:12 -08003422 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003423 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3424 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3425 }
Colin Cross7113d202019-11-20 16:39:12 -08003426 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003427 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3428 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3429 }
3430
Colin Cross7113d202019-11-20 16:39:12 -08003431 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003432 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3433 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3434 }
Colin Cross7113d202019-11-20 16:39:12 -08003435 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003436 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3437 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3438 }
3439}
Colin Crosseabaedd2020-02-06 17:01:55 -08003440
3441func TestProductVariableDefaults(t *testing.T) {
3442 bp := `
3443 cc_defaults {
3444 name: "libfoo_defaults",
3445 srcs: ["foo.c"],
3446 cppflags: ["-DFOO"],
3447 product_variables: {
3448 debuggable: {
3449 cppflags: ["-DBAR"],
3450 },
3451 },
3452 }
3453
3454 cc_library {
3455 name: "libfoo",
3456 defaults: ["libfoo_defaults"],
3457 }
3458 `
3459
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003460 result := ccFixtureFactory.Extend(
3461 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003462
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003463 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3464 variables.Debuggable = BoolPtr(true)
3465 }),
3466 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003467
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003468 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003469 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003470}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003471
3472func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3473 t.Parallel()
3474 bp := `
3475 cc_library_static {
3476 name: "libfoo",
3477 srcs: ["foo.c"],
3478 whole_static_libs: ["libbar"],
3479 }
3480
3481 cc_library_static {
3482 name: "libbar",
3483 whole_static_libs: ["libmissing"],
3484 }
3485 `
3486
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003487 result := ccFixtureFactory.Extend(
3488 android.PrepareForTestWithAllowMissingDependencies,
3489 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003490
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003491 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003492 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003493
Paul Duffine84b1332021-03-12 11:59:43 +00003494 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003495
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003496 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003497 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003498}
Colin Crosse9fe2942020-11-10 18:12:15 -08003499
3500func TestInstallSharedLibs(t *testing.T) {
3501 bp := `
3502 cc_binary {
3503 name: "bin",
3504 host_supported: true,
3505 shared_libs: ["libshared"],
3506 runtime_libs: ["libruntime"],
3507 srcs: [":gen"],
3508 }
3509
3510 cc_library_shared {
3511 name: "libshared",
3512 host_supported: true,
3513 shared_libs: ["libtransitive"],
3514 }
3515
3516 cc_library_shared {
3517 name: "libtransitive",
3518 host_supported: true,
3519 }
3520
3521 cc_library_shared {
3522 name: "libruntime",
3523 host_supported: true,
3524 }
3525
3526 cc_binary_host {
3527 name: "tool",
3528 srcs: ["foo.cpp"],
3529 }
3530
3531 genrule {
3532 name: "gen",
3533 tools: ["tool"],
3534 out: ["gen.cpp"],
3535 cmd: "$(location tool) $(out)",
3536 }
3537 `
3538
3539 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3540 ctx := testCcWithConfig(t, config)
3541
3542 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3543 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3544 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3545 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3546 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3547
3548 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3549 t.Errorf("expected host bin dependency %q, got %q", w, g)
3550 }
3551
3552 if g, w := hostBin.Implicits.Strings(), 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 := hostShared.Implicits.Strings(), hostTransitive.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(), hostRuntime.Output.String(); !android.InList(w, g) {
3561 t.Errorf("expected host bin dependency %q, got %q", w, g)
3562 }
3563
3564 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3565 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3566 }
3567
3568 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3569 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3570 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3571 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3572
3573 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3574 t.Errorf("expected device bin dependency %q, got %q", w, g)
3575 }
3576
3577 if g, w := deviceBin.OrderOnly.Strings(), 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 := deviceShared.OrderOnly.Strings(), deviceTransitive.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(), deviceRuntime.Output.String(); !android.InList(w, g) {
3586 t.Errorf("expected device bin dependency %q, got %q", w, g)
3587 }
3588
3589 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3590 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3591 }
3592
3593}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003594
3595func TestStubsLibReexportsHeaders(t *testing.T) {
3596 ctx := testCc(t, `
3597 cc_library_shared {
3598 name: "libclient",
3599 srcs: ["foo.c"],
3600 shared_libs: ["libfoo#1"],
3601 }
3602
3603 cc_library_shared {
3604 name: "libfoo",
3605 srcs: ["foo.c"],
3606 shared_libs: ["libbar"],
3607 export_shared_lib_headers: ["libbar"],
3608 stubs: {
3609 symbol_file: "foo.map.txt",
3610 versions: ["1", "2", "3"],
3611 },
3612 }
3613
3614 cc_library_shared {
3615 name: "libbar",
3616 export_include_dirs: ["include/libbar"],
3617 srcs: ["foo.c"],
3618 }`)
3619
3620 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3621
3622 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3623 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3624 }
3625}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003626
3627func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3628 ctx := testCc(t, `
3629 cc_library {
3630 name: "libfoo",
3631 srcs: ["a/Foo.aidl"],
3632 aidl: { flags: ["-Werror"], },
3633 }
3634 `)
3635
3636 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3637 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3638 aidlCommand := manifest.Commands[0].GetCommand()
3639 expectedAidlFlag := "-Werror"
3640 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3641 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3642 }
3643}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003644
Jiyong Parka008fb02021-03-16 17:15:53 +09003645func TestMinSdkVersionInClangTriple(t *testing.T) {
3646 ctx := testCc(t, `
3647 cc_library_shared {
3648 name: "libfoo",
3649 srcs: ["foo.c"],
3650 min_sdk_version: "29",
3651 }`)
3652
3653 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3654 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3655}
3656
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003657func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3658 ctx := testCc(t, `
3659 cc_object {
3660 name: "crt_foo",
3661 srcs: ["foo.c"],
3662 crt: true,
3663 stl: "none",
3664 min_sdk_version: "28",
3665
3666 }`)
3667
3668 arch := "android_arm64_armv8-a"
3669 for _, v := range []string{"", "28", "29", "30", "current"} {
3670 var variant string
3671 if v == "" {
3672 variant = arch
3673 } else {
3674 variant = arch + "_sdk_" + v
3675 }
3676 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3677 vNum := v
3678 if v == "current" || v == "" {
3679 vNum = "10000"
3680 }
3681 expected := "-target aarch64-linux-android" + vNum + " "
3682 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3683 }
3684}
3685
3686func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3687 ctx := testCc(t, `
3688 cc_binary {
3689 name: "bin",
3690 srcs: ["foo.c"],
3691 stl: "none",
3692 min_sdk_version: "29",
3693 sdk_version: "current",
3694 }
3695 `)
3696
3697 // Sdk variant uses the crt object of the matching min_sdk_version
3698 variant := "android_arm64_armv8-a_sdk"
3699 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3700 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3701 variant+"_29/crtbegin_dynamic.o")
3702
3703 // platform variant uses the crt object built for platform
3704 variant = "android_arm64_armv8-a"
3705 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3706 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3707 variant+"/crtbegin_dynamic.o")
3708}
3709
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003710type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003711
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003712const (
3713 None MemtagNoteType = iota + 1
3714 Sync
3715 Async
3716)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003717
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003718func (t MemtagNoteType) str() string {
3719 switch t {
3720 case None:
3721 return "none"
3722 case Sync:
3723 return "sync"
3724 case Async:
3725 return "async"
3726 default:
3727 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003728 }
3729}
3730
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003731func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3732 note_async := "note_memtag_heap_async"
3733 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003734
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003735 found := None
3736 implicits := m.Rule("ld").Implicits
3737 for _, lib := range implicits {
3738 if strings.Contains(lib.Rel(), note_async) {
3739 found = Async
3740 break
3741 } else if strings.Contains(lib.Rel(), note_sync) {
3742 found = Sync
3743 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003744 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003745 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003746
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003747 if found != expected {
3748 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3749 }
3750}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003751
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003752var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3753 android.FixtureModifyMockFS(func(fs android.MockFS) {
3754 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003755 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003756 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003757 gtest: false,
3758 }
3759
3760 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003761 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003762 gtest: false,
3763 sanitize: { memtag_heap: false },
3764 }
3765
3766 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003767 name: "%[1]s_test_true",
3768 gtest: false,
3769 sanitize: { memtag_heap: true },
3770 }
3771
3772 cc_test {
3773 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003774 gtest: false,
3775 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3776 }
3777
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003778 cc_test {
3779 name: "%[1]s_test_true_diag",
3780 gtest: false,
3781 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3782 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003783
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003784 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003785 name: "%[1]s_binary",
3786 }
3787
3788 cc_binary {
3789 name: "%[1]s_binary_false",
3790 sanitize: { memtag_heap: false },
3791 }
3792
3793 cc_binary {
3794 name: "%[1]s_binary_true",
3795 sanitize: { memtag_heap: true },
3796 }
3797
3798 cc_binary {
3799 name: "%[1]s_binary_true_nodiag",
3800 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3801 }
3802
3803 cc_binary {
3804 name: "%[1]s_binary_true_diag",
3805 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003806 }
3807 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003808 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3809 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3810 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3811 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003812
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003813 fs.Merge(android.MockFS{
3814 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3815 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3816 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3817 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3818 })
3819 }),
3820 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3821 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3822 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3823 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3824 }),
3825)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003826
3827func TestSanitizeMemtagHeap(t *testing.T) {
3828 variant := "android_arm64_armv8-a"
3829
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003830 result := ccFixtureFactory.Extend(prepareForTestWithMemtagHeap).RunTest(t)
3831 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003832
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003833 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3834 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3835 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3838
3839 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3840 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3841 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3844
3845 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3846 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3847 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3850
3851 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3852 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3853 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3856
3857 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3858 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3859 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3862
3863 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3864 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3865 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3868
3869 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3870 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3871 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3874
3875 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3876 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3877 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3878 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3879 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3880}
3881
3882func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003883 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003884
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003885 result := ccFixtureFactory.Extend(
3886 prepareForTestWithMemtagHeap,
3887 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3888 variables.SanitizeDevice = []string{"memtag_heap"}
3889 }),
3890 ).RunTest(t)
3891 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003892
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003893 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3894 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3895 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3896 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003898
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003899 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3900 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3901 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3902 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3904
3905 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3906 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3907 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3910
3911 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3912 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3913 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3916
3917 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3918 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3919 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3922
3923 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3924 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3925 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3928
3929 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3930 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3931 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3932 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3934
3935 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3936 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3937 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3938 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3940}
3941
3942func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3943 variant := "android_arm64_armv8-a"
3944
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003945 result := ccFixtureFactory.Extend(
3946 prepareForTestWithMemtagHeap,
3947 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3948 variables.SanitizeDevice = []string{"memtag_heap"}
3949 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3950 }),
3951 ).RunTest(t)
3952 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003953
3954 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3955 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3956 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3959
3960 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3961 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3965
3966 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3967 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3971
3972 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3973 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3977
3978 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3979 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3983
3984 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3985 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3986 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3989
3990 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3991 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3992 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3995
3996 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3997 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3998 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3999 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
4000 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07004001}
Paul Duffin3cb603e2021-02-19 13:57:10 +00004002
4003func TestIncludeDirsExporting(t *testing.T) {
4004
4005 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4006 // embedded newline characters alone.
4007 trimIndentingSpaces := func(s string) string {
4008 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4009 }
4010
4011 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4012 t.Helper()
4013 expected = trimIndentingSpaces(expected)
4014 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4015 if expected != actual {
4016 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4017 }
4018 }
4019
4020 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4021
4022 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4023 t.Helper()
4024 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4025 name := module.Name()
4026
4027 for _, checker := range checkers {
4028 checker(t, name, exported)
4029 }
4030 }
4031
4032 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4033 return func(t *testing.T, name string, exported FlagExporterInfo) {
4034 t.Helper()
4035 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4036 }
4037 }
4038
4039 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4040 return func(t *testing.T, name string, exported FlagExporterInfo) {
4041 t.Helper()
4042 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4043 }
4044 }
4045
4046 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4047 return func(t *testing.T, name string, exported FlagExporterInfo) {
4048 t.Helper()
4049 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4050 }
4051 }
4052
4053 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4054 return func(t *testing.T, name string, exported FlagExporterInfo) {
4055 t.Helper()
4056 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4057 }
4058 }
4059
4060 genRuleModules := `
4061 genrule {
4062 name: "genrule_foo",
4063 cmd: "generate-foo",
4064 out: [
4065 "generated_headers/foo/generated_header.h",
4066 ],
4067 export_include_dirs: [
4068 "generated_headers",
4069 ],
4070 }
4071
4072 genrule {
4073 name: "genrule_bar",
4074 cmd: "generate-bar",
4075 out: [
4076 "generated_headers/bar/generated_header.h",
4077 ],
4078 export_include_dirs: [
4079 "generated_headers",
4080 ],
4081 }
4082 `
4083
4084 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4085 ctx := testCc(t, genRuleModules+`
4086 cc_library {
4087 name: "libfoo",
4088 srcs: ["foo.c"],
4089 export_include_dirs: ["foo/standard"],
4090 export_system_include_dirs: ["foo/system"],
4091 generated_headers: ["genrule_foo"],
4092 export_generated_headers: ["genrule_foo"],
4093 }
4094
4095 cc_library {
4096 name: "libbar",
4097 srcs: ["bar.c"],
4098 shared_libs: ["libfoo"],
4099 export_include_dirs: ["bar/standard"],
4100 export_system_include_dirs: ["bar/system"],
4101 generated_headers: ["genrule_bar"],
4102 export_generated_headers: ["genrule_bar"],
4103 }
4104 `)
4105 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4106 checkIncludeDirs(t, ctx, foo,
4107 expectedIncludeDirs(`
4108 foo/standard
4109 .intermediates/genrule_foo/gen/generated_headers
4110 `),
4111 expectedSystemIncludeDirs(`foo/system`),
4112 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4113 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4114 )
4115
4116 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4117 checkIncludeDirs(t, ctx, bar,
4118 expectedIncludeDirs(`
4119 bar/standard
4120 .intermediates/genrule_bar/gen/generated_headers
4121 `),
4122 expectedSystemIncludeDirs(`bar/system`),
4123 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4124 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4125 )
4126 })
4127
4128 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4129 ctx := testCc(t, genRuleModules+`
4130 cc_library {
4131 name: "libfoo",
4132 srcs: ["foo.c"],
4133 export_include_dirs: ["foo/standard"],
4134 export_system_include_dirs: ["foo/system"],
4135 generated_headers: ["genrule_foo"],
4136 export_generated_headers: ["genrule_foo"],
4137 }
4138
4139 cc_library {
4140 name: "libbar",
4141 srcs: ["bar.c"],
4142 whole_static_libs: ["libfoo"],
4143 export_include_dirs: ["bar/standard"],
4144 export_system_include_dirs: ["bar/system"],
4145 generated_headers: ["genrule_bar"],
4146 export_generated_headers: ["genrule_bar"],
4147 }
4148 `)
4149 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4150 checkIncludeDirs(t, ctx, foo,
4151 expectedIncludeDirs(`
4152 foo/standard
4153 .intermediates/genrule_foo/gen/generated_headers
4154 `),
4155 expectedSystemIncludeDirs(`foo/system`),
4156 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4157 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4158 )
4159
4160 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4161 checkIncludeDirs(t, ctx, bar,
4162 expectedIncludeDirs(`
4163 bar/standard
4164 foo/standard
4165 .intermediates/genrule_foo/gen/generated_headers
4166 .intermediates/genrule_bar/gen/generated_headers
4167 `),
4168 expectedSystemIncludeDirs(`
4169 bar/system
4170 foo/system
4171 `),
4172 expectedGeneratedHeaders(`
4173 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4174 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4175 `),
4176 expectedOrderOnlyDeps(`
4177 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4178 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4179 `),
4180 )
4181 })
4182
Paul Duffin3cb603e2021-02-19 13:57:10 +00004183 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4184 ctx := testCc(t, genRuleModules+`
4185 cc_library_shared {
4186 name: "libfoo",
4187 srcs: [
4188 "foo.c",
4189 "b.aidl",
4190 "a.proto",
4191 ],
4192 aidl: {
4193 export_aidl_headers: true,
4194 }
4195 }
4196 `)
4197 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4198 checkIncludeDirs(t, ctx, foo,
4199 expectedIncludeDirs(`
4200 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4201 `),
4202 expectedSystemIncludeDirs(``),
4203 expectedGeneratedHeaders(`
4204 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4205 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4206 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004207 `),
4208 expectedOrderOnlyDeps(`
4209 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4210 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4211 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004212 `),
4213 )
4214 })
4215
Paul Duffin3cb603e2021-02-19 13:57:10 +00004216 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4217 ctx := testCc(t, genRuleModules+`
4218 cc_library_shared {
4219 name: "libfoo",
4220 srcs: [
4221 "foo.c",
4222 "b.aidl",
4223 "a.proto",
4224 ],
4225 proto: {
4226 export_proto_headers: true,
4227 }
4228 }
4229 `)
4230 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4231 checkIncludeDirs(t, ctx, foo,
4232 expectedIncludeDirs(`
4233 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4234 `),
4235 expectedSystemIncludeDirs(``),
4236 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004237 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4238 `),
4239 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004240 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4241 `),
4242 )
4243 })
4244
Paul Duffin33056e82021-02-19 13:49:08 +00004245 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004246 ctx := testCc(t, genRuleModules+`
4247 cc_library_shared {
4248 name: "libfoo",
4249 srcs: [
4250 "foo.c",
4251 "a.sysprop",
4252 "b.aidl",
4253 "a.proto",
4254 ],
4255 }
4256 `)
4257 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4258 checkIncludeDirs(t, ctx, foo,
4259 expectedIncludeDirs(`
4260 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4261 `),
4262 expectedSystemIncludeDirs(``),
4263 expectedGeneratedHeaders(`
4264 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004265 `),
4266 expectedOrderOnlyDeps(`
4267 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4268 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004269 `),
4270 )
4271 })
4272}