blob: bb188aec006db92421f78e2e500f500c004d181b [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
Doug Hornc32c6b02019-01-17 14:44:05 -0800169func TestFuchsiaDeps(t *testing.T) {
170 t.Helper()
171
172 bp := `
173 cc_library {
174 name: "libTest",
175 srcs: ["foo.c"],
176 target: {
177 fuchsia: {
178 srcs: ["bar.c"],
179 },
180 },
181 }`
182
Paul Duffinecdac8a2021-02-24 19:18:42 +0000183 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800184
185 rt := false
186 fb := false
187
Paul Duffinecdac8a2021-02-24 19:18:42 +0000188 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800189 implicits := ld.Implicits
190 for _, lib := range implicits {
191 if strings.Contains(lib.Rel(), "libcompiler_rt") {
192 rt = true
193 }
194
195 if strings.Contains(lib.Rel(), "libbioniccompat") {
196 fb = true
197 }
198 }
199
200 if !rt || !fb {
201 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
202 }
203}
204
205func TestFuchsiaTargetDecl(t *testing.T) {
206 t.Helper()
207
208 bp := `
209 cc_library {
210 name: "libTest",
211 srcs: ["foo.c"],
212 target: {
213 fuchsia: {
214 srcs: ["bar.c"],
215 },
216 },
217 }`
218
Paul Duffinecdac8a2021-02-24 19:18:42 +0000219 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
220 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800221 var objs []string
222 for _, o := range ld.Inputs {
223 objs = append(objs, o.Base())
224 }
Paul Duffine84b1332021-03-12 11:59:43 +0000225 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800226}
227
Jiyong Park6a43f042017-10-12 23:05:00 +0900228func TestVendorSrc(t *testing.T) {
229 ctx := testCc(t, `
230 cc_library {
231 name: "libTest",
232 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700233 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800234 nocrt: true,
235 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900236 vendor_available: true,
237 target: {
238 vendor: {
239 srcs: ["bar.c"],
240 },
241 },
242 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900243 `)
244
Logan Chienf3511742017-10-31 18:04:35 +0800245 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900246 var objs []string
247 for _, o := range ld.Inputs {
248 objs = append(objs, o.Base())
249 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800250 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900251 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
252 }
253}
254
Logan Chienf3511742017-10-31 18:04:35 +0800255func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900256 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800257
Logan Chiend3c59a22018-03-29 14:08:15 +0800258 t.Helper()
259
Justin Yun0ecf0b22020-02-28 15:07:59 +0900260 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800261
262 // Check library properties.
263 lib, ok := mod.compiler.(*libraryDecorator)
264 if !ok {
265 t.Errorf("%q must have libraryDecorator", name)
266 } else if lib.baseInstaller.subDir != subDir {
267 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
268 lib.baseInstaller.subDir)
269 }
270
271 // Check VNDK properties.
272 if mod.vndkdep == nil {
273 t.Fatalf("%q must have `vndkdep`", name)
274 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700275 if !mod.IsVndk() {
276 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800277 }
278 if mod.isVndkSp() != isVndkSp {
279 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
280 }
281
282 // Check VNDK extension properties.
283 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500284 if mod.IsVndkExt() != isVndkExt {
285 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800286 }
287
288 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
289 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
290 }
291}
292
Jose Galmes0a942a02021-02-03 14:23:15 -0800293func 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 -0700294 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900295 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
296 if !ok {
297 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900298 return
299 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900300 outputFiles, err := mod.OutputFiles("")
301 if err != nil || len(outputFiles) != 1 {
302 t.Errorf("%q must have single output\n", moduleName)
303 return
304 }
305 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900306
Bill Peckham945441c2020-08-31 16:07:58 -0700307 if include {
308 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800309 if fake {
310 if out.Rule == nil {
311 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
312 }
313 } else {
314 if out.Input.String() != outputFiles[0].String() {
315 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
316 }
Bill Peckham945441c2020-08-31 16:07:58 -0700317 }
318 } else {
319 out := singleton.MaybeOutput(snapshotPath)
320 if out.Rule != nil {
321 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
322 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900323 }
324}
325
Bill Peckham945441c2020-08-31 16:07:58 -0700326func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800327 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700328}
329
330func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Jose Galmes0a942a02021-02-03 14:23:15 -0800331 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
332}
333
334func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
335 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700336}
337
Jooyung Han2216fb12019-11-06 16:46:15 +0900338func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
339 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800340 content := android.ContentFromFileRuleForTests(t, params)
341 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900342 assertArrayString(t, actual, expected)
343}
344
Jooyung Han097087b2019-10-22 19:32:18 +0900345func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
346 t.Helper()
347 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900348 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
349}
350
351func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
352 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800353 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
354 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900355}
356
Logan Chienf3511742017-10-31 18:04:35 +0800357func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800358 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800359 cc_library {
360 name: "libvndk",
361 vendor_available: true,
362 vndk: {
363 enabled: true,
364 },
365 nocrt: true,
366 }
367
368 cc_library {
369 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900370 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800371 vndk: {
372 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900373 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800374 },
375 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900376 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800377 }
378
379 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900380 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800381 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900382 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800383 vndk: {
384 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900385 },
386 nocrt: true,
387 target: {
388 vendor: {
389 cflags: ["-DTEST"],
390 },
391 product: {
392 cflags: ["-DTEST"],
393 },
394 },
395 }
396
397 cc_library {
398 name: "libvndk_sp",
399 vendor_available: true,
400 vndk: {
401 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800402 support_system_process: true,
403 },
404 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900405 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800406 }
407
408 cc_library {
409 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900410 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800411 vndk: {
412 enabled: true,
413 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900414 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800415 },
416 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900417 target: {
418 vendor: {
419 suffix: "-x",
420 },
421 },
Logan Chienf3511742017-10-31 18:04:35 +0800422 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900423
424 cc_library {
425 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900426 vendor_available: true,
427 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900428 vndk: {
429 enabled: true,
430 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900431 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900432 },
433 nocrt: true,
434 target: {
435 vendor: {
436 suffix: "-x",
437 },
438 product: {
439 suffix: "-x",
440 },
441 },
442 }
443
Colin Crosse4e44bc2020-12-28 13:50:21 -0800444 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900445 name: "llndk.libraries.txt",
446 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800447 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900448 name: "vndkcore.libraries.txt",
449 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800450 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900451 name: "vndksp.libraries.txt",
452 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800453 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900454 name: "vndkprivate.libraries.txt",
455 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800456 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900457 name: "vndkproduct.libraries.txt",
458 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800459 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900460 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800461 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900462 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800463 `
464
465 config := TestConfig(buildDir, android.Android, nil, bp, nil)
466 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900467 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800468 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
469
470 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800471
Jooyung Han261e1582020-10-20 18:54:21 +0900472 // subdir == "" because VNDK libs are not supposed to be installed separately.
473 // They are installed as part of VNDK APEX instead.
474 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
475 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900476 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900477 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
478 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900479 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900480
Justin Yun6977e8a2020-10-29 18:24:11 +0900481 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
482 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900483
Inseob Kim1f086e22019-05-09 13:29:15 +0900484 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900485 snapshotDir := "vndk-snapshot"
486 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
487
488 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
489 "arm64", "armv8-a"))
490 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
491 "arm", "armv7-a-neon"))
492
493 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
494 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
495 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
496 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
497
Colin Crossfb0c16e2019-11-20 17:12:35 -0800498 variant := "android_vendor.VER_arm64_armv8-a_shared"
499 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900500
Inseob Kim7f283f42020-06-01 21:53:49 +0900501 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
502
503 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
504 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900505 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
506 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900507 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
508 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900509
Jooyung Han39edb6c2019-11-06 16:53:07 +0900510 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900511 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
512 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
513 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
514 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900515 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900516
Jooyung Han097087b2019-10-22 19:32:18 +0900517 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
518 "LLNDK: libc.so",
519 "LLNDK: libdl.so",
520 "LLNDK: libft2.so",
521 "LLNDK: libm.so",
522 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900523 "VNDK-SP: libvndk_sp-x.so",
524 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900525 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900526 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900527 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900528 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900529 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900530 "VNDK-private: libvndk-private.so",
531 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900532 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900533 "VNDK-product: libc++.so",
534 "VNDK-product: libvndk_product.so",
535 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900536 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900537 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900538 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
539 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
540 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 +0900541 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 +0900542 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
543}
544
Yo Chiangbba545e2020-06-09 16:15:37 +0800545func TestVndkWithHostSupported(t *testing.T) {
546 ctx := testCc(t, `
547 cc_library {
548 name: "libvndk_host_supported",
549 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900550 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800551 vndk: {
552 enabled: true,
553 },
554 host_supported: true,
555 }
556
557 cc_library {
558 name: "libvndk_host_supported_but_disabled_on_device",
559 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900560 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800561 vndk: {
562 enabled: true,
563 },
564 host_supported: true,
565 enabled: false,
566 target: {
567 host: {
568 enabled: true,
569 }
570 }
571 }
572
Colin Crosse4e44bc2020-12-28 13:50:21 -0800573 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800574 name: "vndkcore.libraries.txt",
575 }
576 `)
577
578 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
579}
580
Jooyung Han2216fb12019-11-06 16:46:15 +0900581func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800582 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800583 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900584 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800585 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800586 }`
587 config := TestConfig(buildDir, android.Android, nil, bp, nil)
588 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
589 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
590 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900591
592 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700593 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900594 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900595}
596
597func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800598 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900599 cc_library {
600 name: "libvndk",
601 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900602 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900603 vndk: {
604 enabled: true,
605 },
606 nocrt: true,
607 }
608
609 cc_library {
610 name: "libvndk_sp",
611 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900612 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900613 vndk: {
614 enabled: true,
615 support_system_process: true,
616 },
617 nocrt: true,
618 }
619
620 cc_library {
621 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900622 vendor_available: true,
623 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900624 vndk: {
625 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900626 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900627 },
628 nocrt: true,
629 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900630
Colin Crosse4e44bc2020-12-28 13:50:21 -0800631 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900632 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800633 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900634 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800635 `
636
637 config := TestConfig(buildDir, android.Android, nil, bp, nil)
638 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
639 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
640 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
641
642 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
643
644 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900645
Jooyung Han2216fb12019-11-06 16:46:15 +0900646 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900647}
648
Chris Parsons79d66a52020-06-05 17:26:16 -0400649func TestDataLibs(t *testing.T) {
650 bp := `
651 cc_test_library {
652 name: "test_lib",
653 srcs: ["test_lib.cpp"],
654 gtest: false,
655 }
656
657 cc_test {
658 name: "main_test",
659 data_libs: ["test_lib"],
660 gtest: false,
661 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400662 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400663
664 config := TestConfig(buildDir, android.Android, nil, bp, nil)
665 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
666 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
667 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
668
669 ctx := testCcWithConfig(t, config)
670 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
671 testBinary := module.(*Module).linker.(*testBinary)
672 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
673 if err != nil {
674 t.Errorf("Expected cc_test to produce output files, error: %s", err)
675 return
676 }
677 if len(outputFiles) != 1 {
678 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
679 return
680 }
681 if len(testBinary.dataPaths()) != 1 {
682 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
683 return
684 }
685
686 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400687 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400688
689 if !strings.HasSuffix(outputPath, "/main_test") {
690 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
691 return
692 }
693 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
694 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
695 return
696 }
697}
698
Chris Parsons216e10a2020-07-09 17:12:52 -0400699func TestDataLibsRelativeInstallPath(t *testing.T) {
700 bp := `
701 cc_test_library {
702 name: "test_lib",
703 srcs: ["test_lib.cpp"],
704 relative_install_path: "foo/bar/baz",
705 gtest: false,
706 }
707
708 cc_test {
709 name: "main_test",
710 data_libs: ["test_lib"],
711 gtest: false,
712 }
713 `
714
715 config := TestConfig(buildDir, android.Android, nil, bp, nil)
716 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
717 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
718 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
719
720 ctx := testCcWithConfig(t, config)
721 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
722 testBinary := module.(*Module).linker.(*testBinary)
723 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
724 if err != nil {
725 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
726 }
727 if len(outputFiles) != 1 {
728 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
729 }
730 if len(testBinary.dataPaths()) != 1 {
731 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
732 }
733
734 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400735
736 if !strings.HasSuffix(outputPath, "/main_test") {
737 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
738 }
Colin Crossaa255532020-07-03 13:18:24 -0700739 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400740 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
741 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400742 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400743 }
744}
745
Jooyung Han0302a842019-10-30 18:43:49 +0900746func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900747 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900748 cc_library {
749 name: "libvndk",
750 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900751 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900752 vndk: {
753 enabled: true,
754 },
755 nocrt: true,
756 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900757 cc_library {
758 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900759 vendor_available: true,
760 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900761 vndk: {
762 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900763 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900764 },
765 nocrt: true,
766 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800767
768 cc_library {
769 name: "libllndk",
770 llndk_stubs: "libllndk.llndk",
771 }
772
773 llndk_library {
774 name: "libllndk.llndk",
775 symbol_file: "",
776 export_llndk_headers: ["libllndk_headers"],
777 }
778
779 llndk_headers {
780 name: "libllndk_headers",
781 export_include_dirs: ["include"],
782 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900783 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900784
785 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
786 "LLNDK: libc.so",
787 "LLNDK: libdl.so",
788 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800789 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900790 "LLNDK: libm.so",
791 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900792 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900793 "VNDK-core: libvndk.so",
794 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900795 "VNDK-private: libvndk-private.so",
796 "VNDK-product: libc++.so",
797 "VNDK-product: libvndk-private.so",
798 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900799 })
Logan Chienf3511742017-10-31 18:04:35 +0800800}
801
Justin Yun63e9ec72020-10-29 16:49:43 +0900802func TestVndkModuleError(t *testing.T) {
803 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900804 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900805 cc_library {
806 name: "libvndk",
807 vndk: {
808 enabled: true,
809 },
810 nocrt: true,
811 }
812 `)
813
Justin Yunc0d8c492021-01-07 17:45:31 +0900814 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900815 cc_library {
816 name: "libvndk",
817 product_available: true,
818 vndk: {
819 enabled: true,
820 },
821 nocrt: true,
822 }
823 `)
824
Justin Yun6977e8a2020-10-29 18:24:11 +0900825 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
826 cc_library {
827 name: "libvndkprop",
828 vendor_available: true,
829 product_available: true,
830 vndk: {
831 enabled: true,
832 },
833 nocrt: true,
834 target: {
835 vendor: {
836 cflags: ["-DTEST",],
837 },
838 },
839 }
840 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900841}
842
Logan Chiend3c59a22018-03-29 14:08:15 +0800843func TestVndkDepError(t *testing.T) {
844 // Check whether an error is emitted when a VNDK lib depends on a system lib.
845 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
846 cc_library {
847 name: "libvndk",
848 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900849 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800850 vndk: {
851 enabled: true,
852 },
853 shared_libs: ["libfwk"], // Cause error
854 nocrt: true,
855 }
856
857 cc_library {
858 name: "libfwk",
859 nocrt: true,
860 }
861 `)
862
863 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
864 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
865 cc_library {
866 name: "libvndk",
867 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900868 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800869 vndk: {
870 enabled: true,
871 },
872 shared_libs: ["libvendor"], // Cause error
873 nocrt: true,
874 }
875
876 cc_library {
877 name: "libvendor",
878 vendor: true,
879 nocrt: true,
880 }
881 `)
882
883 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
884 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
885 cc_library {
886 name: "libvndk_sp",
887 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900888 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800889 vndk: {
890 enabled: true,
891 support_system_process: true,
892 },
893 shared_libs: ["libfwk"], // Cause error
894 nocrt: true,
895 }
896
897 cc_library {
898 name: "libfwk",
899 nocrt: true,
900 }
901 `)
902
903 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
904 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
905 cc_library {
906 name: "libvndk_sp",
907 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900908 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800909 vndk: {
910 enabled: true,
911 support_system_process: true,
912 },
913 shared_libs: ["libvendor"], // Cause error
914 nocrt: true,
915 }
916
917 cc_library {
918 name: "libvendor",
919 vendor: true,
920 nocrt: true,
921 }
922 `)
923
924 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
925 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
926 cc_library {
927 name: "libvndk_sp",
928 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900929 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800930 vndk: {
931 enabled: true,
932 support_system_process: true,
933 },
934 shared_libs: ["libvndk"], // Cause error
935 nocrt: true,
936 }
937
938 cc_library {
939 name: "libvndk",
940 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900941 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800942 vndk: {
943 enabled: true,
944 },
945 nocrt: true,
946 }
947 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900948
949 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
950 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
951 cc_library {
952 name: "libvndk",
953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900954 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900955 vndk: {
956 enabled: true,
957 },
958 shared_libs: ["libnonvndk"],
959 nocrt: true,
960 }
961
962 cc_library {
963 name: "libnonvndk",
964 vendor_available: true,
965 nocrt: true,
966 }
967 `)
968
969 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
970 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
971 cc_library {
972 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900973 vendor_available: true,
974 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900975 vndk: {
976 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900977 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900978 },
979 shared_libs: ["libnonvndk"],
980 nocrt: true,
981 }
982
983 cc_library {
984 name: "libnonvndk",
985 vendor_available: true,
986 nocrt: true,
987 }
988 `)
989
990 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
991 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
992 cc_library {
993 name: "libvndksp",
994 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900995 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900996 vndk: {
997 enabled: true,
998 support_system_process: true,
999 },
1000 shared_libs: ["libnonvndk"],
1001 nocrt: true,
1002 }
1003
1004 cc_library {
1005 name: "libnonvndk",
1006 vendor_available: true,
1007 nocrt: true,
1008 }
1009 `)
1010
1011 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1012 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1013 cc_library {
1014 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001015 vendor_available: true,
1016 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001017 vndk: {
1018 enabled: true,
1019 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001020 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001021 },
1022 shared_libs: ["libnonvndk"],
1023 nocrt: true,
1024 }
1025
1026 cc_library {
1027 name: "libnonvndk",
1028 vendor_available: true,
1029 nocrt: true,
1030 }
1031 `)
1032}
1033
1034func TestDoubleLoadbleDep(t *testing.T) {
1035 // okay to link : LLNDK -> double_loadable VNDK
1036 testCc(t, `
1037 cc_library {
1038 name: "libllndk",
1039 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001040 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001041 }
1042
1043 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001044 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001045 symbol_file: "",
1046 }
1047
1048 cc_library {
1049 name: "libdoubleloadable",
1050 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001051 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001052 vndk: {
1053 enabled: true,
1054 },
1055 double_loadable: true,
1056 }
1057 `)
1058 // okay to link : LLNDK -> VNDK-SP
1059 testCc(t, `
1060 cc_library {
1061 name: "libllndk",
1062 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001063 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001064 }
1065
1066 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001067 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001068 symbol_file: "",
1069 }
1070
1071 cc_library {
1072 name: "libvndksp",
1073 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001074 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001075 vndk: {
1076 enabled: true,
1077 support_system_process: true,
1078 },
1079 }
1080 `)
1081 // okay to link : double_loadable -> double_loadable
1082 testCc(t, `
1083 cc_library {
1084 name: "libdoubleloadable1",
1085 shared_libs: ["libdoubleloadable2"],
1086 vendor_available: true,
1087 double_loadable: true,
1088 }
1089
1090 cc_library {
1091 name: "libdoubleloadable2",
1092 vendor_available: true,
1093 double_loadable: true,
1094 }
1095 `)
1096 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1097 testCc(t, `
1098 cc_library {
1099 name: "libdoubleloadable",
1100 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001101 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001102 vndk: {
1103 enabled: true,
1104 },
1105 double_loadable: true,
1106 shared_libs: ["libnondoubleloadable"],
1107 }
1108
1109 cc_library {
1110 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001111 vendor_available: true,
1112 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001113 vndk: {
1114 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001115 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001116 },
1117 double_loadable: true,
1118 }
1119 `)
1120 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1121 testCc(t, `
1122 cc_library {
1123 name: "libllndk",
1124 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001125 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001126 }
1127
1128 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001129 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001130 symbol_file: "",
1131 }
1132
1133 cc_library {
1134 name: "libcoreonly",
1135 shared_libs: ["libvendoravailable"],
1136 }
1137
1138 // indirect dependency of LLNDK
1139 cc_library {
1140 name: "libvendoravailable",
1141 vendor_available: true,
1142 double_loadable: true,
1143 }
1144 `)
1145}
1146
1147func TestDoubleLoadableDepError(t *testing.T) {
1148 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1149 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1150 cc_library {
1151 name: "libllndk",
1152 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001153 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001154 }
1155
1156 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001157 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001158 symbol_file: "",
1159 }
1160
1161 cc_library {
1162 name: "libnondoubleloadable",
1163 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001164 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001165 vndk: {
1166 enabled: true,
1167 },
1168 }
1169 `)
1170
1171 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1172 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1173 cc_library {
1174 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001175 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001176 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001177 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001178 }
1179
1180 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001181 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001182 symbol_file: "",
1183 }
1184
1185 cc_library {
1186 name: "libnondoubleloadable",
1187 vendor_available: true,
1188 }
1189 `)
1190
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1192 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1193 cc_library {
1194 name: "libllndk",
1195 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001196 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001197 }
1198
1199 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001200 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001201 symbol_file: "",
1202 }
1203
1204 cc_library {
1205 name: "libcoreonly",
1206 shared_libs: ["libvendoravailable"],
1207 }
1208
1209 // indirect dependency of LLNDK
1210 cc_library {
1211 name: "libvendoravailable",
1212 vendor_available: true,
1213 }
1214 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001215
1216 // The error is not from 'client' but from 'libllndk'
1217 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1218 cc_library {
1219 name: "client",
1220 vendor_available: true,
1221 double_loadable: true,
1222 shared_libs: ["libllndk"],
1223 }
1224 cc_library {
1225 name: "libllndk",
1226 shared_libs: ["libnondoubleloadable"],
1227 llndk_stubs: "libllndk.llndk",
1228 }
1229 llndk_library {
1230 name: "libllndk.llndk",
1231 symbol_file: "",
1232 }
1233 cc_library {
1234 name: "libnondoubleloadable",
1235 vendor_available: true,
1236 }
1237 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001238}
1239
Jooyung Han479ca172020-10-19 18:51:07 +09001240func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1241 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1242 cc_library {
1243 name: "libvndksp",
1244 shared_libs: ["libanothervndksp"],
1245 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001246 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001247 vndk: {
1248 enabled: true,
1249 support_system_process: true,
1250 }
1251 }
1252
1253 cc_library {
1254 name: "libllndk",
1255 shared_libs: ["libanothervndksp"],
1256 }
1257
1258 llndk_library {
1259 name: "libllndk",
1260 symbol_file: "",
1261 }
1262
1263 cc_library {
1264 name: "libanothervndksp",
1265 vendor_available: true,
1266 }
1267 `)
1268}
1269
Logan Chienf3511742017-10-31 18:04:35 +08001270func TestVndkExt(t *testing.T) {
1271 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001272 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001273 cc_library {
1274 name: "libvndk",
1275 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001276 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001277 vndk: {
1278 enabled: true,
1279 },
1280 nocrt: true,
1281 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001282 cc_library {
1283 name: "libvndk2",
1284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001285 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001286 vndk: {
1287 enabled: true,
1288 },
1289 target: {
1290 vendor: {
1291 suffix: "-suffix",
1292 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001293 product: {
1294 suffix: "-suffix",
1295 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001296 },
1297 nocrt: true,
1298 }
Logan Chienf3511742017-10-31 18:04:35 +08001299
1300 cc_library {
1301 name: "libvndk_ext",
1302 vendor: true,
1303 vndk: {
1304 enabled: true,
1305 extends: "libvndk",
1306 },
1307 nocrt: true,
1308 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001309
1310 cc_library {
1311 name: "libvndk2_ext",
1312 vendor: true,
1313 vndk: {
1314 enabled: true,
1315 extends: "libvndk2",
1316 },
1317 nocrt: true,
1318 }
Logan Chienf3511742017-10-31 18:04:35 +08001319
Justin Yun0ecf0b22020-02-28 15:07:59 +09001320 cc_library {
1321 name: "libvndk_ext_product",
1322 product_specific: true,
1323 vndk: {
1324 enabled: true,
1325 extends: "libvndk",
1326 },
1327 nocrt: true,
1328 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001329
Justin Yun0ecf0b22020-02-28 15:07:59 +09001330 cc_library {
1331 name: "libvndk2_ext_product",
1332 product_specific: true,
1333 vndk: {
1334 enabled: true,
1335 extends: "libvndk2",
1336 },
1337 nocrt: true,
1338 }
1339 `
1340 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1341 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1342 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1343 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1344
1345 ctx := testCcWithConfig(t, config)
1346
1347 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1348 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1349
1350 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1351 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1352
1353 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1354 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001355}
1356
Logan Chiend3c59a22018-03-29 14:08:15 +08001357func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001358 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1359 ctx := testCcNoVndk(t, `
1360 cc_library {
1361 name: "libvndk",
1362 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001363 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001364 vndk: {
1365 enabled: true,
1366 },
1367 nocrt: true,
1368 }
1369
1370 cc_library {
1371 name: "libvndk_ext",
1372 vendor: true,
1373 vndk: {
1374 enabled: true,
1375 extends: "libvndk",
1376 },
1377 nocrt: true,
1378 }
1379 `)
1380
1381 // Ensures that the core variant of "libvndk_ext" can be found.
1382 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1383 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1384 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1385 }
1386}
1387
Justin Yun0ecf0b22020-02-28 15:07:59 +09001388func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1389 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001390 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001391 cc_library {
1392 name: "libvndk",
1393 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001394 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001395 vndk: {
1396 enabled: true,
1397 },
1398 nocrt: true,
1399 }
1400
1401 cc_library {
1402 name: "libvndk_ext_product",
1403 product_specific: true,
1404 vndk: {
1405 enabled: true,
1406 extends: "libvndk",
1407 },
1408 nocrt: true,
1409 }
1410 `)
1411
1412 // Ensures that the core variant of "libvndk_ext_product" can be found.
1413 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1414 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1415 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1416 }
1417}
1418
Logan Chienf3511742017-10-31 18:04:35 +08001419func TestVndkExtError(t *testing.T) {
1420 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001421 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001422 cc_library {
1423 name: "libvndk",
1424 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001425 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001426 vndk: {
1427 enabled: true,
1428 },
1429 nocrt: true,
1430 }
1431
1432 cc_library {
1433 name: "libvndk_ext",
1434 vndk: {
1435 enabled: true,
1436 extends: "libvndk",
1437 },
1438 nocrt: true,
1439 }
1440 `)
1441
1442 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1443 cc_library {
1444 name: "libvndk",
1445 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001446 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001447 vndk: {
1448 enabled: true,
1449 },
1450 nocrt: true,
1451 }
1452
1453 cc_library {
1454 name: "libvndk_ext",
1455 vendor: true,
1456 vndk: {
1457 enabled: true,
1458 },
1459 nocrt: true,
1460 }
1461 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001462
1463 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1464 cc_library {
1465 name: "libvndk",
1466 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001467 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001468 vndk: {
1469 enabled: true,
1470 },
1471 nocrt: true,
1472 }
1473
1474 cc_library {
1475 name: "libvndk_ext_product",
1476 product_specific: true,
1477 vndk: {
1478 enabled: true,
1479 },
1480 nocrt: true,
1481 }
1482 `)
1483
1484 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1485 cc_library {
1486 name: "libvndk",
1487 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001488 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001489 vndk: {
1490 enabled: true,
1491 },
1492 nocrt: true,
1493 }
1494
1495 cc_library {
1496 name: "libvndk_ext_product",
1497 product_specific: true,
1498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001499 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001500 vndk: {
1501 enabled: true,
1502 extends: "libvndk",
1503 },
1504 nocrt: true,
1505 }
1506 `)
Logan Chienf3511742017-10-31 18:04:35 +08001507}
1508
1509func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1510 // This test ensures an error is emitted for inconsistent support_system_process.
1511 testCcError(t, "module \".*\" with mismatched support_system_process", `
1512 cc_library {
1513 name: "libvndk",
1514 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001515 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001516 vndk: {
1517 enabled: true,
1518 },
1519 nocrt: true,
1520 }
1521
1522 cc_library {
1523 name: "libvndk_sp_ext",
1524 vendor: true,
1525 vndk: {
1526 enabled: true,
1527 extends: "libvndk",
1528 support_system_process: true,
1529 },
1530 nocrt: true,
1531 }
1532 `)
1533
1534 testCcError(t, "module \".*\" with mismatched support_system_process", `
1535 cc_library {
1536 name: "libvndk_sp",
1537 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001538 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001539 vndk: {
1540 enabled: true,
1541 support_system_process: true,
1542 },
1543 nocrt: true,
1544 }
1545
1546 cc_library {
1547 name: "libvndk_ext",
1548 vendor: true,
1549 vndk: {
1550 enabled: true,
1551 extends: "libvndk_sp",
1552 },
1553 nocrt: true,
1554 }
1555 `)
1556}
1557
1558func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001559 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001560 // with `private: true`.
1561 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001562 cc_library {
1563 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001564 vendor_available: true,
1565 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001566 vndk: {
1567 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001568 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001569 },
1570 nocrt: true,
1571 }
1572
1573 cc_library {
1574 name: "libvndk_ext",
1575 vendor: true,
1576 vndk: {
1577 enabled: true,
1578 extends: "libvndk",
1579 },
1580 nocrt: true,
1581 }
1582 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001583
Justin Yunfd9e8042020-12-23 18:23:14 +09001584 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001585 cc_library {
1586 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001587 vendor_available: true,
1588 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001589 vndk: {
1590 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001591 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001592 },
1593 nocrt: true,
1594 }
1595
1596 cc_library {
1597 name: "libvndk_ext_product",
1598 product_specific: true,
1599 vndk: {
1600 enabled: true,
1601 extends: "libvndk",
1602 },
1603 nocrt: true,
1604 }
1605 `)
Logan Chienf3511742017-10-31 18:04:35 +08001606}
1607
Logan Chiend3c59a22018-03-29 14:08:15 +08001608func TestVendorModuleUseVndkExt(t *testing.T) {
1609 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001610 testCc(t, `
1611 cc_library {
1612 name: "libvndk",
1613 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001614 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001615 vndk: {
1616 enabled: true,
1617 },
1618 nocrt: true,
1619 }
1620
1621 cc_library {
1622 name: "libvndk_ext",
1623 vendor: true,
1624 vndk: {
1625 enabled: true,
1626 extends: "libvndk",
1627 },
1628 nocrt: true,
1629 }
1630
1631 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001632 name: "libvndk_sp",
1633 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001634 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001635 vndk: {
1636 enabled: true,
1637 support_system_process: true,
1638 },
1639 nocrt: true,
1640 }
1641
1642 cc_library {
1643 name: "libvndk_sp_ext",
1644 vendor: true,
1645 vndk: {
1646 enabled: true,
1647 extends: "libvndk_sp",
1648 support_system_process: true,
1649 },
1650 nocrt: true,
1651 }
1652
1653 cc_library {
1654 name: "libvendor",
1655 vendor: true,
1656 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1657 nocrt: true,
1658 }
1659 `)
1660}
1661
Logan Chiend3c59a22018-03-29 14:08:15 +08001662func TestVndkExtUseVendorLib(t *testing.T) {
1663 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001664 testCc(t, `
1665 cc_library {
1666 name: "libvndk",
1667 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001668 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001669 vndk: {
1670 enabled: true,
1671 },
1672 nocrt: true,
1673 }
1674
1675 cc_library {
1676 name: "libvndk_ext",
1677 vendor: true,
1678 vndk: {
1679 enabled: true,
1680 extends: "libvndk",
1681 },
1682 shared_libs: ["libvendor"],
1683 nocrt: true,
1684 }
1685
1686 cc_library {
1687 name: "libvendor",
1688 vendor: true,
1689 nocrt: true,
1690 }
1691 `)
Logan Chienf3511742017-10-31 18:04:35 +08001692
Logan Chiend3c59a22018-03-29 14:08:15 +08001693 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1694 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001695 cc_library {
1696 name: "libvndk_sp",
1697 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001698 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001699 vndk: {
1700 enabled: true,
1701 support_system_process: true,
1702 },
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvndk_sp_ext",
1708 vendor: true,
1709 vndk: {
1710 enabled: true,
1711 extends: "libvndk_sp",
1712 support_system_process: true,
1713 },
1714 shared_libs: ["libvendor"], // Cause an error
1715 nocrt: true,
1716 }
1717
1718 cc_library {
1719 name: "libvendor",
1720 vendor: true,
1721 nocrt: true,
1722 }
1723 `)
1724}
1725
Justin Yun0ecf0b22020-02-28 15:07:59 +09001726func TestProductVndkExtDependency(t *testing.T) {
1727 bp := `
1728 cc_library {
1729 name: "libvndk",
1730 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001731 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001732 vndk: {
1733 enabled: true,
1734 },
1735 nocrt: true,
1736 }
1737
1738 cc_library {
1739 name: "libvndk_ext_product",
1740 product_specific: true,
1741 vndk: {
1742 enabled: true,
1743 extends: "libvndk",
1744 },
1745 shared_libs: ["libproduct_for_vndklibs"],
1746 nocrt: true,
1747 }
1748
1749 cc_library {
1750 name: "libvndk_sp",
1751 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001752 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001753 vndk: {
1754 enabled: true,
1755 support_system_process: true,
1756 },
1757 nocrt: true,
1758 }
1759
1760 cc_library {
1761 name: "libvndk_sp_ext_product",
1762 product_specific: true,
1763 vndk: {
1764 enabled: true,
1765 extends: "libvndk_sp",
1766 support_system_process: true,
1767 },
1768 shared_libs: ["libproduct_for_vndklibs"],
1769 nocrt: true,
1770 }
1771
1772 cc_library {
1773 name: "libproduct",
1774 product_specific: true,
1775 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1776 nocrt: true,
1777 }
1778
1779 cc_library {
1780 name: "libproduct_for_vndklibs",
1781 product_specific: true,
1782 nocrt: true,
1783 }
1784 `
1785 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1786 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1787 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1788 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1789
1790 testCcWithConfig(t, config)
1791}
1792
Logan Chiend3c59a22018-03-29 14:08:15 +08001793func TestVndkSpExtUseVndkError(t *testing.T) {
1794 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1795 // library.
1796 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1797 cc_library {
1798 name: "libvndk",
1799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001800 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001801 vndk: {
1802 enabled: true,
1803 },
1804 nocrt: true,
1805 }
1806
1807 cc_library {
1808 name: "libvndk_sp",
1809 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001810 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001811 vndk: {
1812 enabled: true,
1813 support_system_process: true,
1814 },
1815 nocrt: true,
1816 }
1817
1818 cc_library {
1819 name: "libvndk_sp_ext",
1820 vendor: true,
1821 vndk: {
1822 enabled: true,
1823 extends: "libvndk_sp",
1824 support_system_process: true,
1825 },
1826 shared_libs: ["libvndk"], // Cause an error
1827 nocrt: true,
1828 }
1829 `)
1830
1831 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1832 // library.
1833 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1834 cc_library {
1835 name: "libvndk",
1836 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001837 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001838 vndk: {
1839 enabled: true,
1840 },
1841 nocrt: true,
1842 }
1843
1844 cc_library {
1845 name: "libvndk_ext",
1846 vendor: true,
1847 vndk: {
1848 enabled: true,
1849 extends: "libvndk",
1850 },
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk_sp",
1856 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001857 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001858 vndk: {
1859 enabled: true,
1860 support_system_process: true,
1861 },
1862 nocrt: true,
1863 }
1864
1865 cc_library {
1866 name: "libvndk_sp_ext",
1867 vendor: true,
1868 vndk: {
1869 enabled: true,
1870 extends: "libvndk_sp",
1871 support_system_process: true,
1872 },
1873 shared_libs: ["libvndk_ext"], // Cause an error
1874 nocrt: true,
1875 }
1876 `)
1877}
1878
1879func TestVndkUseVndkExtError(t *testing.T) {
1880 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1881 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001882 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1883 cc_library {
1884 name: "libvndk",
1885 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001886 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001887 vndk: {
1888 enabled: true,
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk_ext",
1895 vendor: true,
1896 vndk: {
1897 enabled: true,
1898 extends: "libvndk",
1899 },
1900 nocrt: true,
1901 }
1902
1903 cc_library {
1904 name: "libvndk2",
1905 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001906 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001907 vndk: {
1908 enabled: true,
1909 },
1910 shared_libs: ["libvndk_ext"],
1911 nocrt: true,
1912 }
1913 `)
1914
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001915 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001916 cc_library {
1917 name: "libvndk",
1918 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001919 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001920 vndk: {
1921 enabled: true,
1922 },
1923 nocrt: true,
1924 }
1925
1926 cc_library {
1927 name: "libvndk_ext",
1928 vendor: true,
1929 vndk: {
1930 enabled: true,
1931 extends: "libvndk",
1932 },
1933 nocrt: true,
1934 }
1935
1936 cc_library {
1937 name: "libvndk2",
1938 vendor_available: true,
1939 vndk: {
1940 enabled: true,
1941 },
1942 target: {
1943 vendor: {
1944 shared_libs: ["libvndk_ext"],
1945 },
1946 },
1947 nocrt: true,
1948 }
1949 `)
1950
1951 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1952 cc_library {
1953 name: "libvndk_sp",
1954 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001955 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001956 vndk: {
1957 enabled: true,
1958 support_system_process: true,
1959 },
1960 nocrt: true,
1961 }
1962
1963 cc_library {
1964 name: "libvndk_sp_ext",
1965 vendor: true,
1966 vndk: {
1967 enabled: true,
1968 extends: "libvndk_sp",
1969 support_system_process: true,
1970 },
1971 nocrt: true,
1972 }
1973
1974 cc_library {
1975 name: "libvndk_sp_2",
1976 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001977 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001978 vndk: {
1979 enabled: true,
1980 support_system_process: true,
1981 },
1982 shared_libs: ["libvndk_sp_ext"],
1983 nocrt: true,
1984 }
1985 `)
1986
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001987 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001988 cc_library {
1989 name: "libvndk_sp",
1990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001991 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001992 vndk: {
1993 enabled: true,
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp_ext",
2000 vendor: true,
2001 vndk: {
2002 enabled: true,
2003 extends: "libvndk_sp",
2004 },
2005 nocrt: true,
2006 }
2007
2008 cc_library {
2009 name: "libvndk_sp2",
2010 vendor_available: true,
2011 vndk: {
2012 enabled: true,
2013 },
2014 target: {
2015 vendor: {
2016 shared_libs: ["libvndk_sp_ext"],
2017 },
2018 },
2019 nocrt: true,
2020 }
2021 `)
2022}
2023
Justin Yun5f7f7e82019-11-18 19:52:14 +09002024func TestEnforceProductVndkVersion(t *testing.T) {
2025 bp := `
2026 cc_library {
2027 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002028 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002029 }
2030 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002031 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002032 symbol_file: "",
2033 }
2034 cc_library {
2035 name: "libvndk",
2036 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002037 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002038 vndk: {
2039 enabled: true,
2040 },
2041 nocrt: true,
2042 }
2043 cc_library {
2044 name: "libvndk_sp",
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 support_system_process: true,
2050 },
2051 nocrt: true,
2052 }
2053 cc_library {
2054 name: "libva",
2055 vendor_available: true,
2056 nocrt: true,
2057 }
2058 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002059 name: "libpa",
2060 product_available: true,
2061 nocrt: true,
2062 }
2063 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002064 name: "libboth_available",
2065 vendor_available: true,
2066 product_available: true,
2067 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002068 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002069 target: {
2070 vendor: {
2071 suffix: "-vendor",
2072 },
2073 product: {
2074 suffix: "-product",
2075 },
2076 }
2077 }
2078 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002079 name: "libproduct_va",
2080 product_specific: true,
2081 vendor_available: true,
2082 nocrt: true,
2083 }
2084 cc_library {
2085 name: "libprod",
2086 product_specific: true,
2087 shared_libs: [
2088 "libllndk",
2089 "libvndk",
2090 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002091 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002092 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002093 "libproduct_va",
2094 ],
2095 nocrt: true,
2096 }
2097 cc_library {
2098 name: "libvendor",
2099 vendor: true,
2100 shared_libs: [
2101 "libllndk",
2102 "libvndk",
2103 "libvndk_sp",
2104 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002105 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002106 "libproduct_va",
2107 ],
2108 nocrt: true,
2109 }
2110 `
2111
Justin Yun13decfb2021-03-08 19:25:55 +09002112 ctx := ccFixtureFactory.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002113
Jooyung Han261e1582020-10-20 18:54:21 +09002114 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2115 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002116
2117 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2118 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2119
2120 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2121 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002122
2123 ensureStringContains := func(t *testing.T, str string, substr string) {
2124 t.Helper()
2125 if !strings.Contains(str, substr) {
2126 t.Errorf("%q is not found in %v", substr, str)
2127 }
2128 }
2129 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2130 t.Helper()
2131 if strings.Contains(str, substr) {
2132 t.Errorf("%q is found in %v", substr, str)
2133 }
2134 }
2135
2136 // _static variant is used since _shared reuses *.o from the static variant
2137 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2138 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2139
2140 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2141 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2142 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2143 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2144
2145 product_cflags := product_static.Rule("cc").Args["cFlags"]
2146 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2147 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2148 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002149}
2150
2151func TestEnforceProductVndkVersionErrors(t *testing.T) {
2152 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2153 cc_library {
2154 name: "libprod",
2155 product_specific: true,
2156 shared_libs: [
2157 "libvendor",
2158 ],
2159 nocrt: true,
2160 }
2161 cc_library {
2162 name: "libvendor",
2163 vendor: true,
2164 nocrt: true,
2165 }
2166 `)
2167 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2168 cc_library {
2169 name: "libprod",
2170 product_specific: true,
2171 shared_libs: [
2172 "libsystem",
2173 ],
2174 nocrt: true,
2175 }
2176 cc_library {
2177 name: "libsystem",
2178 nocrt: true,
2179 }
2180 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002181 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2182 cc_library {
2183 name: "libprod",
2184 product_specific: true,
2185 shared_libs: [
2186 "libva",
2187 ],
2188 nocrt: true,
2189 }
2190 cc_library {
2191 name: "libva",
2192 vendor_available: true,
2193 nocrt: true,
2194 }
2195 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002196 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002197 cc_library {
2198 name: "libprod",
2199 product_specific: true,
2200 shared_libs: [
2201 "libvndk_private",
2202 ],
2203 nocrt: true,
2204 }
2205 cc_library {
2206 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002207 vendor_available: true,
2208 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002209 vndk: {
2210 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002211 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002212 },
2213 nocrt: true,
2214 }
2215 `)
2216 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2217 cc_library {
2218 name: "libprod",
2219 product_specific: true,
2220 shared_libs: [
2221 "libsystem_ext",
2222 ],
2223 nocrt: true,
2224 }
2225 cc_library {
2226 name: "libsystem_ext",
2227 system_ext_specific: true,
2228 nocrt: true,
2229 }
2230 `)
2231 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2232 cc_library {
2233 name: "libsystem",
2234 shared_libs: [
2235 "libproduct_va",
2236 ],
2237 nocrt: true,
2238 }
2239 cc_library {
2240 name: "libproduct_va",
2241 product_specific: true,
2242 vendor_available: true,
2243 nocrt: true,
2244 }
2245 `)
2246}
2247
Jooyung Han38002912019-05-16 04:01:54 +09002248func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002249 bp := `
2250 cc_library {
2251 name: "libvndk",
2252 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002253 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002254 vndk: {
2255 enabled: true,
2256 },
2257 }
2258 cc_library {
2259 name: "libvndksp",
2260 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002261 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002262 vndk: {
2263 enabled: true,
2264 support_system_process: true,
2265 },
2266 }
2267 cc_library {
2268 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002269 vendor_available: true,
2270 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002271 vndk: {
2272 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002273 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002274 },
2275 }
2276 cc_library {
2277 name: "libvendor",
2278 vendor: true,
2279 }
2280 cc_library {
2281 name: "libvndkext",
2282 vendor: true,
2283 vndk: {
2284 enabled: true,
2285 extends: "libvndk",
2286 },
2287 }
2288 vndk_prebuilt_shared {
2289 name: "prevndk",
2290 version: "27",
2291 target_arch: "arm",
2292 binder32bit: true,
2293 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002294 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002295 vndk: {
2296 enabled: true,
2297 },
2298 arch: {
2299 arm: {
2300 srcs: ["liba.so"],
2301 },
2302 },
2303 }
2304 cc_library {
2305 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002306 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002307 }
2308 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002309 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002310 symbol_file: "",
2311 }
2312 cc_library {
2313 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002314 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002315 }
2316 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002317 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002318 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002319 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002320 }
2321
2322 llndk_libraries_txt {
2323 name: "llndk.libraries.txt",
2324 }
2325 vndkcore_libraries_txt {
2326 name: "vndkcore.libraries.txt",
2327 }
2328 vndksp_libraries_txt {
2329 name: "vndksp.libraries.txt",
2330 }
2331 vndkprivate_libraries_txt {
2332 name: "vndkprivate.libraries.txt",
2333 }
2334 vndkcorevariant_libraries_txt {
2335 name: "vndkcorevariant.libraries.txt",
2336 insert_vndk_version: false,
2337 }
2338 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002339
2340 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002341 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2342 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2343 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002344 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002345
Colin Cross78212242021-01-06 14:51:30 -08002346 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2347 []string{"libvndk.so", "libvndkprivate.so"})
2348 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2349 []string{"libc++.so", "libvndksp.so"})
2350 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2351 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2352 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2353 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002354
Colin Crossfb0c16e2019-11-20 17:12:35 -08002355 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002356
Jooyung Han38002912019-05-16 04:01:54 +09002357 tests := []struct {
2358 variant string
2359 name string
2360 expected string
2361 }{
2362 {vendorVariant, "libvndk", "native:vndk"},
2363 {vendorVariant, "libvndksp", "native:vndk"},
2364 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2365 {vendorVariant, "libvendor", "native:vendor"},
2366 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002367 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002368 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002369 {coreVariant, "libvndk", "native:platform"},
2370 {coreVariant, "libvndkprivate", "native:platform"},
2371 {coreVariant, "libllndk", "native:platform"},
2372 }
2373 for _, test := range tests {
2374 t.Run(test.name, func(t *testing.T) {
2375 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2376 assertString(t, module.makeLinkType, test.expected)
2377 })
2378 }
2379}
2380
Jeff Gaston294356f2017-09-27 17:05:30 -07002381var staticLinkDepOrderTestCases = []struct {
2382 // This is a string representation of a map[moduleName][]moduleDependency .
2383 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002384 inStatic string
2385
2386 // This is a string representation of a map[moduleName][]moduleDependency .
2387 // It models the dependencies declared in an Android.bp file.
2388 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002389
2390 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2391 // The keys of allOrdered specify which modules we would like to check.
2392 // The values of allOrdered specify the expected result (of the transitive closure of all
2393 // dependencies) for each module to test
2394 allOrdered string
2395
2396 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2397 // The keys of outOrdered specify which modules we would like to check.
2398 // The values of outOrdered specify the expected result (of the ordered linker command line)
2399 // for each module to test.
2400 outOrdered string
2401}{
2402 // Simple tests
2403 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002404 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002405 outOrdered: "",
2406 },
2407 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002408 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002409 outOrdered: "a:",
2410 },
2411 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002412 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002413 outOrdered: "a:b; b:",
2414 },
2415 // Tests of reordering
2416 {
2417 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002418 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002419 outOrdered: "a:b,c,d; b:d; c:d; d:",
2420 },
2421 {
2422 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002423 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002424 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2425 },
2426 {
2427 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002428 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002429 outOrdered: "a:d,b,e,c; d:b; e:c",
2430 },
2431 {
2432 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002433 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002434 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2435 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2436 },
2437 {
2438 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002439 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 -07002440 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2441 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2442 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002443 // shared dependencies
2444 {
2445 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2446 // So, we don't actually have to check that a shared dependency of c will change the order
2447 // of a library that depends statically on b and on c. We only need to check that if c has
2448 // a shared dependency on b, that that shows up in allOrdered.
2449 inShared: "c:b",
2450 allOrdered: "c:b",
2451 outOrdered: "c:",
2452 },
2453 {
2454 // This test doesn't actually include any shared dependencies but it's a reminder of what
2455 // the second phase of the above test would look like
2456 inStatic: "a:b,c; c:b",
2457 allOrdered: "a:c,b; c:b",
2458 outOrdered: "a:c,b; c:b",
2459 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002460 // tiebreakers for when two modules specifying different orderings and there is no dependency
2461 // to dictate an order
2462 {
2463 // 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 -08002464 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002465 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2466 },
2467 {
2468 // 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 -08002469 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 -07002470 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2471 },
2472 // Tests involving duplicate dependencies
2473 {
2474 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002475 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002476 outOrdered: "a:c,b",
2477 },
2478 {
2479 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002480 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002481 outOrdered: "a:d,c,b",
2482 },
2483 // Tests to confirm the nonexistence of infinite loops.
2484 // These cases should never happen, so as long as the test terminates and the
2485 // result is deterministic then that should be fine.
2486 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002487 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002488 outOrdered: "a:a",
2489 },
2490 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002491 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002492 allOrdered: "a:b,c; b:c,a; c:a,b",
2493 outOrdered: "a:b; b:c; c:a",
2494 },
2495 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002496 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002497 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2498 outOrdered: "a:c,b; b:a,c; c:b,a",
2499 },
2500}
2501
2502// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2503func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2504 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2505 strippedText := strings.Replace(text, " ", "", -1)
2506 if len(strippedText) < 1 {
2507 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2508 }
2509 allDeps = make(map[android.Path][]android.Path, 0)
2510
2511 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2512 moduleTexts := strings.Split(strippedText, ";")
2513
2514 outputForModuleName := func(moduleName string) android.Path {
2515 return android.PathForTesting(moduleName)
2516 }
2517
2518 for _, moduleText := range moduleTexts {
2519 // convert from "a:b,c" to ["a", "b,c"]
2520 components := strings.Split(moduleText, ":")
2521 if len(components) != 2 {
2522 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2523 }
2524 moduleName := components[0]
2525 moduleOutput := outputForModuleName(moduleName)
2526 modulesInOrder = append(modulesInOrder, moduleOutput)
2527
2528 depString := components[1]
2529 // convert from "b,c" to ["b", "c"]
2530 depNames := strings.Split(depString, ",")
2531 if len(depString) < 1 {
2532 depNames = []string{}
2533 }
2534 var deps []android.Path
2535 for _, depName := range depNames {
2536 deps = append(deps, outputForModuleName(depName))
2537 }
2538 allDeps[moduleOutput] = deps
2539 }
2540 return modulesInOrder, allDeps
2541}
2542
Jeff Gaston294356f2017-09-27 17:05:30 -07002543func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2544 for _, moduleName := range moduleNames {
2545 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
2546 output := module.outputFile.Path()
2547 paths = append(paths, output)
2548 }
2549 return paths
2550}
2551
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002552func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002553 ctx := testCc(t, `
2554 cc_library {
2555 name: "a",
2556 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002557 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002558 }
2559 cc_library {
2560 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002561 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002562 }
2563 cc_library {
2564 name: "c",
2565 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002566 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002567 }
2568 cc_library {
2569 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002570 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002571 }
2572
2573 `)
2574
Colin Cross7113d202019-11-20 16:39:12 -08002575 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002576 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002577 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2578 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002579
2580 if !reflect.DeepEqual(actual, expected) {
2581 t.Errorf("staticDeps orderings were not propagated correctly"+
2582 "\nactual: %v"+
2583 "\nexpected: %v",
2584 actual,
2585 expected,
2586 )
2587 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002588}
Jeff Gaston294356f2017-09-27 17:05:30 -07002589
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002590func TestStaticLibDepReorderingWithShared(t *testing.T) {
2591 ctx := testCc(t, `
2592 cc_library {
2593 name: "a",
2594 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002595 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596 }
2597 cc_library {
2598 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002599 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002600 }
2601 cc_library {
2602 name: "c",
2603 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002604 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002605 }
2606
2607 `)
2608
Colin Cross7113d202019-11-20 16:39:12 -08002609 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002610 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07002611 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
2612 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002613
2614 if !reflect.DeepEqual(actual, expected) {
2615 t.Errorf("staticDeps orderings did not account for shared libs"+
2616 "\nactual: %v"+
2617 "\nexpected: %v",
2618 actual,
2619 expected,
2620 )
2621 }
2622}
2623
Jooyung Hanb04a4992020-03-13 18:57:35 +09002624func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002625 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002626 if !reflect.DeepEqual(actual, expected) {
2627 t.Errorf(message+
2628 "\nactual: %v"+
2629 "\nexpected: %v",
2630 actual,
2631 expected,
2632 )
2633 }
2634}
2635
Jooyung Han61b66e92020-03-21 14:21:46 +00002636func TestLlndkLibrary(t *testing.T) {
2637 ctx := testCc(t, `
2638 cc_library {
2639 name: "libllndk",
2640 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002641 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002642 }
2643 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002644 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002645 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002646
2647 cc_prebuilt_library_shared {
2648 name: "libllndkprebuilt",
2649 stubs: { versions: ["1", "2"] },
2650 llndk_stubs: "libllndkprebuilt.llndk",
2651 }
2652 llndk_library {
2653 name: "libllndkprebuilt.llndk",
2654 }
2655
2656 cc_library {
2657 name: "libllndk_with_external_headers",
2658 stubs: { versions: ["1", "2"] },
2659 llndk_stubs: "libllndk_with_external_headers.llndk",
2660 header_libs: ["libexternal_headers"],
2661 export_header_lib_headers: ["libexternal_headers"],
2662 }
2663 llndk_library {
2664 name: "libllndk_with_external_headers.llndk",
2665 }
2666 cc_library_headers {
2667 name: "libexternal_headers",
2668 export_include_dirs: ["include"],
2669 vendor_available: true,
2670 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002671 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002672 actual := ctx.ModuleVariantsForTests("libllndk")
2673 for i := 0; i < len(actual); i++ {
2674 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2675 actual = append(actual[:i], actual[i+1:]...)
2676 i--
2677 }
2678 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002679 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002680 "android_vendor.VER_arm64_armv8-a_shared_1",
2681 "android_vendor.VER_arm64_armv8-a_shared_2",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002682 "android_vendor.VER_arm64_armv8-a_shared_current",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002683 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002684 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2685 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09002686 "android_vendor.VER_arm_armv7-a-neon_shared_current",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002687 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002688 }
2689 checkEquals(t, "variants for llndk stubs", expected, actual)
2690
Colin Cross127bb8b2020-12-16 16:46:01 -08002691 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002692 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2693
Colin Cross127bb8b2020-12-16 16:46:01 -08002694 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002695 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2696}
2697
Jiyong Parka46a4d52017-12-14 19:54:34 +09002698func TestLlndkHeaders(t *testing.T) {
2699 ctx := testCc(t, `
2700 llndk_headers {
2701 name: "libllndk_headers",
2702 export_include_dirs: ["my_include"],
2703 }
2704 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002705 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002706 export_llndk_headers: ["libllndk_headers"],
2707 }
2708 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002709 name: "libllndk",
2710 llndk_stubs: "libllndk.llndk",
2711 }
2712
2713 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002714 name: "libvendor",
2715 shared_libs: ["libllndk"],
2716 vendor: true,
2717 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002718 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002719 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002720 }
2721 `)
2722
2723 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002724 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002725 cflags := cc.Args["cFlags"]
2726 if !strings.Contains(cflags, "-Imy_include") {
2727 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2728 }
2729}
2730
Logan Chien43d34c32017-12-20 01:17:32 +08002731func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2732 actual := module.Properties.AndroidMkRuntimeLibs
2733 if !reflect.DeepEqual(actual, expected) {
2734 t.Errorf("incorrect runtime_libs for shared libs"+
2735 "\nactual: %v"+
2736 "\nexpected: %v",
2737 actual,
2738 expected,
2739 )
2740 }
2741}
2742
2743const runtimeLibAndroidBp = `
2744 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002745 name: "liball_available",
2746 vendor_available: true,
2747 product_available: true,
2748 no_libcrt : true,
2749 nocrt : true,
2750 system_shared_libs : [],
2751 }
2752 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002753 name: "libvendor_available1",
2754 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002755 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002756 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002757 nocrt : true,
2758 system_shared_libs : [],
2759 }
2760 cc_library {
2761 name: "libvendor_available2",
2762 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002763 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002764 target: {
2765 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002766 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002767 }
2768 },
Yi Konge7fe9912019-06-02 00:53:50 -07002769 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002770 nocrt : true,
2771 system_shared_libs : [],
2772 }
2773 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002774 name: "libproduct_vendor",
2775 product_specific: true,
2776 vendor_available: true,
2777 no_libcrt : true,
2778 nocrt : true,
2779 system_shared_libs : [],
2780 }
2781 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002782 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002783 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002784 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002785 nocrt : true,
2786 system_shared_libs : [],
2787 }
2788 cc_library {
2789 name: "libvendor1",
2790 vendor: true,
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: "libvendor2",
2797 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002798 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002799 no_libcrt : true,
2800 nocrt : true,
2801 system_shared_libs : [],
2802 }
2803 cc_library {
2804 name: "libproduct_available1",
2805 product_available: true,
2806 runtime_libs: ["liball_available"],
2807 no_libcrt : true,
2808 nocrt : true,
2809 system_shared_libs : [],
2810 }
2811 cc_library {
2812 name: "libproduct1",
2813 product_specific: true,
2814 no_libcrt : true,
2815 nocrt : true,
2816 system_shared_libs : [],
2817 }
2818 cc_library {
2819 name: "libproduct2",
2820 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002821 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002822 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002823 nocrt : true,
2824 system_shared_libs : [],
2825 }
2826`
2827
2828func TestRuntimeLibs(t *testing.T) {
2829 ctx := testCc(t, runtimeLibAndroidBp)
2830
2831 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002832 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002833
Justin Yun8a2600c2020-12-07 12:44:03 +09002834 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2835 checkRuntimeLibs(t, []string{"liball_available"}, module)
2836
2837 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2838 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002839
2840 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002841 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002842
2843 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2844 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002845 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002846
Justin Yun8a2600c2020-12-07 12:44:03 +09002847 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2848 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002849
2850 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002851 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002852
2853 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2854 // and product variants.
2855 variant = "android_product.VER_arm64_armv8-a_shared"
2856
2857 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2858 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2859
2860 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002861 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002862}
2863
2864func TestExcludeRuntimeLibs(t *testing.T) {
2865 ctx := testCc(t, runtimeLibAndroidBp)
2866
Colin Cross7113d202019-11-20 16:39:12 -08002867 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002868 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2869 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002870
Colin Crossfb0c16e2019-11-20 17:12:35 -08002871 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002872 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002873 checkRuntimeLibs(t, nil, module)
2874}
2875
2876func TestRuntimeLibsNoVndk(t *testing.T) {
2877 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2878
2879 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2880
Colin Cross7113d202019-11-20 16:39:12 -08002881 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002882
Justin Yun8a2600c2020-12-07 12:44:03 +09002883 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2884 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002885
2886 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002887 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002888
2889 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002890 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002891}
2892
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002893func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002894 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002895 actual := module.Properties.AndroidMkStaticLibs
2896 if !reflect.DeepEqual(actual, expected) {
2897 t.Errorf("incorrect static_libs"+
2898 "\nactual: %v"+
2899 "\nexpected: %v",
2900 actual,
2901 expected,
2902 )
2903 }
2904}
2905
2906const staticLibAndroidBp = `
2907 cc_library {
2908 name: "lib1",
2909 }
2910 cc_library {
2911 name: "lib2",
2912 static_libs: ["lib1"],
2913 }
2914`
2915
2916func TestStaticLibDepExport(t *testing.T) {
2917 ctx := testCc(t, staticLibAndroidBp)
2918
2919 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002920 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002921 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002922 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002923
2924 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002925 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002926 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2927 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002928 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002929}
2930
Jiyong Parkd08b6972017-09-26 10:50:54 +09002931var compilerFlagsTestCases = []struct {
2932 in string
2933 out bool
2934}{
2935 {
2936 in: "a",
2937 out: false,
2938 },
2939 {
2940 in: "-a",
2941 out: true,
2942 },
2943 {
2944 in: "-Ipath/to/something",
2945 out: false,
2946 },
2947 {
2948 in: "-isystempath/to/something",
2949 out: false,
2950 },
2951 {
2952 in: "--coverage",
2953 out: false,
2954 },
2955 {
2956 in: "-include a/b",
2957 out: true,
2958 },
2959 {
2960 in: "-include a/b c/d",
2961 out: false,
2962 },
2963 {
2964 in: "-DMACRO",
2965 out: true,
2966 },
2967 {
2968 in: "-DMAC RO",
2969 out: false,
2970 },
2971 {
2972 in: "-a -b",
2973 out: false,
2974 },
2975 {
2976 in: "-DMACRO=definition",
2977 out: true,
2978 },
2979 {
2980 in: "-DMACRO=defi nition",
2981 out: true, // TODO(jiyong): this should be false
2982 },
2983 {
2984 in: "-DMACRO(x)=x + 1",
2985 out: true,
2986 },
2987 {
2988 in: "-DMACRO=\"defi nition\"",
2989 out: true,
2990 },
2991}
2992
2993type mockContext struct {
2994 BaseModuleContext
2995 result bool
2996}
2997
2998func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2999 // CheckBadCompilerFlags calls this function when the flag should be rejected
3000 ctx.result = false
3001}
3002
3003func TestCompilerFlags(t *testing.T) {
3004 for _, testCase := range compilerFlagsTestCases {
3005 ctx := &mockContext{result: true}
3006 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3007 if ctx.result != testCase.out {
3008 t.Errorf("incorrect output:")
3009 t.Errorf(" input: %#v", testCase.in)
3010 t.Errorf(" expected: %#v", testCase.out)
3011 t.Errorf(" got: %#v", ctx.result)
3012 }
3013 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003014}
Jiyong Park374510b2018-03-19 18:23:01 +09003015
3016func TestVendorPublicLibraries(t *testing.T) {
3017 ctx := testCc(t, `
3018 cc_library_headers {
3019 name: "libvendorpublic_headers",
3020 export_include_dirs: ["my_include"],
3021 }
3022 vendor_public_library {
3023 name: "libvendorpublic",
3024 symbol_file: "",
3025 export_public_headers: ["libvendorpublic_headers"],
3026 }
3027 cc_library {
3028 name: "libvendorpublic",
3029 srcs: ["foo.c"],
3030 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003031 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003032 nocrt: true,
3033 }
3034
3035 cc_library {
3036 name: "libsystem",
3037 shared_libs: ["libvendorpublic"],
3038 vendor: false,
3039 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003040 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003041 nocrt: true,
3042 }
3043 cc_library {
3044 name: "libvendor",
3045 shared_libs: ["libvendorpublic"],
3046 vendor: true,
3047 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003048 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003049 nocrt: true,
3050 }
3051 `)
3052
Colin Cross7113d202019-11-20 16:39:12 -08003053 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003054 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003055
3056 // test if header search paths are correctly added
3057 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003058 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003059 cflags := cc.Args["cFlags"]
3060 if !strings.Contains(cflags, "-Imy_include") {
3061 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3062 }
3063
3064 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003065 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003066 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003067 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003068 if !strings.Contains(libflags, stubPaths[0].String()) {
3069 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3070 }
3071
3072 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003073 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003074 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003075 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003076 if !strings.Contains(libflags, stubPaths[0].String()) {
3077 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3078 }
3079
3080}
Jiyong Park37b25202018-07-11 10:49:27 +09003081
3082func TestRecovery(t *testing.T) {
3083 ctx := testCc(t, `
3084 cc_library_shared {
3085 name: "librecovery",
3086 recovery: true,
3087 }
3088 cc_library_shared {
3089 name: "librecovery32",
3090 recovery: true,
3091 compile_multilib:"32",
3092 }
Jiyong Park5baac542018-08-28 09:55:37 +09003093 cc_library_shared {
3094 name: "libHalInRecovery",
3095 recovery_available: true,
3096 vendor: true,
3097 }
Jiyong Park37b25202018-07-11 10:49:27 +09003098 `)
3099
3100 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003101 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003102 if len(variants) != 1 || !android.InList(arm64, variants) {
3103 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3104 }
3105
3106 variants = ctx.ModuleVariantsForTests("librecovery32")
3107 if android.InList(arm64, variants) {
3108 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3109 }
Jiyong Park5baac542018-08-28 09:55:37 +09003110
3111 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3112 if !recoveryModule.Platform() {
3113 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3114 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003115}
Jiyong Park5baac542018-08-28 09:55:37 +09003116
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003117func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3118 bp := `
3119 cc_prebuilt_test_library_shared {
3120 name: "test_lib",
3121 relative_install_path: "foo/bar/baz",
3122 srcs: ["srcpath/dontusethispath/baz.so"],
3123 }
3124
3125 cc_test {
3126 name: "main_test",
3127 data_libs: ["test_lib"],
3128 gtest: false,
3129 }
3130 `
3131
3132 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3133 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3134 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3135 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3136
3137 ctx := testCcWithConfig(t, config)
3138 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3139 testBinary := module.(*Module).linker.(*testBinary)
3140 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3141 if err != nil {
3142 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3143 }
3144 if len(outputFiles) != 1 {
3145 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3146 }
3147 if len(testBinary.dataPaths()) != 1 {
3148 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3149 }
3150
3151 outputPath := outputFiles[0].String()
3152
3153 if !strings.HasSuffix(outputPath, "/main_test") {
3154 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3155 }
Colin Crossaa255532020-07-03 13:18:24 -07003156 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003157 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3158 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3159 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3160 }
3161}
3162
Jiyong Park7ed9de32018-10-15 22:25:07 +09003163func TestVersionedStubs(t *testing.T) {
3164 ctx := testCc(t, `
3165 cc_library_shared {
3166 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003167 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003168 stubs: {
3169 symbol_file: "foo.map.txt",
3170 versions: ["1", "2", "3"],
3171 },
3172 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003173
Jiyong Park7ed9de32018-10-15 22:25:07 +09003174 cc_library_shared {
3175 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003176 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003177 shared_libs: ["libFoo#1"],
3178 }`)
3179
3180 variants := ctx.ModuleVariantsForTests("libFoo")
3181 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003182 "android_arm64_armv8-a_shared",
3183 "android_arm64_armv8-a_shared_1",
3184 "android_arm64_armv8-a_shared_2",
3185 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003186 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003187 "android_arm_armv7-a-neon_shared",
3188 "android_arm_armv7-a-neon_shared_1",
3189 "android_arm_armv7-a-neon_shared_2",
3190 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003191 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003192 }
3193 variantsMismatch := false
3194 if len(variants) != len(expectedVariants) {
3195 variantsMismatch = true
3196 } else {
3197 for _, v := range expectedVariants {
3198 if !inList(v, variants) {
3199 variantsMismatch = false
3200 }
3201 }
3202 }
3203 if variantsMismatch {
3204 t.Errorf("variants of libFoo expected:\n")
3205 for _, v := range expectedVariants {
3206 t.Errorf("%q\n", v)
3207 }
3208 t.Errorf(", but got:\n")
3209 for _, v := range variants {
3210 t.Errorf("%q\n", v)
3211 }
3212 }
3213
Colin Cross7113d202019-11-20 16:39:12 -08003214 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003215 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003216 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003217 if !strings.Contains(libFlags, libFoo1StubPath) {
3218 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3219 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003220
Colin Cross7113d202019-11-20 16:39:12 -08003221 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003222 cFlags := libBarCompileRule.Args["cFlags"]
3223 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3224 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3225 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3226 }
Jiyong Park37b25202018-07-11 10:49:27 +09003227}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003228
Jooyung Hanb04a4992020-03-13 18:57:35 +09003229func TestVersioningMacro(t *testing.T) {
3230 for _, tc := range []struct{ moduleName, expected string }{
3231 {"libc", "__LIBC_API__"},
3232 {"libfoo", "__LIBFOO_API__"},
3233 {"libfoo@1", "__LIBFOO_1_API__"},
3234 {"libfoo-v1", "__LIBFOO_V1_API__"},
3235 {"libfoo.v1", "__LIBFOO_V1_API__"},
3236 } {
3237 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3238 }
3239}
3240
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003241func TestStaticExecutable(t *testing.T) {
3242 ctx := testCc(t, `
3243 cc_binary {
3244 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003245 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003246 static_executable: true,
3247 }`)
3248
Colin Cross7113d202019-11-20 16:39:12 -08003249 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003250 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3251 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003252 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003253 for _, lib := range systemStaticLibs {
3254 if !strings.Contains(libFlags, lib) {
3255 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3256 }
3257 }
3258 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3259 for _, lib := range systemSharedLibs {
3260 if strings.Contains(libFlags, lib) {
3261 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3262 }
3263 }
3264}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003265
3266func TestStaticDepsOrderWithStubs(t *testing.T) {
3267 ctx := testCc(t, `
3268 cc_binary {
3269 name: "mybin",
3270 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003271 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003272 static_executable: true,
3273 stl: "none",
3274 }
3275
3276 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003277 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003278 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003279 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003280 stl: "none",
3281 }
3282
3283 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003284 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003285 srcs: ["foo.c"],
3286 stl: "none",
3287 stubs: {
3288 versions: ["1"],
3289 },
3290 }`)
3291
Colin Cross0de8a1e2020-09-18 14:15:30 -07003292 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3293 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003294 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003295
3296 if !reflect.DeepEqual(actual, expected) {
3297 t.Errorf("staticDeps orderings were not propagated correctly"+
3298 "\nactual: %v"+
3299 "\nexpected: %v",
3300 actual,
3301 expected,
3302 )
3303 }
3304}
Jooyung Han38002912019-05-16 04:01:54 +09003305
Jooyung Hand48f3c32019-08-23 11:18:57 +09003306func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3307 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3308 cc_library {
3309 name: "libA",
3310 srcs: ["foo.c"],
3311 shared_libs: ["libB"],
3312 stl: "none",
3313 }
3314
3315 cc_library {
3316 name: "libB",
3317 srcs: ["foo.c"],
3318 enabled: false,
3319 stl: "none",
3320 }
3321 `)
3322}
3323
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003324// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3325// correctly.
3326func TestFuzzTarget(t *testing.T) {
3327 ctx := testCc(t, `
3328 cc_fuzz {
3329 name: "fuzz_smoke_test",
3330 srcs: ["foo.c"],
3331 }`)
3332
Paul Duffin075c4172019-12-19 19:06:13 +00003333 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003334 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3335}
3336
Jiyong Park29074592019-07-07 16:27:47 +09003337func TestAidl(t *testing.T) {
3338}
3339
Jooyung Han38002912019-05-16 04:01:54 +09003340func assertString(t *testing.T, got, expected string) {
3341 t.Helper()
3342 if got != expected {
3343 t.Errorf("expected %q got %q", expected, got)
3344 }
3345}
3346
3347func assertArrayString(t *testing.T, got, expected []string) {
3348 t.Helper()
3349 if len(got) != len(expected) {
3350 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3351 return
3352 }
3353 for i := range got {
3354 if got[i] != expected[i] {
3355 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3356 i, expected[i], expected, got[i], got)
3357 return
3358 }
3359 }
3360}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003361
Jooyung Han0302a842019-10-30 18:43:49 +09003362func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3363 t.Helper()
3364 assertArrayString(t, android.SortedStringKeys(m), expected)
3365}
3366
Colin Crosse1bb5d02019-09-24 14:55:04 -07003367func TestDefaults(t *testing.T) {
3368 ctx := testCc(t, `
3369 cc_defaults {
3370 name: "defaults",
3371 srcs: ["foo.c"],
3372 static: {
3373 srcs: ["bar.c"],
3374 },
3375 shared: {
3376 srcs: ["baz.c"],
3377 },
3378 }
3379
3380 cc_library_static {
3381 name: "libstatic",
3382 defaults: ["defaults"],
3383 }
3384
3385 cc_library_shared {
3386 name: "libshared",
3387 defaults: ["defaults"],
3388 }
3389
3390 cc_library {
3391 name: "libboth",
3392 defaults: ["defaults"],
3393 }
3394
3395 cc_binary {
3396 name: "binary",
3397 defaults: ["defaults"],
3398 }`)
3399
3400 pathsToBase := func(paths android.Paths) []string {
3401 var ret []string
3402 for _, p := range paths {
3403 ret = append(ret, p.Base())
3404 }
3405 return ret
3406 }
3407
Colin Cross7113d202019-11-20 16:39:12 -08003408 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003409 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3410 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3411 }
Colin Cross7113d202019-11-20 16:39:12 -08003412 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003413 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3414 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3415 }
Colin Cross7113d202019-11-20 16:39:12 -08003416 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003417 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3418 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3419 }
3420
Colin Cross7113d202019-11-20 16:39:12 -08003421 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003422 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3423 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3424 }
Colin Cross7113d202019-11-20 16:39:12 -08003425 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003426 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3427 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3428 }
3429}
Colin Crosseabaedd2020-02-06 17:01:55 -08003430
3431func TestProductVariableDefaults(t *testing.T) {
3432 bp := `
3433 cc_defaults {
3434 name: "libfoo_defaults",
3435 srcs: ["foo.c"],
3436 cppflags: ["-DFOO"],
3437 product_variables: {
3438 debuggable: {
3439 cppflags: ["-DBAR"],
3440 },
3441 },
3442 }
3443
3444 cc_library {
3445 name: "libfoo",
3446 defaults: ["libfoo_defaults"],
3447 }
3448 `
3449
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003450 result := ccFixtureFactory.Extend(
3451 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003452
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003453 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3454 variables.Debuggable = BoolPtr(true)
3455 }),
3456 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003457
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003458 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003459 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003460}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003461
3462func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3463 t.Parallel()
3464 bp := `
3465 cc_library_static {
3466 name: "libfoo",
3467 srcs: ["foo.c"],
3468 whole_static_libs: ["libbar"],
3469 }
3470
3471 cc_library_static {
3472 name: "libbar",
3473 whole_static_libs: ["libmissing"],
3474 }
3475 `
3476
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003477 result := ccFixtureFactory.Extend(
3478 android.PrepareForTestWithAllowMissingDependencies,
3479 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003480
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003481 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003482 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003483
Paul Duffine84b1332021-03-12 11:59:43 +00003484 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003485
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003486 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003487 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003488}
Colin Crosse9fe2942020-11-10 18:12:15 -08003489
3490func TestInstallSharedLibs(t *testing.T) {
3491 bp := `
3492 cc_binary {
3493 name: "bin",
3494 host_supported: true,
3495 shared_libs: ["libshared"],
3496 runtime_libs: ["libruntime"],
3497 srcs: [":gen"],
3498 }
3499
3500 cc_library_shared {
3501 name: "libshared",
3502 host_supported: true,
3503 shared_libs: ["libtransitive"],
3504 }
3505
3506 cc_library_shared {
3507 name: "libtransitive",
3508 host_supported: true,
3509 }
3510
3511 cc_library_shared {
3512 name: "libruntime",
3513 host_supported: true,
3514 }
3515
3516 cc_binary_host {
3517 name: "tool",
3518 srcs: ["foo.cpp"],
3519 }
3520
3521 genrule {
3522 name: "gen",
3523 tools: ["tool"],
3524 out: ["gen.cpp"],
3525 cmd: "$(location tool) $(out)",
3526 }
3527 `
3528
3529 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3530 ctx := testCcWithConfig(t, config)
3531
3532 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3533 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3534 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3535 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3536 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3537
3538 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3539 t.Errorf("expected host bin dependency %q, got %q", w, g)
3540 }
3541
3542 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3543 t.Errorf("expected host bin dependency %q, got %q", w, g)
3544 }
3545
3546 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3547 t.Errorf("expected host bin dependency %q, got %q", w, g)
3548 }
3549
3550 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3551 t.Errorf("expected host bin dependency %q, got %q", w, g)
3552 }
3553
3554 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3555 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3556 }
3557
3558 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3559 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3560 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3561 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3562
3563 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3564 t.Errorf("expected device bin dependency %q, got %q", w, g)
3565 }
3566
3567 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3568 t.Errorf("expected device bin dependency %q, got %q", w, g)
3569 }
3570
3571 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3572 t.Errorf("expected device bin dependency %q, got %q", w, g)
3573 }
3574
3575 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3576 t.Errorf("expected device bin dependency %q, got %q", w, g)
3577 }
3578
3579 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3580 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3581 }
3582
3583}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003584
3585func TestStubsLibReexportsHeaders(t *testing.T) {
3586 ctx := testCc(t, `
3587 cc_library_shared {
3588 name: "libclient",
3589 srcs: ["foo.c"],
3590 shared_libs: ["libfoo#1"],
3591 }
3592
3593 cc_library_shared {
3594 name: "libfoo",
3595 srcs: ["foo.c"],
3596 shared_libs: ["libbar"],
3597 export_shared_lib_headers: ["libbar"],
3598 stubs: {
3599 symbol_file: "foo.map.txt",
3600 versions: ["1", "2", "3"],
3601 },
3602 }
3603
3604 cc_library_shared {
3605 name: "libbar",
3606 export_include_dirs: ["include/libbar"],
3607 srcs: ["foo.c"],
3608 }`)
3609
3610 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3611
3612 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3613 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3614 }
3615}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003616
3617func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3618 ctx := testCc(t, `
3619 cc_library {
3620 name: "libfoo",
3621 srcs: ["a/Foo.aidl"],
3622 aidl: { flags: ["-Werror"], },
3623 }
3624 `)
3625
3626 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3627 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3628 aidlCommand := manifest.Commands[0].GetCommand()
3629 expectedAidlFlag := "-Werror"
3630 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3631 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3632 }
3633}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003634
Jiyong Parka008fb02021-03-16 17:15:53 +09003635func TestMinSdkVersionInClangTriple(t *testing.T) {
3636 ctx := testCc(t, `
3637 cc_library_shared {
3638 name: "libfoo",
3639 srcs: ["foo.c"],
3640 min_sdk_version: "29",
3641 }`)
3642
3643 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3644 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3645}
3646
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003647func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3648 ctx := testCc(t, `
3649 cc_object {
3650 name: "crt_foo",
3651 srcs: ["foo.c"],
3652 crt: true,
3653 stl: "none",
3654 min_sdk_version: "28",
3655
3656 }`)
3657
3658 arch := "android_arm64_armv8-a"
3659 for _, v := range []string{"", "28", "29", "30", "current"} {
3660 var variant string
3661 if v == "" {
3662 variant = arch
3663 } else {
3664 variant = arch + "_sdk_" + v
3665 }
3666 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3667 vNum := v
3668 if v == "current" || v == "" {
3669 vNum = "10000"
3670 }
3671 expected := "-target aarch64-linux-android" + vNum + " "
3672 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3673 }
3674}
3675
3676func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3677 ctx := testCc(t, `
3678 cc_binary {
3679 name: "bin",
3680 srcs: ["foo.c"],
3681 stl: "none",
3682 min_sdk_version: "29",
3683 sdk_version: "current",
3684 }
3685 `)
3686
3687 // Sdk variant uses the crt object of the matching min_sdk_version
3688 variant := "android_arm64_armv8-a_sdk"
3689 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3690 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3691 variant+"_29/crtbegin_dynamic.o")
3692
3693 // platform variant uses the crt object built for platform
3694 variant = "android_arm64_armv8-a"
3695 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3696 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3697 variant+"/crtbegin_dynamic.o")
3698}
3699
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003700type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003701
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003702const (
3703 None MemtagNoteType = iota + 1
3704 Sync
3705 Async
3706)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003707
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003708func (t MemtagNoteType) str() string {
3709 switch t {
3710 case None:
3711 return "none"
3712 case Sync:
3713 return "sync"
3714 case Async:
3715 return "async"
3716 default:
3717 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003718 }
3719}
3720
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003721func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3722 note_async := "note_memtag_heap_async"
3723 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003724
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003725 found := None
3726 implicits := m.Rule("ld").Implicits
3727 for _, lib := range implicits {
3728 if strings.Contains(lib.Rel(), note_async) {
3729 found = Async
3730 break
3731 } else if strings.Contains(lib.Rel(), note_sync) {
3732 found = Sync
3733 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003734 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003735 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003736
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003737 if found != expected {
3738 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3739 }
3740}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003741
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003742var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3743 android.FixtureModifyMockFS(func(fs android.MockFS) {
3744 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003745 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003746 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003747 gtest: false,
3748 }
3749
3750 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003751 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003752 gtest: false,
3753 sanitize: { memtag_heap: false },
3754 }
3755
3756 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003757 name: "%[1]s_test_true",
3758 gtest: false,
3759 sanitize: { memtag_heap: true },
3760 }
3761
3762 cc_test {
3763 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003764 gtest: false,
3765 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3766 }
3767
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003768 cc_test {
3769 name: "%[1]s_test_true_diag",
3770 gtest: false,
3771 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3772 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003773
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003774 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003775 name: "%[1]s_binary",
3776 }
3777
3778 cc_binary {
3779 name: "%[1]s_binary_false",
3780 sanitize: { memtag_heap: false },
3781 }
3782
3783 cc_binary {
3784 name: "%[1]s_binary_true",
3785 sanitize: { memtag_heap: true },
3786 }
3787
3788 cc_binary {
3789 name: "%[1]s_binary_true_nodiag",
3790 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3791 }
3792
3793 cc_binary {
3794 name: "%[1]s_binary_true_diag",
3795 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003796 }
3797 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003798 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3799 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3800 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3801 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003802
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003803 fs.Merge(android.MockFS{
3804 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3805 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3806 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3807 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3808 })
3809 }),
3810 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3811 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3812 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3813 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3814 }),
3815)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003816
3817func TestSanitizeMemtagHeap(t *testing.T) {
3818 variant := "android_arm64_armv8-a"
3819
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003820 result := ccFixtureFactory.Extend(prepareForTestWithMemtagHeap).RunTest(t)
3821 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003822
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003823 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3824 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3825 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3826 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3827 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3828
3829 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3830 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3833 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3834
3835 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3839 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3840
3841 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3845 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3846
3847 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3851 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3852
3853 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3857 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3858
3859 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3860 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3861 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3862 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3863 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3864
3865 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3866 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3867 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3868 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3869 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3870}
3871
3872func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003873 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003874
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003875 result := ccFixtureFactory.Extend(
3876 prepareForTestWithMemtagHeap,
3877 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3878 variables.SanitizeDevice = []string{"memtag_heap"}
3879 }),
3880 ).RunTest(t)
3881 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003882
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003883 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3884 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3885 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3886 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3887 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003888
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003889 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3893 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3894
3895 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3896 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3899 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3900
3901 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3902 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3905 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3906
3907 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3911 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3912
3913 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3917 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3918
3919 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3920 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3921 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3922 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3923 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3924
3925 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3926 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3927 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3928 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3929 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3930}
3931
3932func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3933 variant := "android_arm64_armv8-a"
3934
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003935 result := ccFixtureFactory.Extend(
3936 prepareForTestWithMemtagHeap,
3937 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3938 variables.SanitizeDevice = []string{"memtag_heap"}
3939 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3940 }),
3941 ).RunTest(t)
3942 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003943
3944 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3947 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3949
3950 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3955
3956 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3961
3962 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3979
3980 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3981 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3982 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3983 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3984 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3985
3986 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3987 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3988 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3989 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3990 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003991}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003992
3993func TestIncludeDirsExporting(t *testing.T) {
3994
3995 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3996 // embedded newline characters alone.
3997 trimIndentingSpaces := func(s string) string {
3998 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3999 }
4000
4001 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4002 t.Helper()
4003 expected = trimIndentingSpaces(expected)
4004 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4005 if expected != actual {
4006 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4007 }
4008 }
4009
4010 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4011
4012 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4013 t.Helper()
4014 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4015 name := module.Name()
4016
4017 for _, checker := range checkers {
4018 checker(t, name, exported)
4019 }
4020 }
4021
4022 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4023 return func(t *testing.T, name string, exported FlagExporterInfo) {
4024 t.Helper()
4025 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4026 }
4027 }
4028
4029 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4030 return func(t *testing.T, name string, exported FlagExporterInfo) {
4031 t.Helper()
4032 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4033 }
4034 }
4035
4036 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4037 return func(t *testing.T, name string, exported FlagExporterInfo) {
4038 t.Helper()
4039 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4040 }
4041 }
4042
4043 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4044 return func(t *testing.T, name string, exported FlagExporterInfo) {
4045 t.Helper()
4046 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4047 }
4048 }
4049
4050 genRuleModules := `
4051 genrule {
4052 name: "genrule_foo",
4053 cmd: "generate-foo",
4054 out: [
4055 "generated_headers/foo/generated_header.h",
4056 ],
4057 export_include_dirs: [
4058 "generated_headers",
4059 ],
4060 }
4061
4062 genrule {
4063 name: "genrule_bar",
4064 cmd: "generate-bar",
4065 out: [
4066 "generated_headers/bar/generated_header.h",
4067 ],
4068 export_include_dirs: [
4069 "generated_headers",
4070 ],
4071 }
4072 `
4073
4074 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4075 ctx := testCc(t, genRuleModules+`
4076 cc_library {
4077 name: "libfoo",
4078 srcs: ["foo.c"],
4079 export_include_dirs: ["foo/standard"],
4080 export_system_include_dirs: ["foo/system"],
4081 generated_headers: ["genrule_foo"],
4082 export_generated_headers: ["genrule_foo"],
4083 }
4084
4085 cc_library {
4086 name: "libbar",
4087 srcs: ["bar.c"],
4088 shared_libs: ["libfoo"],
4089 export_include_dirs: ["bar/standard"],
4090 export_system_include_dirs: ["bar/system"],
4091 generated_headers: ["genrule_bar"],
4092 export_generated_headers: ["genrule_bar"],
4093 }
4094 `)
4095 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4096 checkIncludeDirs(t, ctx, foo,
4097 expectedIncludeDirs(`
4098 foo/standard
4099 .intermediates/genrule_foo/gen/generated_headers
4100 `),
4101 expectedSystemIncludeDirs(`foo/system`),
4102 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4103 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4104 )
4105
4106 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4107 checkIncludeDirs(t, ctx, bar,
4108 expectedIncludeDirs(`
4109 bar/standard
4110 .intermediates/genrule_bar/gen/generated_headers
4111 `),
4112 expectedSystemIncludeDirs(`bar/system`),
4113 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4114 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4115 )
4116 })
4117
4118 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4119 ctx := testCc(t, genRuleModules+`
4120 cc_library {
4121 name: "libfoo",
4122 srcs: ["foo.c"],
4123 export_include_dirs: ["foo/standard"],
4124 export_system_include_dirs: ["foo/system"],
4125 generated_headers: ["genrule_foo"],
4126 export_generated_headers: ["genrule_foo"],
4127 }
4128
4129 cc_library {
4130 name: "libbar",
4131 srcs: ["bar.c"],
4132 whole_static_libs: ["libfoo"],
4133 export_include_dirs: ["bar/standard"],
4134 export_system_include_dirs: ["bar/system"],
4135 generated_headers: ["genrule_bar"],
4136 export_generated_headers: ["genrule_bar"],
4137 }
4138 `)
4139 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4140 checkIncludeDirs(t, ctx, foo,
4141 expectedIncludeDirs(`
4142 foo/standard
4143 .intermediates/genrule_foo/gen/generated_headers
4144 `),
4145 expectedSystemIncludeDirs(`foo/system`),
4146 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4147 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4148 )
4149
4150 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4151 checkIncludeDirs(t, ctx, bar,
4152 expectedIncludeDirs(`
4153 bar/standard
4154 foo/standard
4155 .intermediates/genrule_foo/gen/generated_headers
4156 .intermediates/genrule_bar/gen/generated_headers
4157 `),
4158 expectedSystemIncludeDirs(`
4159 bar/system
4160 foo/system
4161 `),
4162 expectedGeneratedHeaders(`
4163 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4164 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4165 `),
4166 expectedOrderOnlyDeps(`
4167 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4168 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4169 `),
4170 )
4171 })
4172
Paul Duffin3cb603e2021-02-19 13:57:10 +00004173 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4174 ctx := testCc(t, genRuleModules+`
4175 cc_library_shared {
4176 name: "libfoo",
4177 srcs: [
4178 "foo.c",
4179 "b.aidl",
4180 "a.proto",
4181 ],
4182 aidl: {
4183 export_aidl_headers: true,
4184 }
4185 }
4186 `)
4187 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4188 checkIncludeDirs(t, ctx, foo,
4189 expectedIncludeDirs(`
4190 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4191 `),
4192 expectedSystemIncludeDirs(``),
4193 expectedGeneratedHeaders(`
4194 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4195 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4196 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004197 `),
4198 expectedOrderOnlyDeps(`
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 )
4204 })
4205
Paul Duffin3cb603e2021-02-19 13:57:10 +00004206 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4207 ctx := testCc(t, genRuleModules+`
4208 cc_library_shared {
4209 name: "libfoo",
4210 srcs: [
4211 "foo.c",
4212 "b.aidl",
4213 "a.proto",
4214 ],
4215 proto: {
4216 export_proto_headers: true,
4217 }
4218 }
4219 `)
4220 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4221 checkIncludeDirs(t, ctx, foo,
4222 expectedIncludeDirs(`
4223 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4224 `),
4225 expectedSystemIncludeDirs(``),
4226 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004227 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4228 `),
4229 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004230 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4231 `),
4232 )
4233 })
4234
Paul Duffin33056e82021-02-19 13:49:08 +00004235 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004236 ctx := testCc(t, genRuleModules+`
4237 cc_library_shared {
4238 name: "libfoo",
4239 srcs: [
4240 "foo.c",
4241 "a.sysprop",
4242 "b.aidl",
4243 "a.proto",
4244 ],
4245 }
4246 `)
4247 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4248 checkIncludeDirs(t, ctx, foo,
4249 expectedIncludeDirs(`
4250 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4251 `),
4252 expectedSystemIncludeDirs(``),
4253 expectedGeneratedHeaders(`
4254 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004255 `),
4256 expectedOrderOnlyDeps(`
4257 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4258 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004259 `),
4260 )
4261 })
4262}