blob: f1efbff0e9055ad94f026553b29d8bc0a2379f15 [file] [log] [blame]
Colin Crossd00350c2017-11-17 10:55:38 -08001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Colin Cross74d1ec02015-04-28 13:30:13 -070015package cc
16
17import (
Jeff Gaston294356f2017-09-27 17:05:30 -070018 "fmt"
Jiyong Park6a43f042017-10-12 23:05:00 +090019 "io/ioutil"
20 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090021 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070022 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000023 "regexp"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030var buildDir string
31
32func setUp() {
33 var err error
34 buildDir, err = ioutil.TempDir("", "soong_cc_test")
35 if err != nil {
36 panic(err)
37 }
38}
39
40func tearDown() {
41 os.RemoveAll(buildDir)
42}
43
44func TestMain(m *testing.M) {
45 run := func() int {
46 setUp()
47 defer tearDown()
48
49 return m.Run()
50 }
51
52 os.Exit(run())
53}
54
Paul Duffin02a3d652021-02-24 18:51:54 +000055var ccFixtureFactory = android.NewFixtureFactory(
56 &buildDir,
57 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000058 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
59 variables.DeviceVndkVersion = StringPtr("current")
60 variables.ProductVndkVersion = StringPtr("current")
61 variables.Platform_vndk_version = StringPtr("VER")
62 }),
63)
64
65// testCcWithConfig runs tests using the ccFixtureFactory
66//
67// See testCc for an explanation as to how to stop using this deprecated method.
68//
69// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080070func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070071 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000072 result := ccFixtureFactory.RunTestWithConfig(t, config)
73 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090074}
75
Paul Duffin02a3d652021-02-24 18:51:54 +000076// testCc runs tests using the ccFixtureFactory
77//
78// Do not add any new usages of this, instead use the ccFixtureFactory directly as it makes it much
79// easier to customize the test behavior.
80//
81// If it is necessary to customize the behavior of an existing test that uses this then please first
82// convert the test to using ccFixtureFactory first and then in a following change add the
83// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
84// that it did not change the test behavior unexpectedly.
85//
86// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080087func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080088 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000089 result := ccFixtureFactory.RunTestWithBp(t, bp)
90 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080091}
92
Paul Duffin02a3d652021-02-24 18:51:54 +000093// testCcNoVndk runs tests using the ccFixtureFactory
94//
95// See testCc for an explanation as to how to stop using this deprecated method.
96//
97// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080098func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080099 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800100 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700101 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +0800102
Colin Cross98be1bb2019-12-13 20:41:13 -0800103 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800104}
105
Paul Duffin02a3d652021-02-24 18:51:54 +0000106// testCcNoProductVndk runs tests using the ccFixtureFactory
107//
108// See testCc for an explanation as to how to stop using this deprecated method.
109//
110// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +0900111func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
112 t.Helper()
113 config := TestConfig(buildDir, android.Android, nil, bp, nil)
114 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
115 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
116
117 return testCcWithConfig(t, config)
118}
119
Paul Duffin02a3d652021-02-24 18:51:54 +0000120// testCcErrorWithConfig runs tests using the ccFixtureFactory
121//
122// See testCc for an explanation as to how to stop using this deprecated method.
123//
124// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900125func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800126 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800127
Paul Duffin02a3d652021-02-24 18:51:54 +0000128 ccFixtureFactory.Extend().
129 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
130 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800131}
132
Paul Duffin02a3d652021-02-24 18:51:54 +0000133// testCcError runs tests using the ccFixtureFactory
134//
135// See testCc for an explanation as to how to stop using this deprecated method.
136//
137// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900138func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900139 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900140 config := TestConfig(buildDir, android.Android, nil, bp, nil)
141 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
142 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
143 testCcErrorWithConfig(t, pattern, config)
144 return
145}
146
Paul Duffin02a3d652021-02-24 18:51:54 +0000147// testCcErrorProductVndk runs tests using the ccFixtureFactory
148//
149// See testCc for an explanation as to how to stop using this deprecated method.
150//
151// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900152func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900153 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900154 config := TestConfig(buildDir, android.Android, nil, bp, nil)
155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
156 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
157 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
158 testCcErrorWithConfig(t, pattern, config)
159 return
160}
161
Logan Chienf3511742017-10-31 18:04:35 +0800162const (
Colin Cross7113d202019-11-20 16:39:12 -0800163 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800164 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900165 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800166 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800167)
168
Paul Duffindb462dd2021-03-21 22:01:55 +0000169// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
170// running it in a fixture that requires all source files to exist.
171func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
172 android.GroupFixturePreparers(
173 PrepareForTestWithCcDefaultModules,
174 android.PrepareForTestDisallowNonExistentPaths,
175 ).RunTest(t)
176}
177
Doug Hornc32c6b02019-01-17 14:44:05 -0800178func TestFuchsiaDeps(t *testing.T) {
179 t.Helper()
180
181 bp := `
182 cc_library {
183 name: "libTest",
184 srcs: ["foo.c"],
185 target: {
186 fuchsia: {
187 srcs: ["bar.c"],
188 },
189 },
190 }`
191
Paul Duffinecdac8a2021-02-24 19:18:42 +0000192 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800193
194 rt := false
195 fb := false
196
Paul Duffinecdac8a2021-02-24 19:18:42 +0000197 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800198 implicits := ld.Implicits
199 for _, lib := range implicits {
200 if strings.Contains(lib.Rel(), "libcompiler_rt") {
201 rt = true
202 }
203
204 if strings.Contains(lib.Rel(), "libbioniccompat") {
205 fb = true
206 }
207 }
208
209 if !rt || !fb {
210 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
211 }
212}
213
214func TestFuchsiaTargetDecl(t *testing.T) {
215 t.Helper()
216
217 bp := `
218 cc_library {
219 name: "libTest",
220 srcs: ["foo.c"],
221 target: {
222 fuchsia: {
223 srcs: ["bar.c"],
224 },
225 },
226 }`
227
Paul Duffinecdac8a2021-02-24 19:18:42 +0000228 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
229 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800230 var objs []string
231 for _, o := range ld.Inputs {
232 objs = append(objs, o.Base())
233 }
Paul Duffine84b1332021-03-12 11:59:43 +0000234 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800235}
236
Jiyong Park6a43f042017-10-12 23:05:00 +0900237func TestVendorSrc(t *testing.T) {
238 ctx := testCc(t, `
239 cc_library {
240 name: "libTest",
241 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700242 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800243 nocrt: true,
244 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900245 vendor_available: true,
246 target: {
247 vendor: {
248 srcs: ["bar.c"],
249 },
250 },
251 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900252 `)
253
Logan Chienf3511742017-10-31 18:04:35 +0800254 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900255 var objs []string
256 for _, o := range ld.Inputs {
257 objs = append(objs, o.Base())
258 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800259 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900260 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
261 }
262}
263
Logan Chienf3511742017-10-31 18:04:35 +0800264func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900265 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800266
Logan Chiend3c59a22018-03-29 14:08:15 +0800267 t.Helper()
268
Justin Yun0ecf0b22020-02-28 15:07:59 +0900269 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800270
271 // Check library properties.
272 lib, ok := mod.compiler.(*libraryDecorator)
273 if !ok {
274 t.Errorf("%q must have libraryDecorator", name)
275 } else if lib.baseInstaller.subDir != subDir {
276 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
277 lib.baseInstaller.subDir)
278 }
279
280 // Check VNDK properties.
281 if mod.vndkdep == nil {
282 t.Fatalf("%q must have `vndkdep`", name)
283 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700284 if !mod.IsVndk() {
285 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800286 }
287 if mod.isVndkSp() != isVndkSp {
288 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
289 }
290
291 // Check VNDK extension properties.
292 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500293 if mod.IsVndkExt() != isVndkExt {
294 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800295 }
296
297 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
298 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
299 }
300}
301
Jose Galmes0a942a02021-02-03 14:23:15 -0800302func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700303 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900304 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
305 if !ok {
306 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900307 return
308 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900309 outputFiles, err := mod.OutputFiles("")
310 if err != nil || len(outputFiles) != 1 {
311 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) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800336 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700337}
338
339func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800340 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
341}
342
343func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
344 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700345}
346
Jooyung Han2216fb12019-11-06 16:46:15 +0900347func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
348 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800349 content := android.ContentFromFileRuleForTests(t, params)
350 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900351 assertArrayString(t, actual, expected)
352}
353
Jooyung Han097087b2019-10-22 19:32:18 +0900354func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
355 t.Helper()
356 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900357 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
358}
359
360func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
361 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800362 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
363 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900364}
365
Logan Chienf3511742017-10-31 18:04:35 +0800366func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800367 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800368 cc_library {
369 name: "libvndk",
370 vendor_available: true,
371 vndk: {
372 enabled: true,
373 },
374 nocrt: true,
375 }
376
377 cc_library {
378 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900379 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800380 vndk: {
381 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900382 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800383 },
384 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900385 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800386 }
387
388 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900389 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800390 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900391 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800392 vndk: {
393 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900394 },
395 nocrt: true,
396 target: {
397 vendor: {
398 cflags: ["-DTEST"],
399 },
400 product: {
401 cflags: ["-DTEST"],
402 },
403 },
404 }
405
406 cc_library {
407 name: "libvndk_sp",
408 vendor_available: true,
409 vndk: {
410 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800411 support_system_process: true,
412 },
413 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900414 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800415 }
416
417 cc_library {
418 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900419 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800420 vndk: {
421 enabled: true,
422 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900423 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800424 },
425 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900426 target: {
427 vendor: {
428 suffix: "-x",
429 },
430 },
Logan Chienf3511742017-10-31 18:04:35 +0800431 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900432
433 cc_library {
434 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900435 vendor_available: true,
436 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900437 vndk: {
438 enabled: true,
439 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900440 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900441 },
442 nocrt: true,
443 target: {
444 vendor: {
445 suffix: "-x",
446 },
447 product: {
448 suffix: "-x",
449 },
450 },
451 }
452
Colin Crosse4e44bc2020-12-28 13:50:21 -0800453 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900454 name: "llndk.libraries.txt",
455 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800456 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900457 name: "vndkcore.libraries.txt",
458 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800459 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900460 name: "vndksp.libraries.txt",
461 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800462 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900463 name: "vndkprivate.libraries.txt",
464 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800465 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900466 name: "vndkproduct.libraries.txt",
467 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800468 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900469 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800470 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900471 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800472 `
473
474 config := TestConfig(buildDir, android.Android, nil, bp, nil)
475 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900476 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800477 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
478
479 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800480
Jooyung Han261e1582020-10-20 18:54:21 +0900481 // subdir == "" because VNDK libs are not supposed to be installed separately.
482 // They are installed as part of VNDK APEX instead.
483 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
484 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900485 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900486 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
487 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900488 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900489
Justin Yun6977e8a2020-10-29 18:24:11 +0900490 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
491 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900492
Inseob Kim1f086e22019-05-09 13:29:15 +0900493 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900494 snapshotDir := "vndk-snapshot"
495 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
496
497 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
498 "arm64", "armv8-a"))
499 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
500 "arm", "armv7-a-neon"))
501
502 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
503 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
504 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
505 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
506
Colin Crossfb0c16e2019-11-20 17:12:35 -0800507 variant := "android_vendor.VER_arm64_armv8-a_shared"
508 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900509
Inseob Kim7f283f42020-06-01 21:53:49 +0900510 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
511
512 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
513 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900514 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
515 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900516 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
517 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900518
Jooyung Han39edb6c2019-11-06 16:53:07 +0900519 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900520 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
521 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
522 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
523 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900524 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900525
Jooyung Han097087b2019-10-22 19:32:18 +0900526 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
527 "LLNDK: libc.so",
528 "LLNDK: libdl.so",
529 "LLNDK: libft2.so",
530 "LLNDK: libm.so",
531 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900532 "VNDK-SP: libvndk_sp-x.so",
533 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900534 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900535 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900536 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900537 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900538 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900539 "VNDK-private: libvndk-private.so",
540 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900541 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900542 "VNDK-product: libc++.so",
543 "VNDK-product: libvndk_product.so",
544 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900545 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900546 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900547 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
548 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
549 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 +0900550 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 +0900551 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
552}
553
Yo Chiangbba545e2020-06-09 16:15:37 +0800554func TestVndkWithHostSupported(t *testing.T) {
555 ctx := testCc(t, `
556 cc_library {
557 name: "libvndk_host_supported",
558 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900559 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800560 vndk: {
561 enabled: true,
562 },
563 host_supported: true,
564 }
565
566 cc_library {
567 name: "libvndk_host_supported_but_disabled_on_device",
568 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900569 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800570 vndk: {
571 enabled: true,
572 },
573 host_supported: true,
574 enabled: false,
575 target: {
576 host: {
577 enabled: true,
578 }
579 }
580 }
581
Colin Crosse4e44bc2020-12-28 13:50:21 -0800582 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800583 name: "vndkcore.libraries.txt",
584 }
585 `)
586
587 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
588}
589
Jooyung Han2216fb12019-11-06 16:46:15 +0900590func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800591 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800592 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900593 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800594 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800595 }`
596 config := TestConfig(buildDir, android.Android, nil, bp, nil)
597 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
598 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
599 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900600
601 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700602 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900603 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900604}
605
606func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800607 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900608 cc_library {
609 name: "libvndk",
610 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900611 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900612 vndk: {
613 enabled: true,
614 },
615 nocrt: true,
616 }
617
618 cc_library {
619 name: "libvndk_sp",
620 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900621 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900622 vndk: {
623 enabled: true,
624 support_system_process: true,
625 },
626 nocrt: true,
627 }
628
629 cc_library {
630 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900631 vendor_available: true,
632 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900633 vndk: {
634 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900635 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900636 },
637 nocrt: true,
638 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900639
Colin Crosse4e44bc2020-12-28 13:50:21 -0800640 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900641 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800642 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900643 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800644 `
645
646 config := TestConfig(buildDir, android.Android, nil, bp, nil)
647 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
648 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
649 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
650
651 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
652
653 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900654
Jooyung Han2216fb12019-11-06 16:46:15 +0900655 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900656}
657
Chris Parsons79d66a52020-06-05 17:26:16 -0400658func TestDataLibs(t *testing.T) {
659 bp := `
660 cc_test_library {
661 name: "test_lib",
662 srcs: ["test_lib.cpp"],
663 gtest: false,
664 }
665
666 cc_test {
667 name: "main_test",
668 data_libs: ["test_lib"],
669 gtest: false,
670 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400671 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400672
673 config := TestConfig(buildDir, android.Android, nil, bp, nil)
674 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
675 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
676 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
677
678 ctx := testCcWithConfig(t, config)
679 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
680 testBinary := module.(*Module).linker.(*testBinary)
681 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
682 if err != nil {
683 t.Errorf("Expected cc_test to produce output files, error: %s", err)
684 return
685 }
686 if len(outputFiles) != 1 {
687 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
688 return
689 }
690 if len(testBinary.dataPaths()) != 1 {
691 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
692 return
693 }
694
695 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400696 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400697
698 if !strings.HasSuffix(outputPath, "/main_test") {
699 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
700 return
701 }
702 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
703 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
704 return
705 }
706}
707
Chris Parsons216e10a2020-07-09 17:12:52 -0400708func TestDataLibsRelativeInstallPath(t *testing.T) {
709 bp := `
710 cc_test_library {
711 name: "test_lib",
712 srcs: ["test_lib.cpp"],
713 relative_install_path: "foo/bar/baz",
714 gtest: false,
715 }
716
717 cc_test {
718 name: "main_test",
719 data_libs: ["test_lib"],
720 gtest: false,
721 }
722 `
723
724 config := TestConfig(buildDir, android.Android, nil, bp, nil)
725 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
726 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
727 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
728
729 ctx := testCcWithConfig(t, config)
730 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
731 testBinary := module.(*Module).linker.(*testBinary)
732 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
733 if err != nil {
734 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
735 }
736 if len(outputFiles) != 1 {
737 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
738 }
739 if len(testBinary.dataPaths()) != 1 {
740 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
741 }
742
743 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400744
745 if !strings.HasSuffix(outputPath, "/main_test") {
746 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
747 }
Colin Crossaa255532020-07-03 13:18:24 -0700748 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400749 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
750 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400751 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400752 }
753}
754
Jooyung Han0302a842019-10-30 18:43:49 +0900755func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900756 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900757 cc_library {
758 name: "libvndk",
759 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900760 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900761 vndk: {
762 enabled: true,
763 },
764 nocrt: true,
765 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900766 cc_library {
767 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900768 vendor_available: true,
769 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900770 vndk: {
771 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900772 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900773 },
774 nocrt: true,
775 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800776
777 cc_library {
778 name: "libllndk",
779 llndk_stubs: "libllndk.llndk",
780 }
781
782 llndk_library {
783 name: "libllndk.llndk",
784 symbol_file: "",
785 export_llndk_headers: ["libllndk_headers"],
786 }
787
788 llndk_headers {
789 name: "libllndk_headers",
790 export_include_dirs: ["include"],
791 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900792 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900793
794 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
795 "LLNDK: libc.so",
796 "LLNDK: libdl.so",
797 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800798 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900799 "LLNDK: libm.so",
800 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900801 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900802 "VNDK-core: libvndk.so",
803 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900804 "VNDK-private: libvndk-private.so",
805 "VNDK-product: libc++.so",
806 "VNDK-product: libvndk-private.so",
807 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900808 })
Logan Chienf3511742017-10-31 18:04:35 +0800809}
810
Justin Yun63e9ec72020-10-29 16:49:43 +0900811func TestVndkModuleError(t *testing.T) {
812 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900813 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900814 cc_library {
815 name: "libvndk",
816 vndk: {
817 enabled: true,
818 },
819 nocrt: true,
820 }
821 `)
822
Justin Yunc0d8c492021-01-07 17:45:31 +0900823 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900824 cc_library {
825 name: "libvndk",
826 product_available: true,
827 vndk: {
828 enabled: true,
829 },
830 nocrt: true,
831 }
832 `)
833
Justin Yun6977e8a2020-10-29 18:24:11 +0900834 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
835 cc_library {
836 name: "libvndkprop",
837 vendor_available: true,
838 product_available: true,
839 vndk: {
840 enabled: true,
841 },
842 nocrt: true,
843 target: {
844 vendor: {
845 cflags: ["-DTEST",],
846 },
847 },
848 }
849 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900850}
851
Logan Chiend3c59a22018-03-29 14:08:15 +0800852func TestVndkDepError(t *testing.T) {
853 // Check whether an error is emitted when a VNDK lib depends on a system lib.
854 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
855 cc_library {
856 name: "libvndk",
857 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900858 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800859 vndk: {
860 enabled: true,
861 },
862 shared_libs: ["libfwk"], // Cause error
863 nocrt: true,
864 }
865
866 cc_library {
867 name: "libfwk",
868 nocrt: true,
869 }
870 `)
871
872 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
873 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
874 cc_library {
875 name: "libvndk",
876 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900877 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800878 vndk: {
879 enabled: true,
880 },
881 shared_libs: ["libvendor"], // Cause error
882 nocrt: true,
883 }
884
885 cc_library {
886 name: "libvendor",
887 vendor: true,
888 nocrt: true,
889 }
890 `)
891
892 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
893 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
894 cc_library {
895 name: "libvndk_sp",
896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900897 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800898 vndk: {
899 enabled: true,
900 support_system_process: true,
901 },
902 shared_libs: ["libfwk"], // Cause error
903 nocrt: true,
904 }
905
906 cc_library {
907 name: "libfwk",
908 nocrt: true,
909 }
910 `)
911
912 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
913 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
914 cc_library {
915 name: "libvndk_sp",
916 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900917 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800918 vndk: {
919 enabled: true,
920 support_system_process: true,
921 },
922 shared_libs: ["libvendor"], // Cause error
923 nocrt: true,
924 }
925
926 cc_library {
927 name: "libvendor",
928 vendor: true,
929 nocrt: true,
930 }
931 `)
932
933 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
934 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
935 cc_library {
936 name: "libvndk_sp",
937 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900938 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800939 vndk: {
940 enabled: true,
941 support_system_process: true,
942 },
943 shared_libs: ["libvndk"], // Cause error
944 nocrt: true,
945 }
946
947 cc_library {
948 name: "libvndk",
949 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900950 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800951 vndk: {
952 enabled: true,
953 },
954 nocrt: true,
955 }
956 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900957
958 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
959 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
960 cc_library {
961 name: "libvndk",
962 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900963 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900964 vndk: {
965 enabled: true,
966 },
967 shared_libs: ["libnonvndk"],
968 nocrt: true,
969 }
970
971 cc_library {
972 name: "libnonvndk",
973 vendor_available: true,
974 nocrt: true,
975 }
976 `)
977
978 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
979 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
980 cc_library {
981 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900982 vendor_available: true,
983 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900984 vndk: {
985 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900986 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900987 },
988 shared_libs: ["libnonvndk"],
989 nocrt: true,
990 }
991
992 cc_library {
993 name: "libnonvndk",
994 vendor_available: true,
995 nocrt: true,
996 }
997 `)
998
999 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1000 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1001 cc_library {
1002 name: "libvndksp",
1003 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001004 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001005 vndk: {
1006 enabled: true,
1007 support_system_process: true,
1008 },
1009 shared_libs: ["libnonvndk"],
1010 nocrt: true,
1011 }
1012
1013 cc_library {
1014 name: "libnonvndk",
1015 vendor_available: true,
1016 nocrt: true,
1017 }
1018 `)
1019
1020 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1021 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1022 cc_library {
1023 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001024 vendor_available: true,
1025 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001026 vndk: {
1027 enabled: true,
1028 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001029 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001030 },
1031 shared_libs: ["libnonvndk"],
1032 nocrt: true,
1033 }
1034
1035 cc_library {
1036 name: "libnonvndk",
1037 vendor_available: true,
1038 nocrt: true,
1039 }
1040 `)
1041}
1042
1043func TestDoubleLoadbleDep(t *testing.T) {
1044 // okay to link : LLNDK -> double_loadable VNDK
1045 testCc(t, `
1046 cc_library {
1047 name: "libllndk",
1048 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001049 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001050 }
1051
1052 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001053 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001054 symbol_file: "",
1055 }
1056
1057 cc_library {
1058 name: "libdoubleloadable",
1059 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001060 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001061 vndk: {
1062 enabled: true,
1063 },
1064 double_loadable: true,
1065 }
1066 `)
1067 // okay to link : LLNDK -> VNDK-SP
1068 testCc(t, `
1069 cc_library {
1070 name: "libllndk",
1071 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001072 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001073 }
1074
1075 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001076 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001077 symbol_file: "",
1078 }
1079
1080 cc_library {
1081 name: "libvndksp",
1082 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001083 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001084 vndk: {
1085 enabled: true,
1086 support_system_process: true,
1087 },
1088 }
1089 `)
1090 // okay to link : double_loadable -> double_loadable
1091 testCc(t, `
1092 cc_library {
1093 name: "libdoubleloadable1",
1094 shared_libs: ["libdoubleloadable2"],
1095 vendor_available: true,
1096 double_loadable: true,
1097 }
1098
1099 cc_library {
1100 name: "libdoubleloadable2",
1101 vendor_available: true,
1102 double_loadable: true,
1103 }
1104 `)
1105 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1106 testCc(t, `
1107 cc_library {
1108 name: "libdoubleloadable",
1109 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001110 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001111 vndk: {
1112 enabled: true,
1113 },
1114 double_loadable: true,
1115 shared_libs: ["libnondoubleloadable"],
1116 }
1117
1118 cc_library {
1119 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001120 vendor_available: true,
1121 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001122 vndk: {
1123 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001124 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001125 },
1126 double_loadable: true,
1127 }
1128 `)
1129 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1130 testCc(t, `
1131 cc_library {
1132 name: "libllndk",
1133 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001134 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001135 }
1136
1137 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001138 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001139 symbol_file: "",
1140 }
1141
1142 cc_library {
1143 name: "libcoreonly",
1144 shared_libs: ["libvendoravailable"],
1145 }
1146
1147 // indirect dependency of LLNDK
1148 cc_library {
1149 name: "libvendoravailable",
1150 vendor_available: true,
1151 double_loadable: true,
1152 }
1153 `)
1154}
1155
1156func TestDoubleLoadableDepError(t *testing.T) {
1157 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1158 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1159 cc_library {
1160 name: "libllndk",
1161 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001162 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001163 }
1164
1165 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001166 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001167 symbol_file: "",
1168 }
1169
1170 cc_library {
1171 name: "libnondoubleloadable",
1172 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001173 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001174 vndk: {
1175 enabled: true,
1176 },
1177 }
1178 `)
1179
1180 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1181 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1182 cc_library {
1183 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001184 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001185 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001186 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001187 }
1188
1189 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001190 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 symbol_file: "",
1192 }
1193
1194 cc_library {
1195 name: "libnondoubleloadable",
1196 vendor_available: true,
1197 }
1198 `)
1199
Jooyung Hana70f0672019-01-18 15:20:43 +09001200 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1201 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1202 cc_library {
1203 name: "libllndk",
1204 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001205 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001206 }
1207
1208 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001209 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001210 symbol_file: "",
1211 }
1212
1213 cc_library {
1214 name: "libcoreonly",
1215 shared_libs: ["libvendoravailable"],
1216 }
1217
1218 // indirect dependency of LLNDK
1219 cc_library {
1220 name: "libvendoravailable",
1221 vendor_available: true,
1222 }
1223 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001224
1225 // The error is not from 'client' but from 'libllndk'
1226 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1227 cc_library {
1228 name: "client",
1229 vendor_available: true,
1230 double_loadable: true,
1231 shared_libs: ["libllndk"],
1232 }
1233 cc_library {
1234 name: "libllndk",
1235 shared_libs: ["libnondoubleloadable"],
1236 llndk_stubs: "libllndk.llndk",
1237 }
1238 llndk_library {
1239 name: "libllndk.llndk",
1240 symbol_file: "",
1241 }
1242 cc_library {
1243 name: "libnondoubleloadable",
1244 vendor_available: true,
1245 }
1246 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001247}
1248
Jooyung Han479ca172020-10-19 18:51:07 +09001249func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1250 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1251 cc_library {
1252 name: "libvndksp",
1253 shared_libs: ["libanothervndksp"],
1254 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001255 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001256 vndk: {
1257 enabled: true,
1258 support_system_process: true,
1259 }
1260 }
1261
1262 cc_library {
1263 name: "libllndk",
1264 shared_libs: ["libanothervndksp"],
1265 }
1266
1267 llndk_library {
1268 name: "libllndk",
1269 symbol_file: "",
1270 }
1271
1272 cc_library {
1273 name: "libanothervndksp",
1274 vendor_available: true,
1275 }
1276 `)
1277}
1278
Logan Chienf3511742017-10-31 18:04:35 +08001279func TestVndkExt(t *testing.T) {
1280 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001281 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001282 cc_library {
1283 name: "libvndk",
1284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001285 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001286 vndk: {
1287 enabled: true,
1288 },
1289 nocrt: true,
1290 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001291 cc_library {
1292 name: "libvndk2",
1293 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001294 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001295 vndk: {
1296 enabled: true,
1297 },
1298 target: {
1299 vendor: {
1300 suffix: "-suffix",
1301 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001302 product: {
1303 suffix: "-suffix",
1304 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001305 },
1306 nocrt: true,
1307 }
Logan Chienf3511742017-10-31 18:04:35 +08001308
1309 cc_library {
1310 name: "libvndk_ext",
1311 vendor: true,
1312 vndk: {
1313 enabled: true,
1314 extends: "libvndk",
1315 },
1316 nocrt: true,
1317 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001318
1319 cc_library {
1320 name: "libvndk2_ext",
1321 vendor: true,
1322 vndk: {
1323 enabled: true,
1324 extends: "libvndk2",
1325 },
1326 nocrt: true,
1327 }
Logan Chienf3511742017-10-31 18:04:35 +08001328
Justin Yun0ecf0b22020-02-28 15:07:59 +09001329 cc_library {
1330 name: "libvndk_ext_product",
1331 product_specific: true,
1332 vndk: {
1333 enabled: true,
1334 extends: "libvndk",
1335 },
1336 nocrt: true,
1337 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001338
Justin Yun0ecf0b22020-02-28 15:07:59 +09001339 cc_library {
1340 name: "libvndk2_ext_product",
1341 product_specific: true,
1342 vndk: {
1343 enabled: true,
1344 extends: "libvndk2",
1345 },
1346 nocrt: true,
1347 }
1348 `
1349 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1350 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1351 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1352 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1353
1354 ctx := testCcWithConfig(t, config)
1355
1356 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1357 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1358
1359 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1360 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1361
1362 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1363 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001364}
1365
Logan Chiend3c59a22018-03-29 14:08:15 +08001366func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001367 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1368 ctx := testCcNoVndk(t, `
1369 cc_library {
1370 name: "libvndk",
1371 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001372 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001373 vndk: {
1374 enabled: true,
1375 },
1376 nocrt: true,
1377 }
1378
1379 cc_library {
1380 name: "libvndk_ext",
1381 vendor: true,
1382 vndk: {
1383 enabled: true,
1384 extends: "libvndk",
1385 },
1386 nocrt: true,
1387 }
1388 `)
1389
1390 // Ensures that the core variant of "libvndk_ext" can be found.
1391 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1392 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1393 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1394 }
1395}
1396
Justin Yun0ecf0b22020-02-28 15:07:59 +09001397func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1398 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001399 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001400 cc_library {
1401 name: "libvndk",
1402 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001403 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001404 vndk: {
1405 enabled: true,
1406 },
1407 nocrt: true,
1408 }
1409
1410 cc_library {
1411 name: "libvndk_ext_product",
1412 product_specific: true,
1413 vndk: {
1414 enabled: true,
1415 extends: "libvndk",
1416 },
1417 nocrt: true,
1418 }
1419 `)
1420
1421 // Ensures that the core variant of "libvndk_ext_product" can be found.
1422 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1423 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1424 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1425 }
1426}
1427
Logan Chienf3511742017-10-31 18:04:35 +08001428func TestVndkExtError(t *testing.T) {
1429 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001430 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001431 cc_library {
1432 name: "libvndk",
1433 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001434 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001435 vndk: {
1436 enabled: true,
1437 },
1438 nocrt: true,
1439 }
1440
1441 cc_library {
1442 name: "libvndk_ext",
1443 vndk: {
1444 enabled: true,
1445 extends: "libvndk",
1446 },
1447 nocrt: true,
1448 }
1449 `)
1450
1451 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1452 cc_library {
1453 name: "libvndk",
1454 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001455 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001456 vndk: {
1457 enabled: true,
1458 },
1459 nocrt: true,
1460 }
1461
1462 cc_library {
1463 name: "libvndk_ext",
1464 vendor: true,
1465 vndk: {
1466 enabled: true,
1467 },
1468 nocrt: true,
1469 }
1470 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001471
1472 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1473 cc_library {
1474 name: "libvndk",
1475 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001476 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001477 vndk: {
1478 enabled: true,
1479 },
1480 nocrt: true,
1481 }
1482
1483 cc_library {
1484 name: "libvndk_ext_product",
1485 product_specific: true,
1486 vndk: {
1487 enabled: true,
1488 },
1489 nocrt: true,
1490 }
1491 `)
1492
1493 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1494 cc_library {
1495 name: "libvndk",
1496 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001497 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001498 vndk: {
1499 enabled: true,
1500 },
1501 nocrt: true,
1502 }
1503
1504 cc_library {
1505 name: "libvndk_ext_product",
1506 product_specific: true,
1507 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001508 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001509 vndk: {
1510 enabled: true,
1511 extends: "libvndk",
1512 },
1513 nocrt: true,
1514 }
1515 `)
Logan Chienf3511742017-10-31 18:04:35 +08001516}
1517
1518func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1519 // This test ensures an error is emitted for inconsistent support_system_process.
1520 testCcError(t, "module \".*\" with mismatched support_system_process", `
1521 cc_library {
1522 name: "libvndk",
1523 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001524 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001525 vndk: {
1526 enabled: true,
1527 },
1528 nocrt: true,
1529 }
1530
1531 cc_library {
1532 name: "libvndk_sp_ext",
1533 vendor: true,
1534 vndk: {
1535 enabled: true,
1536 extends: "libvndk",
1537 support_system_process: true,
1538 },
1539 nocrt: true,
1540 }
1541 `)
1542
1543 testCcError(t, "module \".*\" with mismatched support_system_process", `
1544 cc_library {
1545 name: "libvndk_sp",
1546 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001547 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001548 vndk: {
1549 enabled: true,
1550 support_system_process: true,
1551 },
1552 nocrt: true,
1553 }
1554
1555 cc_library {
1556 name: "libvndk_ext",
1557 vendor: true,
1558 vndk: {
1559 enabled: true,
1560 extends: "libvndk_sp",
1561 },
1562 nocrt: true,
1563 }
1564 `)
1565}
1566
1567func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001568 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001569 // with `private: true`.
1570 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001571 cc_library {
1572 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001573 vendor_available: true,
1574 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001575 vndk: {
1576 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001577 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001578 },
1579 nocrt: true,
1580 }
1581
1582 cc_library {
1583 name: "libvndk_ext",
1584 vendor: true,
1585 vndk: {
1586 enabled: true,
1587 extends: "libvndk",
1588 },
1589 nocrt: true,
1590 }
1591 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001592
Justin Yunfd9e8042020-12-23 18:23:14 +09001593 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001594 cc_library {
1595 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001596 vendor_available: true,
1597 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001598 vndk: {
1599 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001600 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001601 },
1602 nocrt: true,
1603 }
1604
1605 cc_library {
1606 name: "libvndk_ext_product",
1607 product_specific: true,
1608 vndk: {
1609 enabled: true,
1610 extends: "libvndk",
1611 },
1612 nocrt: true,
1613 }
1614 `)
Logan Chienf3511742017-10-31 18:04:35 +08001615}
1616
Logan Chiend3c59a22018-03-29 14:08:15 +08001617func TestVendorModuleUseVndkExt(t *testing.T) {
1618 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001619 testCc(t, `
1620 cc_library {
1621 name: "libvndk",
1622 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001623 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001624 vndk: {
1625 enabled: true,
1626 },
1627 nocrt: true,
1628 }
1629
1630 cc_library {
1631 name: "libvndk_ext",
1632 vendor: true,
1633 vndk: {
1634 enabled: true,
1635 extends: "libvndk",
1636 },
1637 nocrt: true,
1638 }
1639
1640 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001641 name: "libvndk_sp",
1642 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001643 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001644 vndk: {
1645 enabled: true,
1646 support_system_process: true,
1647 },
1648 nocrt: true,
1649 }
1650
1651 cc_library {
1652 name: "libvndk_sp_ext",
1653 vendor: true,
1654 vndk: {
1655 enabled: true,
1656 extends: "libvndk_sp",
1657 support_system_process: true,
1658 },
1659 nocrt: true,
1660 }
1661
1662 cc_library {
1663 name: "libvendor",
1664 vendor: true,
1665 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1666 nocrt: true,
1667 }
1668 `)
1669}
1670
Logan Chiend3c59a22018-03-29 14:08:15 +08001671func TestVndkExtUseVendorLib(t *testing.T) {
1672 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001673 testCc(t, `
1674 cc_library {
1675 name: "libvndk",
1676 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001677 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001678 vndk: {
1679 enabled: true,
1680 },
1681 nocrt: true,
1682 }
1683
1684 cc_library {
1685 name: "libvndk_ext",
1686 vendor: true,
1687 vndk: {
1688 enabled: true,
1689 extends: "libvndk",
1690 },
1691 shared_libs: ["libvendor"],
1692 nocrt: true,
1693 }
1694
1695 cc_library {
1696 name: "libvendor",
1697 vendor: true,
1698 nocrt: true,
1699 }
1700 `)
Logan Chienf3511742017-10-31 18:04:35 +08001701
Logan Chiend3c59a22018-03-29 14:08:15 +08001702 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1703 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001704 cc_library {
1705 name: "libvndk_sp",
1706 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001707 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001708 vndk: {
1709 enabled: true,
1710 support_system_process: true,
1711 },
1712 nocrt: true,
1713 }
1714
1715 cc_library {
1716 name: "libvndk_sp_ext",
1717 vendor: true,
1718 vndk: {
1719 enabled: true,
1720 extends: "libvndk_sp",
1721 support_system_process: true,
1722 },
1723 shared_libs: ["libvendor"], // Cause an error
1724 nocrt: true,
1725 }
1726
1727 cc_library {
1728 name: "libvendor",
1729 vendor: true,
1730 nocrt: true,
1731 }
1732 `)
1733}
1734
Justin Yun0ecf0b22020-02-28 15:07:59 +09001735func TestProductVndkExtDependency(t *testing.T) {
1736 bp := `
1737 cc_library {
1738 name: "libvndk",
1739 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001740 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001741 vndk: {
1742 enabled: true,
1743 },
1744 nocrt: true,
1745 }
1746
1747 cc_library {
1748 name: "libvndk_ext_product",
1749 product_specific: true,
1750 vndk: {
1751 enabled: true,
1752 extends: "libvndk",
1753 },
1754 shared_libs: ["libproduct_for_vndklibs"],
1755 nocrt: true,
1756 }
1757
1758 cc_library {
1759 name: "libvndk_sp",
1760 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001761 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001762 vndk: {
1763 enabled: true,
1764 support_system_process: true,
1765 },
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "libvndk_sp_ext_product",
1771 product_specific: true,
1772 vndk: {
1773 enabled: true,
1774 extends: "libvndk_sp",
1775 support_system_process: true,
1776 },
1777 shared_libs: ["libproduct_for_vndklibs"],
1778 nocrt: true,
1779 }
1780
1781 cc_library {
1782 name: "libproduct",
1783 product_specific: true,
1784 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1785 nocrt: true,
1786 }
1787
1788 cc_library {
1789 name: "libproduct_for_vndklibs",
1790 product_specific: true,
1791 nocrt: true,
1792 }
1793 `
1794 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1795 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1796 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1797 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1798
1799 testCcWithConfig(t, config)
1800}
1801
Logan Chiend3c59a22018-03-29 14:08:15 +08001802func TestVndkSpExtUseVndkError(t *testing.T) {
1803 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1804 // library.
1805 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1806 cc_library {
1807 name: "libvndk",
1808 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001809 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001810 vndk: {
1811 enabled: true,
1812 },
1813 nocrt: true,
1814 }
1815
1816 cc_library {
1817 name: "libvndk_sp",
1818 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001819 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001820 vndk: {
1821 enabled: true,
1822 support_system_process: true,
1823 },
1824 nocrt: true,
1825 }
1826
1827 cc_library {
1828 name: "libvndk_sp_ext",
1829 vendor: true,
1830 vndk: {
1831 enabled: true,
1832 extends: "libvndk_sp",
1833 support_system_process: true,
1834 },
1835 shared_libs: ["libvndk"], // Cause an error
1836 nocrt: true,
1837 }
1838 `)
1839
1840 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1841 // library.
1842 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1843 cc_library {
1844 name: "libvndk",
1845 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001846 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001847 vndk: {
1848 enabled: true,
1849 },
1850 nocrt: true,
1851 }
1852
1853 cc_library {
1854 name: "libvndk_ext",
1855 vendor: true,
1856 vndk: {
1857 enabled: true,
1858 extends: "libvndk",
1859 },
1860 nocrt: true,
1861 }
1862
1863 cc_library {
1864 name: "libvndk_sp",
1865 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001866 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001867 vndk: {
1868 enabled: true,
1869 support_system_process: true,
1870 },
1871 nocrt: true,
1872 }
1873
1874 cc_library {
1875 name: "libvndk_sp_ext",
1876 vendor: true,
1877 vndk: {
1878 enabled: true,
1879 extends: "libvndk_sp",
1880 support_system_process: true,
1881 },
1882 shared_libs: ["libvndk_ext"], // Cause an error
1883 nocrt: true,
1884 }
1885 `)
1886}
1887
1888func TestVndkUseVndkExtError(t *testing.T) {
1889 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1890 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001891 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1892 cc_library {
1893 name: "libvndk",
1894 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001895 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001896 vndk: {
1897 enabled: true,
1898 },
1899 nocrt: true,
1900 }
1901
1902 cc_library {
1903 name: "libvndk_ext",
1904 vendor: true,
1905 vndk: {
1906 enabled: true,
1907 extends: "libvndk",
1908 },
1909 nocrt: true,
1910 }
1911
1912 cc_library {
1913 name: "libvndk2",
1914 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001915 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001916 vndk: {
1917 enabled: true,
1918 },
1919 shared_libs: ["libvndk_ext"],
1920 nocrt: true,
1921 }
1922 `)
1923
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001924 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001925 cc_library {
1926 name: "libvndk",
1927 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001928 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001929 vndk: {
1930 enabled: true,
1931 },
1932 nocrt: true,
1933 }
1934
1935 cc_library {
1936 name: "libvndk_ext",
1937 vendor: true,
1938 vndk: {
1939 enabled: true,
1940 extends: "libvndk",
1941 },
1942 nocrt: true,
1943 }
1944
1945 cc_library {
1946 name: "libvndk2",
1947 vendor_available: true,
1948 vndk: {
1949 enabled: true,
1950 },
1951 target: {
1952 vendor: {
1953 shared_libs: ["libvndk_ext"],
1954 },
1955 },
1956 nocrt: true,
1957 }
1958 `)
1959
1960 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1961 cc_library {
1962 name: "libvndk_sp",
1963 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001964 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001965 vndk: {
1966 enabled: true,
1967 support_system_process: true,
1968 },
1969 nocrt: true,
1970 }
1971
1972 cc_library {
1973 name: "libvndk_sp_ext",
1974 vendor: true,
1975 vndk: {
1976 enabled: true,
1977 extends: "libvndk_sp",
1978 support_system_process: true,
1979 },
1980 nocrt: true,
1981 }
1982
1983 cc_library {
1984 name: "libvndk_sp_2",
1985 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001986 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001987 vndk: {
1988 enabled: true,
1989 support_system_process: true,
1990 },
1991 shared_libs: ["libvndk_sp_ext"],
1992 nocrt: true,
1993 }
1994 `)
1995
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001996 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001997 cc_library {
1998 name: "libvndk_sp",
1999 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002000 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002001 vndk: {
2002 enabled: true,
2003 },
2004 nocrt: true,
2005 }
2006
2007 cc_library {
2008 name: "libvndk_sp_ext",
2009 vendor: true,
2010 vndk: {
2011 enabled: true,
2012 extends: "libvndk_sp",
2013 },
2014 nocrt: true,
2015 }
2016
2017 cc_library {
2018 name: "libvndk_sp2",
2019 vendor_available: true,
2020 vndk: {
2021 enabled: true,
2022 },
2023 target: {
2024 vendor: {
2025 shared_libs: ["libvndk_sp_ext"],
2026 },
2027 },
2028 nocrt: true,
2029 }
2030 `)
2031}
2032
Justin Yun5f7f7e82019-11-18 19:52:14 +09002033func TestEnforceProductVndkVersion(t *testing.T) {
2034 bp := `
2035 cc_library {
2036 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002037 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002038 }
2039 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002040 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002041 symbol_file: "",
2042 }
2043 cc_library {
2044 name: "libvndk",
2045 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002046 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002047 vndk: {
2048 enabled: true,
2049 },
2050 nocrt: true,
2051 }
2052 cc_library {
2053 name: "libvndk_sp",
2054 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002055 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002056 vndk: {
2057 enabled: true,
2058 support_system_process: true,
2059 },
2060 nocrt: true,
2061 }
2062 cc_library {
2063 name: "libva",
2064 vendor_available: true,
2065 nocrt: true,
2066 }
2067 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002068 name: "libpa",
2069 product_available: true,
2070 nocrt: true,
2071 }
2072 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002073 name: "libboth_available",
2074 vendor_available: true,
2075 product_available: true,
2076 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002077 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002078 target: {
2079 vendor: {
2080 suffix: "-vendor",
2081 },
2082 product: {
2083 suffix: "-product",
2084 },
2085 }
2086 }
2087 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002088 name: "libproduct_va",
2089 product_specific: true,
2090 vendor_available: true,
2091 nocrt: true,
2092 }
2093 cc_library {
2094 name: "libprod",
2095 product_specific: true,
2096 shared_libs: [
2097 "libllndk",
2098 "libvndk",
2099 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002100 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002101 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002102 "libproduct_va",
2103 ],
2104 nocrt: true,
2105 }
2106 cc_library {
2107 name: "libvendor",
2108 vendor: true,
2109 shared_libs: [
2110 "libllndk",
2111 "libvndk",
2112 "libvndk_sp",
2113 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002114 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002115 "libproduct_va",
2116 ],
2117 nocrt: true,
2118 }
2119 `
2120
Justin Yun13decfb2021-03-08 19:25:55 +09002121 ctx := ccFixtureFactory.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002122
Jooyung Han261e1582020-10-20 18:54:21 +09002123 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2124 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002125
2126 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2127 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2128
2129 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2130 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002131
2132 ensureStringContains := func(t *testing.T, str string, substr string) {
2133 t.Helper()
2134 if !strings.Contains(str, substr) {
2135 t.Errorf("%q is not found in %v", substr, str)
2136 }
2137 }
2138 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2139 t.Helper()
2140 if strings.Contains(str, substr) {
2141 t.Errorf("%q is found in %v", substr, str)
2142 }
2143 }
2144
2145 // _static variant is used since _shared reuses *.o from the static variant
2146 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2147 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2148
2149 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2150 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2151 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2152 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2153
2154 product_cflags := product_static.Rule("cc").Args["cFlags"]
2155 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2156 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2157 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002158}
2159
2160func TestEnforceProductVndkVersionErrors(t *testing.T) {
2161 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2162 cc_library {
2163 name: "libprod",
2164 product_specific: true,
2165 shared_libs: [
2166 "libvendor",
2167 ],
2168 nocrt: true,
2169 }
2170 cc_library {
2171 name: "libvendor",
2172 vendor: true,
2173 nocrt: true,
2174 }
2175 `)
2176 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2177 cc_library {
2178 name: "libprod",
2179 product_specific: true,
2180 shared_libs: [
2181 "libsystem",
2182 ],
2183 nocrt: true,
2184 }
2185 cc_library {
2186 name: "libsystem",
2187 nocrt: true,
2188 }
2189 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002190 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2191 cc_library {
2192 name: "libprod",
2193 product_specific: true,
2194 shared_libs: [
2195 "libva",
2196 ],
2197 nocrt: true,
2198 }
2199 cc_library {
2200 name: "libva",
2201 vendor_available: true,
2202 nocrt: true,
2203 }
2204 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002205 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 cc_library {
2207 name: "libprod",
2208 product_specific: true,
2209 shared_libs: [
2210 "libvndk_private",
2211 ],
2212 nocrt: true,
2213 }
2214 cc_library {
2215 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002216 vendor_available: true,
2217 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002218 vndk: {
2219 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002220 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002221 },
2222 nocrt: true,
2223 }
2224 `)
2225 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2226 cc_library {
2227 name: "libprod",
2228 product_specific: true,
2229 shared_libs: [
2230 "libsystem_ext",
2231 ],
2232 nocrt: true,
2233 }
2234 cc_library {
2235 name: "libsystem_ext",
2236 system_ext_specific: true,
2237 nocrt: true,
2238 }
2239 `)
2240 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2241 cc_library {
2242 name: "libsystem",
2243 shared_libs: [
2244 "libproduct_va",
2245 ],
2246 nocrt: true,
2247 }
2248 cc_library {
2249 name: "libproduct_va",
2250 product_specific: true,
2251 vendor_available: true,
2252 nocrt: true,
2253 }
2254 `)
2255}
2256
Jooyung Han38002912019-05-16 04:01:54 +09002257func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002258 bp := `
2259 cc_library {
2260 name: "libvndk",
2261 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002262 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002263 vndk: {
2264 enabled: true,
2265 },
2266 }
2267 cc_library {
2268 name: "libvndksp",
2269 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002270 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002271 vndk: {
2272 enabled: true,
2273 support_system_process: true,
2274 },
2275 }
2276 cc_library {
2277 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002278 vendor_available: true,
2279 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002280 vndk: {
2281 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002282 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002283 },
2284 }
2285 cc_library {
2286 name: "libvendor",
2287 vendor: true,
2288 }
2289 cc_library {
2290 name: "libvndkext",
2291 vendor: true,
2292 vndk: {
2293 enabled: true,
2294 extends: "libvndk",
2295 },
2296 }
2297 vndk_prebuilt_shared {
2298 name: "prevndk",
2299 version: "27",
2300 target_arch: "arm",
2301 binder32bit: true,
2302 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002303 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002304 vndk: {
2305 enabled: true,
2306 },
2307 arch: {
2308 arm: {
2309 srcs: ["liba.so"],
2310 },
2311 },
2312 }
2313 cc_library {
2314 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002315 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002316 }
2317 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002318 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002319 symbol_file: "",
2320 }
2321 cc_library {
2322 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002323 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002324 }
2325 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002326 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002327 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002328 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002329 }
2330
2331 llndk_libraries_txt {
2332 name: "llndk.libraries.txt",
2333 }
2334 vndkcore_libraries_txt {
2335 name: "vndkcore.libraries.txt",
2336 }
2337 vndksp_libraries_txt {
2338 name: "vndksp.libraries.txt",
2339 }
2340 vndkprivate_libraries_txt {
2341 name: "vndkprivate.libraries.txt",
2342 }
2343 vndkcorevariant_libraries_txt {
2344 name: "vndkcorevariant.libraries.txt",
2345 insert_vndk_version: false,
2346 }
2347 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002348
2349 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002350 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2351 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2352 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002353 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002354
Colin Cross78212242021-01-06 14:51:30 -08002355 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2356 []string{"libvndk.so", "libvndkprivate.so"})
2357 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2358 []string{"libc++.so", "libvndksp.so"})
2359 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2360 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2361 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2362 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002363
Colin Crossfb0c16e2019-11-20 17:12:35 -08002364 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002365
Jooyung Han38002912019-05-16 04:01:54 +09002366 tests := []struct {
2367 variant string
2368 name string
2369 expected string
2370 }{
2371 {vendorVariant, "libvndk", "native:vndk"},
2372 {vendorVariant, "libvndksp", "native:vndk"},
2373 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2374 {vendorVariant, "libvendor", "native:vendor"},
2375 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002376 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002377 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002378 {coreVariant, "libvndk", "native:platform"},
2379 {coreVariant, "libvndkprivate", "native:platform"},
2380 {coreVariant, "libllndk", "native:platform"},
2381 }
2382 for _, test := range tests {
2383 t.Run(test.name, func(t *testing.T) {
2384 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2385 assertString(t, module.makeLinkType, test.expected)
2386 })
2387 }
2388}
2389
Jeff Gaston294356f2017-09-27 17:05:30 -07002390var staticLinkDepOrderTestCases = []struct {
2391 // This is a string representation of a map[moduleName][]moduleDependency .
2392 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002393 inStatic string
2394
2395 // This is a string representation of a map[moduleName][]moduleDependency .
2396 // It models the dependencies declared in an Android.bp file.
2397 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002398
2399 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2400 // The keys of allOrdered specify which modules we would like to check.
2401 // The values of allOrdered specify the expected result (of the transitive closure of all
2402 // dependencies) for each module to test
2403 allOrdered string
2404
2405 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2406 // The keys of outOrdered specify which modules we would like to check.
2407 // The values of outOrdered specify the expected result (of the ordered linker command line)
2408 // for each module to test.
2409 outOrdered string
2410}{
2411 // Simple tests
2412 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002413 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002414 outOrdered: "",
2415 },
2416 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002417 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002418 outOrdered: "a:",
2419 },
2420 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002421 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002422 outOrdered: "a:b; b:",
2423 },
2424 // Tests of reordering
2425 {
2426 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002427 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002428 outOrdered: "a:b,c,d; b:d; c:d; d:",
2429 },
2430 {
2431 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002432 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002433 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2434 },
2435 {
2436 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002437 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002438 outOrdered: "a:d,b,e,c; d:b; e:c",
2439 },
2440 {
2441 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002442 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002443 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2444 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2445 },
2446 {
2447 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002448 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 -07002449 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2450 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2451 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002452 // shared dependencies
2453 {
2454 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2455 // So, we don't actually have to check that a shared dependency of c will change the order
2456 // of a library that depends statically on b and on c. We only need to check that if c has
2457 // a shared dependency on b, that that shows up in allOrdered.
2458 inShared: "c:b",
2459 allOrdered: "c:b",
2460 outOrdered: "c:",
2461 },
2462 {
2463 // This test doesn't actually include any shared dependencies but it's a reminder of what
2464 // the second phase of the above test would look like
2465 inStatic: "a:b,c; c:b",
2466 allOrdered: "a:c,b; c:b",
2467 outOrdered: "a:c,b; c:b",
2468 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002469 // tiebreakers for when two modules specifying different orderings and there is no dependency
2470 // to dictate an order
2471 {
2472 // 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 -08002473 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002474 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2475 },
2476 {
2477 // 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 -08002478 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 -07002479 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2480 },
2481 // Tests involving duplicate dependencies
2482 {
2483 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002484 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002485 outOrdered: "a:c,b",
2486 },
2487 {
2488 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002489 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002490 outOrdered: "a:d,c,b",
2491 },
2492 // Tests to confirm the nonexistence of infinite loops.
2493 // These cases should never happen, so as long as the test terminates and the
2494 // result is deterministic then that should be fine.
2495 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002496 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002497 outOrdered: "a:a",
2498 },
2499 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002500 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002501 allOrdered: "a:b,c; b:c,a; c:a,b",
2502 outOrdered: "a:b; b:c; c:a",
2503 },
2504 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002505 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002506 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2507 outOrdered: "a:c,b; b:a,c; c:b,a",
2508 },
2509}
2510
2511// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2512func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2513 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2514 strippedText := strings.Replace(text, " ", "", -1)
2515 if len(strippedText) < 1 {
2516 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2517 }
2518 allDeps = make(map[android.Path][]android.Path, 0)
2519
2520 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2521 moduleTexts := strings.Split(strippedText, ";")
2522
2523 outputForModuleName := func(moduleName string) android.Path {
2524 return android.PathForTesting(moduleName)
2525 }
2526
2527 for _, moduleText := range moduleTexts {
2528 // convert from "a:b,c" to ["a", "b,c"]
2529 components := strings.Split(moduleText, ":")
2530 if len(components) != 2 {
2531 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2532 }
2533 moduleName := components[0]
2534 moduleOutput := outputForModuleName(moduleName)
2535 modulesInOrder = append(modulesInOrder, moduleOutput)
2536
2537 depString := components[1]
2538 // convert from "b,c" to ["b", "c"]
2539 depNames := strings.Split(depString, ",")
2540 if len(depString) < 1 {
2541 depNames = []string{}
2542 }
2543 var deps []android.Path
2544 for _, depName := range depNames {
2545 deps = append(deps, outputForModuleName(depName))
2546 }
2547 allDeps[moduleOutput] = deps
2548 }
2549 return modulesInOrder, allDeps
2550}
2551
Jeff Gaston294356f2017-09-27 17:05:30 -07002552func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2553 for _, moduleName := range moduleNames {
2554 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2555 output := module.outputFile.Path()
2556 paths = append(paths, output)
2557 }
2558 return paths
2559}
2560
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002561func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002562 ctx := testCc(t, `
2563 cc_library {
2564 name: "a",
2565 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002566 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 }
2568 cc_library {
2569 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002570 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002571 }
2572 cc_library {
2573 name: "c",
2574 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002575 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002576 }
2577 cc_library {
2578 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002579 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002580 }
2581
2582 `)
2583
Colin Cross7113d202019-11-20 16:39:12 -08002584 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002585 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002586 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2587 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002588
2589 if !reflect.DeepEqual(actual, expected) {
2590 t.Errorf("staticDeps orderings were not propagated correctly"+
2591 "\nactual: %v"+
2592 "\nexpected: %v",
2593 actual,
2594 expected,
2595 )
2596 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002597}
Jeff Gaston294356f2017-09-27 17:05:30 -07002598
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002599func TestStaticLibDepReorderingWithShared(t *testing.T) {
2600 ctx := testCc(t, `
2601 cc_library {
2602 name: "a",
2603 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002604 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002605 }
2606 cc_library {
2607 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002608 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002609 }
2610 cc_library {
2611 name: "c",
2612 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002613 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002614 }
2615
2616 `)
2617
Colin Cross7113d202019-11-20 16:39:12 -08002618 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002619 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002620 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2621 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002622
2623 if !reflect.DeepEqual(actual, expected) {
2624 t.Errorf("staticDeps orderings did not account for shared libs"+
2625 "\nactual: %v"+
2626 "\nexpected: %v",
2627 actual,
2628 expected,
2629 )
2630 }
2631}
2632
Jooyung Hanb04a4992020-03-13 18:57:35 +09002633func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002634 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002635 if !reflect.DeepEqual(actual, expected) {
2636 t.Errorf(message+
2637 "\nactual: %v"+
2638 "\nexpected: %v",
2639 actual,
2640 expected,
2641 )
2642 }
2643}
2644
Jooyung Han61b66e92020-03-21 14:21:46 +00002645func TestLlndkLibrary(t *testing.T) {
2646 ctx := testCc(t, `
2647 cc_library {
2648 name: "libllndk",
2649 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002650 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002651 }
2652 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002653 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002654 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002655
2656 cc_prebuilt_library_shared {
2657 name: "libllndkprebuilt",
2658 stubs: { versions: ["1", "2"] },
2659 llndk_stubs: "libllndkprebuilt.llndk",
2660 }
2661 llndk_library {
2662 name: "libllndkprebuilt.llndk",
2663 }
2664
2665 cc_library {
2666 name: "libllndk_with_external_headers",
2667 stubs: { versions: ["1", "2"] },
2668 llndk_stubs: "libllndk_with_external_headers.llndk",
2669 header_libs: ["libexternal_headers"],
2670 export_header_lib_headers: ["libexternal_headers"],
2671 }
2672 llndk_library {
2673 name: "libllndk_with_external_headers.llndk",
2674 }
2675 cc_library_headers {
2676 name: "libexternal_headers",
2677 export_include_dirs: ["include"],
2678 vendor_available: true,
2679 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002680 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002681 actual := ctx.ModuleVariantsForTests("libllndk")
2682 for i := 0; i < len(actual); i++ {
2683 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2684 actual = append(actual[:i], actual[i+1:]...)
2685 i--
2686 }
2687 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002688 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002689 "android_vendor.VER_arm64_armv8-a_shared_1",
2690 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002691 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002692 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2693 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002694 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002695 }
2696 checkEquals(t, "variants for llndk stubs", expected, actual)
2697
Colin Cross127bb8b2020-12-16 16:46:01 -08002698 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002699 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2700
Colin Cross127bb8b2020-12-16 16:46:01 -08002701 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002702 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2703}
2704
Jiyong Parka46a4d52017-12-14 19:54:34 +09002705func TestLlndkHeaders(t *testing.T) {
2706 ctx := testCc(t, `
2707 llndk_headers {
2708 name: "libllndk_headers",
2709 export_include_dirs: ["my_include"],
2710 }
2711 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002712 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002713 export_llndk_headers: ["libllndk_headers"],
2714 }
2715 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002716 name: "libllndk",
2717 llndk_stubs: "libllndk.llndk",
2718 }
2719
2720 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002721 name: "libvendor",
2722 shared_libs: ["libllndk"],
2723 vendor: true,
2724 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002725 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002726 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002727 }
2728 `)
2729
2730 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002731 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002732 cflags := cc.Args["cFlags"]
2733 if !strings.Contains(cflags, "-Imy_include") {
2734 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2735 }
2736}
2737
Logan Chien43d34c32017-12-20 01:17:32 +08002738func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2739 actual := module.Properties.AndroidMkRuntimeLibs
2740 if !reflect.DeepEqual(actual, expected) {
2741 t.Errorf("incorrect runtime_libs for shared libs"+
2742 "\nactual: %v"+
2743 "\nexpected: %v",
2744 actual,
2745 expected,
2746 )
2747 }
2748}
2749
2750const runtimeLibAndroidBp = `
2751 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002752 name: "liball_available",
2753 vendor_available: true,
2754 product_available: true,
2755 no_libcrt : true,
2756 nocrt : true,
2757 system_shared_libs : [],
2758 }
2759 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002760 name: "libvendor_available1",
2761 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002762 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002763 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002764 nocrt : true,
2765 system_shared_libs : [],
2766 }
2767 cc_library {
2768 name: "libvendor_available2",
2769 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002770 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002771 target: {
2772 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002773 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002774 }
2775 },
Yi Konge7fe9912019-06-02 00:53:50 -07002776 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002777 nocrt : true,
2778 system_shared_libs : [],
2779 }
2780 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002781 name: "libproduct_vendor",
2782 product_specific: true,
2783 vendor_available: true,
2784 no_libcrt : true,
2785 nocrt : true,
2786 system_shared_libs : [],
2787 }
2788 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002789 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002790 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002791 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002792 nocrt : true,
2793 system_shared_libs : [],
2794 }
2795 cc_library {
2796 name: "libvendor1",
2797 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002798 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002799 nocrt : true,
2800 system_shared_libs : [],
2801 }
2802 cc_library {
2803 name: "libvendor2",
2804 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002805 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002806 no_libcrt : true,
2807 nocrt : true,
2808 system_shared_libs : [],
2809 }
2810 cc_library {
2811 name: "libproduct_available1",
2812 product_available: true,
2813 runtime_libs: ["liball_available"],
2814 no_libcrt : true,
2815 nocrt : true,
2816 system_shared_libs : [],
2817 }
2818 cc_library {
2819 name: "libproduct1",
2820 product_specific: true,
2821 no_libcrt : true,
2822 nocrt : true,
2823 system_shared_libs : [],
2824 }
2825 cc_library {
2826 name: "libproduct2",
2827 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002828 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002829 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002830 nocrt : true,
2831 system_shared_libs : [],
2832 }
2833`
2834
2835func TestRuntimeLibs(t *testing.T) {
2836 ctx := testCc(t, runtimeLibAndroidBp)
2837
2838 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002839 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002840
Justin Yun8a2600c2020-12-07 12:44:03 +09002841 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2842 checkRuntimeLibs(t, []string{"liball_available"}, module)
2843
2844 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2845 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002846
2847 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002848 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002849
2850 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2851 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002852 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002853
Justin Yun8a2600c2020-12-07 12:44:03 +09002854 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2855 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002856
2857 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002858 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002859
2860 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2861 // and product variants.
2862 variant = "android_product.VER_arm64_armv8-a_shared"
2863
2864 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2865 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2866
2867 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002868 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002869}
2870
2871func TestExcludeRuntimeLibs(t *testing.T) {
2872 ctx := testCc(t, runtimeLibAndroidBp)
2873
Colin Cross7113d202019-11-20 16:39:12 -08002874 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002875 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2876 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002877
Colin Crossfb0c16e2019-11-20 17:12:35 -08002878 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002879 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002880 checkRuntimeLibs(t, nil, module)
2881}
2882
2883func TestRuntimeLibsNoVndk(t *testing.T) {
2884 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2885
2886 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2887
Colin Cross7113d202019-11-20 16:39:12 -08002888 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002889
Justin Yun8a2600c2020-12-07 12:44:03 +09002890 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2891 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002892
2893 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002894 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002895
2896 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002897 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002898}
2899
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002900func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002901 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002902 actual := module.Properties.AndroidMkStaticLibs
2903 if !reflect.DeepEqual(actual, expected) {
2904 t.Errorf("incorrect static_libs"+
2905 "\nactual: %v"+
2906 "\nexpected: %v",
2907 actual,
2908 expected,
2909 )
2910 }
2911}
2912
2913const staticLibAndroidBp = `
2914 cc_library {
2915 name: "lib1",
2916 }
2917 cc_library {
2918 name: "lib2",
2919 static_libs: ["lib1"],
2920 }
2921`
2922
2923func TestStaticLibDepExport(t *testing.T) {
2924 ctx := testCc(t, staticLibAndroidBp)
2925
2926 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002927 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002928 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002929 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002930
2931 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002932 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002933 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2934 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002935 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002936}
2937
Jiyong Parkd08b6972017-09-26 10:50:54 +09002938var compilerFlagsTestCases = []struct {
2939 in string
2940 out bool
2941}{
2942 {
2943 in: "a",
2944 out: false,
2945 },
2946 {
2947 in: "-a",
2948 out: true,
2949 },
2950 {
2951 in: "-Ipath/to/something",
2952 out: false,
2953 },
2954 {
2955 in: "-isystempath/to/something",
2956 out: false,
2957 },
2958 {
2959 in: "--coverage",
2960 out: false,
2961 },
2962 {
2963 in: "-include a/b",
2964 out: true,
2965 },
2966 {
2967 in: "-include a/b c/d",
2968 out: false,
2969 },
2970 {
2971 in: "-DMACRO",
2972 out: true,
2973 },
2974 {
2975 in: "-DMAC RO",
2976 out: false,
2977 },
2978 {
2979 in: "-a -b",
2980 out: false,
2981 },
2982 {
2983 in: "-DMACRO=definition",
2984 out: true,
2985 },
2986 {
2987 in: "-DMACRO=defi nition",
2988 out: true, // TODO(jiyong): this should be false
2989 },
2990 {
2991 in: "-DMACRO(x)=x + 1",
2992 out: true,
2993 },
2994 {
2995 in: "-DMACRO=\"defi nition\"",
2996 out: true,
2997 },
2998}
2999
3000type mockContext struct {
3001 BaseModuleContext
3002 result bool
3003}
3004
3005func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3006 // CheckBadCompilerFlags calls this function when the flag should be rejected
3007 ctx.result = false
3008}
3009
3010func TestCompilerFlags(t *testing.T) {
3011 for _, testCase := range compilerFlagsTestCases {
3012 ctx := &mockContext{result: true}
3013 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3014 if ctx.result != testCase.out {
3015 t.Errorf("incorrect output:")
3016 t.Errorf(" input: %#v", testCase.in)
3017 t.Errorf(" expected: %#v", testCase.out)
3018 t.Errorf(" got: %#v", ctx.result)
3019 }
3020 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003021}
Jiyong Park374510b2018-03-19 18:23:01 +09003022
3023func TestVendorPublicLibraries(t *testing.T) {
3024 ctx := testCc(t, `
3025 cc_library_headers {
3026 name: "libvendorpublic_headers",
3027 export_include_dirs: ["my_include"],
3028 }
3029 vendor_public_library {
3030 name: "libvendorpublic",
3031 symbol_file: "",
3032 export_public_headers: ["libvendorpublic_headers"],
3033 }
3034 cc_library {
3035 name: "libvendorpublic",
3036 srcs: ["foo.c"],
3037 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003038 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003039 nocrt: true,
3040 }
3041
3042 cc_library {
3043 name: "libsystem",
3044 shared_libs: ["libvendorpublic"],
3045 vendor: false,
3046 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003047 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003048 nocrt: true,
3049 }
3050 cc_library {
3051 name: "libvendor",
3052 shared_libs: ["libvendorpublic"],
3053 vendor: true,
3054 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003055 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003056 nocrt: true,
3057 }
3058 `)
3059
Colin Cross7113d202019-11-20 16:39:12 -08003060 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003061 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003062
3063 // test if header search paths are correctly added
3064 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003065 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003066 cflags := cc.Args["cFlags"]
3067 if !strings.Contains(cflags, "-Imy_include") {
3068 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3069 }
3070
3071 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003072 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003073 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003074 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003075 if !strings.Contains(libflags, stubPaths[0].String()) {
3076 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3077 }
3078
3079 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003080 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003081 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003082 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003083 if !strings.Contains(libflags, stubPaths[0].String()) {
3084 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3085 }
3086
3087}
Jiyong Park37b25202018-07-11 10:49:27 +09003088
3089func TestRecovery(t *testing.T) {
3090 ctx := testCc(t, `
3091 cc_library_shared {
3092 name: "librecovery",
3093 recovery: true,
3094 }
3095 cc_library_shared {
3096 name: "librecovery32",
3097 recovery: true,
3098 compile_multilib:"32",
3099 }
Jiyong Park5baac542018-08-28 09:55:37 +09003100 cc_library_shared {
3101 name: "libHalInRecovery",
3102 recovery_available: true,
3103 vendor: true,
3104 }
Jiyong Park37b25202018-07-11 10:49:27 +09003105 `)
3106
3107 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003108 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003109 if len(variants) != 1 || !android.InList(arm64, variants) {
3110 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3111 }
3112
3113 variants = ctx.ModuleVariantsForTests("librecovery32")
3114 if android.InList(arm64, variants) {
3115 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3116 }
Jiyong Park5baac542018-08-28 09:55:37 +09003117
3118 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3119 if !recoveryModule.Platform() {
3120 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3121 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003122}
Jiyong Park5baac542018-08-28 09:55:37 +09003123
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003124func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3125 bp := `
3126 cc_prebuilt_test_library_shared {
3127 name: "test_lib",
3128 relative_install_path: "foo/bar/baz",
3129 srcs: ["srcpath/dontusethispath/baz.so"],
3130 }
3131
3132 cc_test {
3133 name: "main_test",
3134 data_libs: ["test_lib"],
3135 gtest: false,
3136 }
3137 `
3138
3139 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3140 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3141 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3142 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3143
3144 ctx := testCcWithConfig(t, config)
3145 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3146 testBinary := module.(*Module).linker.(*testBinary)
3147 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3148 if err != nil {
3149 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3150 }
3151 if len(outputFiles) != 1 {
3152 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3153 }
3154 if len(testBinary.dataPaths()) != 1 {
3155 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3156 }
3157
3158 outputPath := outputFiles[0].String()
3159
3160 if !strings.HasSuffix(outputPath, "/main_test") {
3161 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3162 }
Colin Crossaa255532020-07-03 13:18:24 -07003163 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003164 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3165 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3166 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3167 }
3168}
3169
Jiyong Park7ed9de32018-10-15 22:25:07 +09003170func TestVersionedStubs(t *testing.T) {
3171 ctx := testCc(t, `
3172 cc_library_shared {
3173 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003174 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003175 stubs: {
3176 symbol_file: "foo.map.txt",
3177 versions: ["1", "2", "3"],
3178 },
3179 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003180
Jiyong Park7ed9de32018-10-15 22:25:07 +09003181 cc_library_shared {
3182 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003183 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003184 shared_libs: ["libFoo#1"],
3185 }`)
3186
3187 variants := ctx.ModuleVariantsForTests("libFoo")
3188 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003189 "android_arm64_armv8-a_shared",
3190 "android_arm64_armv8-a_shared_1",
3191 "android_arm64_armv8-a_shared_2",
3192 "android_arm64_armv8-a_shared_3",
3193 "android_arm_armv7-a-neon_shared",
3194 "android_arm_armv7-a-neon_shared_1",
3195 "android_arm_armv7-a-neon_shared_2",
3196 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003197 }
3198 variantsMismatch := false
3199 if len(variants) != len(expectedVariants) {
3200 variantsMismatch = true
3201 } else {
3202 for _, v := range expectedVariants {
3203 if !inList(v, variants) {
3204 variantsMismatch = false
3205 }
3206 }
3207 }
3208 if variantsMismatch {
3209 t.Errorf("variants of libFoo expected:\n")
3210 for _, v := range expectedVariants {
3211 t.Errorf("%q\n", v)
3212 }
3213 t.Errorf(", but got:\n")
3214 for _, v := range variants {
3215 t.Errorf("%q\n", v)
3216 }
3217 }
3218
Colin Cross7113d202019-11-20 16:39:12 -08003219 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003220 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003221 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003222 if !strings.Contains(libFlags, libFoo1StubPath) {
3223 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3224 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003225
Colin Cross7113d202019-11-20 16:39:12 -08003226 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003227 cFlags := libBarCompileRule.Args["cFlags"]
3228 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3229 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3230 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3231 }
Jiyong Park37b25202018-07-11 10:49:27 +09003232}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003233
Jooyung Hanb04a4992020-03-13 18:57:35 +09003234func TestVersioningMacro(t *testing.T) {
3235 for _, tc := range []struct{ moduleName, expected string }{
3236 {"libc", "__LIBC_API__"},
3237 {"libfoo", "__LIBFOO_API__"},
3238 {"libfoo@1", "__LIBFOO_1_API__"},
3239 {"libfoo-v1", "__LIBFOO_V1_API__"},
3240 {"libfoo.v1", "__LIBFOO_V1_API__"},
3241 } {
3242 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3243 }
3244}
3245
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003246func TestStaticExecutable(t *testing.T) {
3247 ctx := testCc(t, `
3248 cc_binary {
3249 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003250 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003251 static_executable: true,
3252 }`)
3253
Colin Cross7113d202019-11-20 16:39:12 -08003254 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003255 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3256 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003257 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003258 for _, lib := range systemStaticLibs {
3259 if !strings.Contains(libFlags, lib) {
3260 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3261 }
3262 }
3263 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3264 for _, lib := range systemSharedLibs {
3265 if strings.Contains(libFlags, lib) {
3266 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3267 }
3268 }
3269}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003270
3271func TestStaticDepsOrderWithStubs(t *testing.T) {
3272 ctx := testCc(t, `
3273 cc_binary {
3274 name: "mybin",
3275 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003276 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003277 static_executable: true,
3278 stl: "none",
3279 }
3280
3281 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003282 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003283 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003284 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003285 stl: "none",
3286 }
3287
3288 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003289 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003290 srcs: ["foo.c"],
3291 stl: "none",
3292 stubs: {
3293 versions: ["1"],
3294 },
3295 }`)
3296
Colin Cross0de8a1e2020-09-18 14:15:30 -07003297 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3298 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003299 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003300
3301 if !reflect.DeepEqual(actual, expected) {
3302 t.Errorf("staticDeps orderings were not propagated correctly"+
3303 "\nactual: %v"+
3304 "\nexpected: %v",
3305 actual,
3306 expected,
3307 )
3308 }
3309}
Jooyung Han38002912019-05-16 04:01:54 +09003310
Jooyung Hand48f3c32019-08-23 11:18:57 +09003311func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3312 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3313 cc_library {
3314 name: "libA",
3315 srcs: ["foo.c"],
3316 shared_libs: ["libB"],
3317 stl: "none",
3318 }
3319
3320 cc_library {
3321 name: "libB",
3322 srcs: ["foo.c"],
3323 enabled: false,
3324 stl: "none",
3325 }
3326 `)
3327}
3328
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003329// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3330// correctly.
3331func TestFuzzTarget(t *testing.T) {
3332 ctx := testCc(t, `
3333 cc_fuzz {
3334 name: "fuzz_smoke_test",
3335 srcs: ["foo.c"],
3336 }`)
3337
Paul Duffin075c4172019-12-19 19:06:13 +00003338 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003339 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3340}
3341
Jiyong Park29074592019-07-07 16:27:47 +09003342func TestAidl(t *testing.T) {
3343}
3344
Jooyung Han38002912019-05-16 04:01:54 +09003345func assertString(t *testing.T, got, expected string) {
3346 t.Helper()
3347 if got != expected {
3348 t.Errorf("expected %q got %q", expected, got)
3349 }
3350}
3351
3352func assertArrayString(t *testing.T, got, expected []string) {
3353 t.Helper()
3354 if len(got) != len(expected) {
3355 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3356 return
3357 }
3358 for i := range got {
3359 if got[i] != expected[i] {
3360 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3361 i, expected[i], expected, got[i], got)
3362 return
3363 }
3364 }
3365}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003366
Jooyung Han0302a842019-10-30 18:43:49 +09003367func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3368 t.Helper()
3369 assertArrayString(t, android.SortedStringKeys(m), expected)
3370}
3371
Colin Crosse1bb5d02019-09-24 14:55:04 -07003372func TestDefaults(t *testing.T) {
3373 ctx := testCc(t, `
3374 cc_defaults {
3375 name: "defaults",
3376 srcs: ["foo.c"],
3377 static: {
3378 srcs: ["bar.c"],
3379 },
3380 shared: {
3381 srcs: ["baz.c"],
3382 },
3383 }
3384
3385 cc_library_static {
3386 name: "libstatic",
3387 defaults: ["defaults"],
3388 }
3389
3390 cc_library_shared {
3391 name: "libshared",
3392 defaults: ["defaults"],
3393 }
3394
3395 cc_library {
3396 name: "libboth",
3397 defaults: ["defaults"],
3398 }
3399
3400 cc_binary {
3401 name: "binary",
3402 defaults: ["defaults"],
3403 }`)
3404
3405 pathsToBase := func(paths android.Paths) []string {
3406 var ret []string
3407 for _, p := range paths {
3408 ret = append(ret, p.Base())
3409 }
3410 return ret
3411 }
3412
Colin Cross7113d202019-11-20 16:39:12 -08003413 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003414 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3415 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3416 }
Colin Cross7113d202019-11-20 16:39:12 -08003417 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003418 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3419 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3420 }
Colin Cross7113d202019-11-20 16:39:12 -08003421 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003422 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3423 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3424 }
3425
Colin Cross7113d202019-11-20 16:39:12 -08003426 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003427 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3428 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3429 }
Colin Cross7113d202019-11-20 16:39:12 -08003430 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003431 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3432 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3433 }
3434}
Colin Crosseabaedd2020-02-06 17:01:55 -08003435
3436func TestProductVariableDefaults(t *testing.T) {
3437 bp := `
3438 cc_defaults {
3439 name: "libfoo_defaults",
3440 srcs: ["foo.c"],
3441 cppflags: ["-DFOO"],
3442 product_variables: {
3443 debuggable: {
3444 cppflags: ["-DBAR"],
3445 },
3446 },
3447 }
3448
3449 cc_library {
3450 name: "libfoo",
3451 defaults: ["libfoo_defaults"],
3452 }
3453 `
3454
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003455 result := ccFixtureFactory.Extend(
3456 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003457
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003458 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3459 variables.Debuggable = BoolPtr(true)
3460 }),
3461 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003462
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003463 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003464 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003465}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003466
3467func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3468 t.Parallel()
3469 bp := `
3470 cc_library_static {
3471 name: "libfoo",
3472 srcs: ["foo.c"],
3473 whole_static_libs: ["libbar"],
3474 }
3475
3476 cc_library_static {
3477 name: "libbar",
3478 whole_static_libs: ["libmissing"],
3479 }
3480 `
3481
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003482 result := ccFixtureFactory.Extend(
3483 android.PrepareForTestWithAllowMissingDependencies,
3484 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003485
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003486 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003487 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003488
Paul Duffine84b1332021-03-12 11:59:43 +00003489 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003490
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003491 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003492 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003493}
Colin Crosse9fe2942020-11-10 18:12:15 -08003494
3495func TestInstallSharedLibs(t *testing.T) {
3496 bp := `
3497 cc_binary {
3498 name: "bin",
3499 host_supported: true,
3500 shared_libs: ["libshared"],
3501 runtime_libs: ["libruntime"],
3502 srcs: [":gen"],
3503 }
3504
3505 cc_library_shared {
3506 name: "libshared",
3507 host_supported: true,
3508 shared_libs: ["libtransitive"],
3509 }
3510
3511 cc_library_shared {
3512 name: "libtransitive",
3513 host_supported: true,
3514 }
3515
3516 cc_library_shared {
3517 name: "libruntime",
3518 host_supported: true,
3519 }
3520
3521 cc_binary_host {
3522 name: "tool",
3523 srcs: ["foo.cpp"],
3524 }
3525
3526 genrule {
3527 name: "gen",
3528 tools: ["tool"],
3529 out: ["gen.cpp"],
3530 cmd: "$(location tool) $(out)",
3531 }
3532 `
3533
3534 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3535 ctx := testCcWithConfig(t, config)
3536
3537 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3538 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3539 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3540 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3541 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3542
3543 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3544 t.Errorf("expected host bin dependency %q, got %q", w, g)
3545 }
3546
3547 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3548 t.Errorf("expected host bin dependency %q, got %q", w, g)
3549 }
3550
3551 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3552 t.Errorf("expected host bin dependency %q, got %q", w, g)
3553 }
3554
3555 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3556 t.Errorf("expected host bin dependency %q, got %q", w, g)
3557 }
3558
3559 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3560 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3561 }
3562
3563 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3564 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3565 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3566 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3567
3568 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3569 t.Errorf("expected device bin dependency %q, got %q", w, g)
3570 }
3571
3572 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3573 t.Errorf("expected device bin dependency %q, got %q", w, g)
3574 }
3575
3576 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3577 t.Errorf("expected device bin dependency %q, got %q", w, g)
3578 }
3579
3580 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3581 t.Errorf("expected device bin dependency %q, got %q", w, g)
3582 }
3583
3584 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3585 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3586 }
3587
3588}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003589
3590func TestStubsLibReexportsHeaders(t *testing.T) {
3591 ctx := testCc(t, `
3592 cc_library_shared {
3593 name: "libclient",
3594 srcs: ["foo.c"],
3595 shared_libs: ["libfoo#1"],
3596 }
3597
3598 cc_library_shared {
3599 name: "libfoo",
3600 srcs: ["foo.c"],
3601 shared_libs: ["libbar"],
3602 export_shared_lib_headers: ["libbar"],
3603 stubs: {
3604 symbol_file: "foo.map.txt",
3605 versions: ["1", "2", "3"],
3606 },
3607 }
3608
3609 cc_library_shared {
3610 name: "libbar",
3611 export_include_dirs: ["include/libbar"],
3612 srcs: ["foo.c"],
3613 }`)
3614
3615 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3616
3617 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3618 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3619 }
3620}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003621
3622func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3623 ctx := testCc(t, `
3624 cc_library {
3625 name: "libfoo",
3626 srcs: ["a/Foo.aidl"],
3627 aidl: { flags: ["-Werror"], },
3628 }
3629 `)
3630
3631 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3632 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3633 aidlCommand := manifest.Commands[0].GetCommand()
3634 expectedAidlFlag := "-Werror"
3635 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3636 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3637 }
3638}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003639
Jiyong Parka008fb02021-03-16 17:15:53 +09003640func TestMinSdkVersionInClangTriple(t *testing.T) {
3641 ctx := testCc(t, `
3642 cc_library_shared {
3643 name: "libfoo",
3644 srcs: ["foo.c"],
3645 min_sdk_version: "29",
3646 }`)
3647
3648 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3649 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3650}
3651
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003652func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3653 ctx := testCc(t, `
3654 cc_object {
3655 name: "crt_foo",
3656 srcs: ["foo.c"],
3657 crt: true,
3658 stl: "none",
3659 min_sdk_version: "28",
3660
3661 }`)
3662
3663 arch := "android_arm64_armv8-a"
3664 for _, v := range []string{"", "28", "29", "30", "current"} {
3665 var variant string
3666 if v == "" {
3667 variant = arch
3668 } else {
3669 variant = arch + "_sdk_" + v
3670 }
3671 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3672 vNum := v
3673 if v == "current" || v == "" {
3674 vNum = "10000"
3675 }
3676 expected := "-target aarch64-linux-android" + vNum + " "
3677 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3678 }
3679}
3680
3681func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3682 ctx := testCc(t, `
3683 cc_binary {
3684 name: "bin",
3685 srcs: ["foo.c"],
3686 stl: "none",
3687 min_sdk_version: "29",
3688 sdk_version: "current",
3689 }
3690 `)
3691
3692 // Sdk variant uses the crt object of the matching min_sdk_version
3693 variant := "android_arm64_armv8-a_sdk"
3694 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3695 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3696 variant+"_29/crtbegin_dynamic.o")
3697
3698 // platform variant uses the crt object built for platform
3699 variant = "android_arm64_armv8-a"
3700 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3701 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3702 variant+"/crtbegin_dynamic.o")
3703}
3704
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003705type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003706
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003707const (
3708 None MemtagNoteType = iota + 1
3709 Sync
3710 Async
3711)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003712
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003713func (t MemtagNoteType) str() string {
3714 switch t {
3715 case None:
3716 return "none"
3717 case Sync:
3718 return "sync"
3719 case Async:
3720 return "async"
3721 default:
3722 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003723 }
3724}
3725
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003726func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3727 note_async := "note_memtag_heap_async"
3728 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003729
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003730 found := None
3731 implicits := m.Rule("ld").Implicits
3732 for _, lib := range implicits {
3733 if strings.Contains(lib.Rel(), note_async) {
3734 found = Async
3735 break
3736 } else if strings.Contains(lib.Rel(), note_sync) {
3737 found = Sync
3738 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003739 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003740 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003741
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003742 if found != expected {
3743 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3744 }
3745}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003746
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003747var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3748 android.FixtureModifyMockFS(func(fs android.MockFS) {
3749 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003750 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003751 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003752 gtest: false,
3753 }
3754
3755 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003756 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003757 gtest: false,
3758 sanitize: { memtag_heap: false },
3759 }
3760
3761 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003762 name: "%[1]s_test_true",
3763 gtest: false,
3764 sanitize: { memtag_heap: true },
3765 }
3766
3767 cc_test {
3768 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003769 gtest: false,
3770 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3771 }
3772
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003773 cc_test {
3774 name: "%[1]s_test_true_diag",
3775 gtest: false,
3776 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3777 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003778
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003779 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003780 name: "%[1]s_binary",
3781 }
3782
3783 cc_binary {
3784 name: "%[1]s_binary_false",
3785 sanitize: { memtag_heap: false },
3786 }
3787
3788 cc_binary {
3789 name: "%[1]s_binary_true",
3790 sanitize: { memtag_heap: true },
3791 }
3792
3793 cc_binary {
3794 name: "%[1]s_binary_true_nodiag",
3795 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3796 }
3797
3798 cc_binary {
3799 name: "%[1]s_binary_true_diag",
3800 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003801 }
3802 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003803 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3804 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3805 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3806 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003807
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003808 fs.Merge(android.MockFS{
3809 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3810 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3811 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3812 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3813 })
3814 }),
3815 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3816 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3817 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3818 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3819 }),
3820)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003821
3822func TestSanitizeMemtagHeap(t *testing.T) {
3823 variant := "android_arm64_armv8-a"
3824
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003825 result := ccFixtureFactory.Extend(prepareForTestWithMemtagHeap).RunTest(t)
3826 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003827
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003828 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3829 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3830 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3833
3834 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3835 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3839
3840 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3841 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3845
3846 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3847 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3851
3852 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3853 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3857
3858 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3859 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3862 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3863
3864 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3865 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3868 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3869
3870 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3871 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3874 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3875}
3876
3877func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003878 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003879
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003880 result := ccFixtureFactory.Extend(
3881 prepareForTestWithMemtagHeap,
3882 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3883 variables.SanitizeDevice = []string{"memtag_heap"}
3884 }),
3885 ).RunTest(t)
3886 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003887
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003888 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3889 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003893
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003894 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3895 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3896 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3899
3900 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3901 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3902 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3905
3906 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3907 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3911
3912 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3913 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3917
3918 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3919 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3923
3924 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3925 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3929
3930 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3931 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3932 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3934 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3935}
3936
3937func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3938 variant := "android_arm64_armv8-a"
3939
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003940 result := ccFixtureFactory.Extend(
3941 prepareForTestWithMemtagHeap,
3942 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3943 variables.SanitizeDevice = []string{"memtag_heap"}
3944 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3945 }),
3946 ).RunTest(t)
3947 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003948
3949 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3954
3955 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3956 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3960
3961 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3966
3967 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3968 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3972
3973 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3978
3979 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3980 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3984
3985 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3990
3991 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3992 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3993 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3994 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3995 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003996}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003997
3998func TestIncludeDirsExporting(t *testing.T) {
3999
4000 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4001 // embedded newline characters alone.
4002 trimIndentingSpaces := func(s string) string {
4003 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4004 }
4005
4006 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4007 t.Helper()
4008 expected = trimIndentingSpaces(expected)
4009 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4010 if expected != actual {
4011 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4012 }
4013 }
4014
4015 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4016
4017 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4018 t.Helper()
4019 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4020 name := module.Name()
4021
4022 for _, checker := range checkers {
4023 checker(t, name, exported)
4024 }
4025 }
4026
4027 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4028 return func(t *testing.T, name string, exported FlagExporterInfo) {
4029 t.Helper()
4030 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4031 }
4032 }
4033
4034 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4035 return func(t *testing.T, name string, exported FlagExporterInfo) {
4036 t.Helper()
4037 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4038 }
4039 }
4040
4041 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4042 return func(t *testing.T, name string, exported FlagExporterInfo) {
4043 t.Helper()
4044 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4045 }
4046 }
4047
4048 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4049 return func(t *testing.T, name string, exported FlagExporterInfo) {
4050 t.Helper()
4051 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4052 }
4053 }
4054
4055 genRuleModules := `
4056 genrule {
4057 name: "genrule_foo",
4058 cmd: "generate-foo",
4059 out: [
4060 "generated_headers/foo/generated_header.h",
4061 ],
4062 export_include_dirs: [
4063 "generated_headers",
4064 ],
4065 }
4066
4067 genrule {
4068 name: "genrule_bar",
4069 cmd: "generate-bar",
4070 out: [
4071 "generated_headers/bar/generated_header.h",
4072 ],
4073 export_include_dirs: [
4074 "generated_headers",
4075 ],
4076 }
4077 `
4078
4079 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4080 ctx := testCc(t, genRuleModules+`
4081 cc_library {
4082 name: "libfoo",
4083 srcs: ["foo.c"],
4084 export_include_dirs: ["foo/standard"],
4085 export_system_include_dirs: ["foo/system"],
4086 generated_headers: ["genrule_foo"],
4087 export_generated_headers: ["genrule_foo"],
4088 }
4089
4090 cc_library {
4091 name: "libbar",
4092 srcs: ["bar.c"],
4093 shared_libs: ["libfoo"],
4094 export_include_dirs: ["bar/standard"],
4095 export_system_include_dirs: ["bar/system"],
4096 generated_headers: ["genrule_bar"],
4097 export_generated_headers: ["genrule_bar"],
4098 }
4099 `)
4100 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4101 checkIncludeDirs(t, ctx, foo,
4102 expectedIncludeDirs(`
4103 foo/standard
4104 .intermediates/genrule_foo/gen/generated_headers
4105 `),
4106 expectedSystemIncludeDirs(`foo/system`),
4107 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4108 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4109 )
4110
4111 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4112 checkIncludeDirs(t, ctx, bar,
4113 expectedIncludeDirs(`
4114 bar/standard
4115 .intermediates/genrule_bar/gen/generated_headers
4116 `),
4117 expectedSystemIncludeDirs(`bar/system`),
4118 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4119 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4120 )
4121 })
4122
4123 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4124 ctx := testCc(t, genRuleModules+`
4125 cc_library {
4126 name: "libfoo",
4127 srcs: ["foo.c"],
4128 export_include_dirs: ["foo/standard"],
4129 export_system_include_dirs: ["foo/system"],
4130 generated_headers: ["genrule_foo"],
4131 export_generated_headers: ["genrule_foo"],
4132 }
4133
4134 cc_library {
4135 name: "libbar",
4136 srcs: ["bar.c"],
4137 whole_static_libs: ["libfoo"],
4138 export_include_dirs: ["bar/standard"],
4139 export_system_include_dirs: ["bar/system"],
4140 generated_headers: ["genrule_bar"],
4141 export_generated_headers: ["genrule_bar"],
4142 }
4143 `)
4144 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4145 checkIncludeDirs(t, ctx, foo,
4146 expectedIncludeDirs(`
4147 foo/standard
4148 .intermediates/genrule_foo/gen/generated_headers
4149 `),
4150 expectedSystemIncludeDirs(`foo/system`),
4151 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4152 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4153 )
4154
4155 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4156 checkIncludeDirs(t, ctx, bar,
4157 expectedIncludeDirs(`
4158 bar/standard
4159 foo/standard
4160 .intermediates/genrule_foo/gen/generated_headers
4161 .intermediates/genrule_bar/gen/generated_headers
4162 `),
4163 expectedSystemIncludeDirs(`
4164 bar/system
4165 foo/system
4166 `),
4167 expectedGeneratedHeaders(`
4168 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4169 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4170 `),
4171 expectedOrderOnlyDeps(`
4172 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4173 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4174 `),
4175 )
4176 })
4177
Paul Duffin3cb603e2021-02-19 13:57:10 +00004178 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4179 ctx := testCc(t, genRuleModules+`
4180 cc_library_shared {
4181 name: "libfoo",
4182 srcs: [
4183 "foo.c",
4184 "b.aidl",
4185 "a.proto",
4186 ],
4187 aidl: {
4188 export_aidl_headers: true,
4189 }
4190 }
4191 `)
4192 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4193 checkIncludeDirs(t, ctx, foo,
4194 expectedIncludeDirs(`
4195 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4196 `),
4197 expectedSystemIncludeDirs(``),
4198 expectedGeneratedHeaders(`
4199 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4200 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4201 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004202 `),
4203 expectedOrderOnlyDeps(`
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 )
4209 })
4210
Paul Duffin3cb603e2021-02-19 13:57:10 +00004211 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4212 ctx := testCc(t, genRuleModules+`
4213 cc_library_shared {
4214 name: "libfoo",
4215 srcs: [
4216 "foo.c",
4217 "b.aidl",
4218 "a.proto",
4219 ],
4220 proto: {
4221 export_proto_headers: true,
4222 }
4223 }
4224 `)
4225 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4226 checkIncludeDirs(t, ctx, foo,
4227 expectedIncludeDirs(`
4228 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4229 `),
4230 expectedSystemIncludeDirs(``),
4231 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004232 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4233 `),
4234 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004235 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4236 `),
4237 )
4238 })
4239
Paul Duffin33056e82021-02-19 13:49:08 +00004240 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004241 ctx := testCc(t, genRuleModules+`
4242 cc_library_shared {
4243 name: "libfoo",
4244 srcs: [
4245 "foo.c",
4246 "a.sysprop",
4247 "b.aidl",
4248 "a.proto",
4249 ],
4250 }
4251 `)
4252 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4253 checkIncludeDirs(t, ctx, foo,
4254 expectedIncludeDirs(`
4255 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4256 `),
4257 expectedSystemIncludeDirs(``),
4258 expectedGeneratedHeaders(`
4259 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004260 `),
4261 expectedOrderOnlyDeps(`
4262 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4263 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004264 `),
4265 )
4266 })
4267}