blob: 2190b89aee52ef39dac713952ebd3780c6b74e0e [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"
Jeff Gaston294356f2017-09-27 17:05:30 -070023 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070024 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070025
26 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070027)
28
Jiyong Park6a43f042017-10-12 23:05:00 +090029var buildDir string
30
31func setUp() {
32 var err error
33 buildDir, err = ioutil.TempDir("", "soong_cc_test")
34 if err != nil {
35 panic(err)
36 }
37}
38
39func tearDown() {
40 os.RemoveAll(buildDir)
41}
42
43func TestMain(m *testing.M) {
44 run := func() int {
45 setUp()
46 defer tearDown()
47
48 return m.Run()
49 }
50
51 os.Exit(run())
52}
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070055 t.Helper()
Colin Crossae8600b2020-10-29 17:09:13 -070056 ctx := CreateTestContext(config)
57 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080058
Jeff Gastond3e141d2017-08-08 17:46:01 -070059 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Logan Chien42039712018-03-12 16:29:17 +080060 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090061 _, errs = ctx.PrepareBuildActions(config)
Logan Chien42039712018-03-12 16:29:17 +080062 android.FailIfErrored(t, errs)
Jiyong Park6a43f042017-10-12 23:05:00 +090063
64 return ctx
65}
66
Logan Chienf3511742017-10-31 18:04:35 +080067func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080068 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080069 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070070 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun8a2600c2020-12-07 12:44:03 +090071 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Dan Willemsen674dc7f2018-03-12 18:06:05 -070072 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080073
Colin Cross98be1bb2019-12-13 20:41:13 -080074 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080075}
76
77func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080078 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -080079 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070080 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080081
Colin Cross98be1bb2019-12-13 20:41:13 -080082 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080083}
84
Justin Yun8a2600c2020-12-07 12:44:03 +090085func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
86 t.Helper()
87 config := TestConfig(buildDir, android.Android, nil, bp, nil)
88 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
89 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
90
91 return testCcWithConfig(t, config)
92}
93
Justin Yun5f7f7e82019-11-18 19:52:14 +090094func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +080095 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +080096
Colin Crossae8600b2020-10-29 17:09:13 -070097 ctx := CreateTestContext(config)
98 ctx.Register()
Logan Chienf3511742017-10-31 18:04:35 +080099
100 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
101 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800102 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800103 return
104 }
105
106 _, errs = ctx.PrepareBuildActions(config)
107 if len(errs) > 0 {
Logan Chienee97c3e2018-03-12 16:34:26 +0800108 android.FailIfNoMatchingErrors(t, pattern, errs)
Logan Chienf3511742017-10-31 18:04:35 +0800109 return
110 }
111
112 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
113}
114
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900116 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900117 config := TestConfig(buildDir, android.Android, nil, bp, nil)
118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
119 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
124func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900125 t.Helper()
Justin Yun5f7f7e82019-11-18 19:52:14 +0900126 config := TestConfig(buildDir, android.Android, nil, bp, nil)
127 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
128 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
129 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
130 testCcErrorWithConfig(t, pattern, config)
131 return
132}
133
Logan Chienf3511742017-10-31 18:04:35 +0800134const (
Colin Cross7113d202019-11-20 16:39:12 -0800135 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800136 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800138 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800139)
140
Doug Hornc32c6b02019-01-17 14:44:05 -0800141func TestFuchsiaDeps(t *testing.T) {
142 t.Helper()
143
144 bp := `
145 cc_library {
146 name: "libTest",
147 srcs: ["foo.c"],
148 target: {
149 fuchsia: {
150 srcs: ["bar.c"],
151 },
152 },
153 }`
154
Colin Cross98be1bb2019-12-13 20:41:13 -0800155 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
156 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800157
158 rt := false
159 fb := false
160
161 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
162 implicits := ld.Implicits
163 for _, lib := range implicits {
164 if strings.Contains(lib.Rel(), "libcompiler_rt") {
165 rt = true
166 }
167
168 if strings.Contains(lib.Rel(), "libbioniccompat") {
169 fb = true
170 }
171 }
172
173 if !rt || !fb {
174 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
175 }
176}
177
178func TestFuchsiaTargetDecl(t *testing.T) {
179 t.Helper()
180
181 bp := `
182 cc_library {
183 name: "libTest",
184 srcs: ["foo.c"],
185 target: {
186 fuchsia: {
187 srcs: ["bar.c"],
188 },
189 },
190 }`
191
Colin Cross98be1bb2019-12-13 20:41:13 -0800192 config := TestConfig(buildDir, android.Fuchsia, nil, bp, nil)
193 ctx := testCcWithConfig(t, config)
Doug Hornc32c6b02019-01-17 14:44:05 -0800194 ld := ctx.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
195 var objs []string
196 for _, o := range ld.Inputs {
197 objs = append(objs, o.Base())
198 }
199 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
200 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
201 }
202}
203
Jiyong Park6a43f042017-10-12 23:05:00 +0900204func TestVendorSrc(t *testing.T) {
205 ctx := testCc(t, `
206 cc_library {
207 name: "libTest",
208 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700209 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800210 nocrt: true,
211 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900212 vendor_available: true,
213 target: {
214 vendor: {
215 srcs: ["bar.c"],
216 },
217 },
218 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900219 `)
220
Logan Chienf3511742017-10-31 18:04:35 +0800221 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900222 var objs []string
223 for _, o := range ld.Inputs {
224 objs = append(objs, o.Base())
225 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800226 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900227 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
228 }
229}
230
Logan Chienf3511742017-10-31 18:04:35 +0800231func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900232 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800233
Logan Chiend3c59a22018-03-29 14:08:15 +0800234 t.Helper()
235
Justin Yun0ecf0b22020-02-28 15:07:59 +0900236 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Ivan Lozano52767be2019-10-18 14:49:46 -0700237 if !mod.HasVendorVariant() {
Justin Yun0ecf0b22020-02-28 15:07:59 +0900238 t.Errorf("%q must have variant %q", name, variant)
Logan Chienf3511742017-10-31 18:04:35 +0800239 }
240
241 // Check library properties.
242 lib, ok := mod.compiler.(*libraryDecorator)
243 if !ok {
244 t.Errorf("%q must have libraryDecorator", name)
245 } else if lib.baseInstaller.subDir != subDir {
246 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
247 lib.baseInstaller.subDir)
248 }
249
250 // Check VNDK properties.
251 if mod.vndkdep == nil {
252 t.Fatalf("%q must have `vndkdep`", name)
253 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700254 if !mod.IsVndk() {
255 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800256 }
257 if mod.isVndkSp() != isVndkSp {
258 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
259 }
260
261 // Check VNDK extension properties.
262 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500263 if mod.IsVndkExt() != isVndkExt {
264 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800265 }
266
267 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
268 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
269 }
270}
271
Bill Peckham945441c2020-08-31 16:07:58 -0700272func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool) {
273 t.Helper()
Jooyung Han39edb6c2019-11-06 16:53:07 +0900274 mod, ok := ctx.ModuleForTests(moduleName, variant).Module().(android.OutputFileProducer)
275 if !ok {
276 t.Errorf("%q must have output\n", moduleName)
Inseob Kim1f086e22019-05-09 13:29:15 +0900277 return
278 }
Jooyung Han39edb6c2019-11-06 16:53:07 +0900279 outputFiles, err := mod.OutputFiles("")
280 if err != nil || len(outputFiles) != 1 {
281 t.Errorf("%q must have single output\n", moduleName)
282 return
283 }
284 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900285
Bill Peckham945441c2020-08-31 16:07:58 -0700286 if include {
287 out := singleton.Output(snapshotPath)
288 if out.Input.String() != outputFiles[0].String() {
289 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
290 }
291 } else {
292 out := singleton.MaybeOutput(snapshotPath)
293 if out.Rule != nil {
294 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
295 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900296 }
297}
298
Bill Peckham945441c2020-08-31 16:07:58 -0700299func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
300 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true)
301}
302
303func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
304 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false)
305}
306
Jooyung Han2216fb12019-11-06 16:46:15 +0900307func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
308 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800309 content := android.ContentFromFileRuleForTests(t, params)
310 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900311 assertArrayString(t, actual, expected)
312}
313
Jooyung Han097087b2019-10-22 19:32:18 +0900314func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
315 t.Helper()
316 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900317 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
318}
319
320func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
321 t.Helper()
322 vndkLibraries := ctx.ModuleForTests(module, "")
Kiyoung Kime1aa8ea2019-12-30 11:12:55 +0900323
324 var output string
325 if module != "vndkcorevariant.libraries.txt" {
326 output = insertVndkVersion(module, "VER")
327 } else {
328 output = module
329 }
330
Jooyung Han2216fb12019-11-06 16:46:15 +0900331 checkWriteFileOutput(t, vndkLibraries.Output(output), expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900332}
333
Logan Chienf3511742017-10-31 18:04:35 +0800334func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800335 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800336 cc_library {
337 name: "libvndk",
338 vendor_available: true,
339 vndk: {
340 enabled: true,
341 },
342 nocrt: true,
343 }
344
345 cc_library {
346 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900347 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800348 vndk: {
349 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900350 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800351 },
352 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900353 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800354 }
355
356 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900357 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800358 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900359 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800360 vndk: {
361 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900362 },
363 nocrt: true,
364 target: {
365 vendor: {
366 cflags: ["-DTEST"],
367 },
368 product: {
369 cflags: ["-DTEST"],
370 },
371 },
372 }
373
374 cc_library {
375 name: "libvndk_sp",
376 vendor_available: true,
377 vndk: {
378 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800379 support_system_process: true,
380 },
381 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900382 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800383 }
384
385 cc_library {
386 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900387 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800388 vndk: {
389 enabled: true,
390 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900391 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800392 },
393 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900394 target: {
395 vendor: {
396 suffix: "-x",
397 },
398 },
Logan Chienf3511742017-10-31 18:04:35 +0800399 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900400
401 cc_library {
402 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900403 vendor_available: true,
404 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900405 vndk: {
406 enabled: true,
407 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900408 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900409 },
410 nocrt: true,
411 target: {
412 vendor: {
413 suffix: "-x",
414 },
415 product: {
416 suffix: "-x",
417 },
418 },
419 }
420
Jooyung Han2216fb12019-11-06 16:46:15 +0900421 vndk_libraries_txt {
422 name: "llndk.libraries.txt",
423 }
424 vndk_libraries_txt {
425 name: "vndkcore.libraries.txt",
426 }
427 vndk_libraries_txt {
428 name: "vndksp.libraries.txt",
429 }
430 vndk_libraries_txt {
431 name: "vndkprivate.libraries.txt",
432 }
433 vndk_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900434 name: "vndkproduct.libraries.txt",
435 }
436 vndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900437 name: "vndkcorevariant.libraries.txt",
438 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800439 `
440
441 config := TestConfig(buildDir, android.Android, nil, bp, nil)
442 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900443 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800444 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
445
446 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800447
Jooyung Han261e1582020-10-20 18:54:21 +0900448 // subdir == "" because VNDK libs are not supposed to be installed separately.
449 // They are installed as part of VNDK APEX instead.
450 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
451 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900452 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900453 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
454 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900455 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900456
Justin Yun6977e8a2020-10-29 18:24:11 +0900457 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
458 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900459
Inseob Kim1f086e22019-05-09 13:29:15 +0900460 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900461 snapshotDir := "vndk-snapshot"
462 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
463
464 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
465 "arm64", "armv8-a"))
466 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
467 "arm", "armv7-a-neon"))
468
469 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
470 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
471 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
472 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
473
Colin Crossfb0c16e2019-11-20 17:12:35 -0800474 variant := "android_vendor.VER_arm64_armv8-a_shared"
475 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900476
Inseob Kim7f283f42020-06-01 21:53:49 +0900477 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
478
479 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
480 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900481 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
482 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900483 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
484 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900485
Jooyung Han39edb6c2019-11-06 16:53:07 +0900486 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900487 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
488 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
489 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
490 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900491 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900492
Jooyung Han097087b2019-10-22 19:32:18 +0900493 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
494 "LLNDK: libc.so",
495 "LLNDK: libdl.so",
496 "LLNDK: libft2.so",
497 "LLNDK: libm.so",
498 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900499 "VNDK-SP: libvndk_sp-x.so",
500 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900501 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900502 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900503 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900504 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900505 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900506 "VNDK-private: libvndk-private.so",
507 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900508 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900509 "VNDK-product: libc++.so",
510 "VNDK-product: libvndk_product.so",
511 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900512 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900513 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900514 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
515 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
516 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 +0900517 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 +0900518 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
519}
520
Yo Chiangbba545e2020-06-09 16:15:37 +0800521func TestVndkWithHostSupported(t *testing.T) {
522 ctx := testCc(t, `
523 cc_library {
524 name: "libvndk_host_supported",
525 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900526 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800527 vndk: {
528 enabled: true,
529 },
530 host_supported: true,
531 }
532
533 cc_library {
534 name: "libvndk_host_supported_but_disabled_on_device",
535 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900536 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800537 vndk: {
538 enabled: true,
539 },
540 host_supported: true,
541 enabled: false,
542 target: {
543 host: {
544 enabled: true,
545 }
546 }
547 }
548
549 vndk_libraries_txt {
550 name: "vndkcore.libraries.txt",
551 }
552 `)
553
554 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
555}
556
Jooyung Han2216fb12019-11-06 16:46:15 +0900557func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800558 bp := `
Jooyung Han2216fb12019-11-06 16:46:15 +0900559 vndk_libraries_txt {
560 name: "llndk.libraries.txt",
Colin Cross98be1bb2019-12-13 20:41:13 -0800561 }`
562 config := TestConfig(buildDir, android.Android, nil, bp, nil)
563 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
564 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
565 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900566
567 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900568 entries := android.AndroidMkEntriesForTest(t, config, "", module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900569 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900570}
571
572func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800573 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900574 cc_library {
575 name: "libvndk",
576 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900577 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900578 vndk: {
579 enabled: true,
580 },
581 nocrt: true,
582 }
583
584 cc_library {
585 name: "libvndk_sp",
586 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900587 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900588 vndk: {
589 enabled: true,
590 support_system_process: true,
591 },
592 nocrt: true,
593 }
594
595 cc_library {
596 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900597 vendor_available: true,
598 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900599 vndk: {
600 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900601 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900602 },
603 nocrt: true,
604 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900605
606 vndk_libraries_txt {
607 name: "vndkcorevariant.libraries.txt",
608 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800609 `
610
611 config := TestConfig(buildDir, android.Android, nil, bp, nil)
612 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
613 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
614 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
615
616 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
617
618 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900619
Jooyung Han2216fb12019-11-06 16:46:15 +0900620 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900621}
622
Chris Parsons79d66a52020-06-05 17:26:16 -0400623func TestDataLibs(t *testing.T) {
624 bp := `
625 cc_test_library {
626 name: "test_lib",
627 srcs: ["test_lib.cpp"],
628 gtest: false,
629 }
630
631 cc_test {
632 name: "main_test",
633 data_libs: ["test_lib"],
634 gtest: false,
635 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400636 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400637
638 config := TestConfig(buildDir, android.Android, nil, bp, nil)
639 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
640 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
641 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
642
643 ctx := testCcWithConfig(t, config)
644 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
645 testBinary := module.(*Module).linker.(*testBinary)
646 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
647 if err != nil {
648 t.Errorf("Expected cc_test to produce output files, error: %s", err)
649 return
650 }
651 if len(outputFiles) != 1 {
652 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
653 return
654 }
655 if len(testBinary.dataPaths()) != 1 {
656 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
657 return
658 }
659
660 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400661 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400662
663 if !strings.HasSuffix(outputPath, "/main_test") {
664 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
665 return
666 }
667 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
668 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
669 return
670 }
671}
672
Chris Parsons216e10a2020-07-09 17:12:52 -0400673func TestDataLibsRelativeInstallPath(t *testing.T) {
674 bp := `
675 cc_test_library {
676 name: "test_lib",
677 srcs: ["test_lib.cpp"],
678 relative_install_path: "foo/bar/baz",
679 gtest: false,
680 }
681
682 cc_test {
683 name: "main_test",
684 data_libs: ["test_lib"],
685 gtest: false,
686 }
687 `
688
689 config := TestConfig(buildDir, android.Android, nil, bp, nil)
690 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
691 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
692 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
693
694 ctx := testCcWithConfig(t, config)
695 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
696 testBinary := module.(*Module).linker.(*testBinary)
697 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
698 if err != nil {
699 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
700 }
701 if len(outputFiles) != 1 {
702 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
703 }
704 if len(testBinary.dataPaths()) != 1 {
705 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
706 }
707
708 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400709
710 if !strings.HasSuffix(outputPath, "/main_test") {
711 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
712 }
713 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
714 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
715 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400716 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400717 }
718}
719
Jooyung Han0302a842019-10-30 18:43:49 +0900720func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900721 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900722 cc_library {
723 name: "libvndk",
724 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900725 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900726 vndk: {
727 enabled: true,
728 },
729 nocrt: true,
730 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900731 cc_library {
732 name: "libvndk-private",
733 vendor_available: false,
734 product_available: false,
735 vndk: {
736 enabled: true,
737 },
738 nocrt: true,
739 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900740 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900741
742 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
743 "LLNDK: libc.so",
744 "LLNDK: libdl.so",
745 "LLNDK: libft2.so",
746 "LLNDK: libm.so",
747 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900748 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900749 "VNDK-core: libvndk.so",
750 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900751 "VNDK-private: libvndk-private.so",
752 "VNDK-product: libc++.so",
753 "VNDK-product: libvndk-private.so",
754 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900755 })
Logan Chienf3511742017-10-31 18:04:35 +0800756}
757
Justin Yun63e9ec72020-10-29 16:49:43 +0900758func TestVndkModuleError(t *testing.T) {
759 // Check the error message for vendor_available and product_available properties.
Justin Yun6977e8a2020-10-29 18:24:11 +0900760 testCcErrorProductVndk(t, "vndk: vendor_available must be set to either true or false when `vndk: {enabled: true}`", `
761 cc_library {
762 name: "libvndk",
763 vndk: {
764 enabled: true,
765 },
766 nocrt: true,
767 }
768 `)
769
770 testCcErrorProductVndk(t, "vndk: vendor_available must be set to either true or false when `vndk: {enabled: true}`", `
771 cc_library {
772 name: "libvndk",
773 product_available: true,
774 vndk: {
775 enabled: true,
776 },
777 nocrt: true,
778 }
779 `)
780
Justin Yun6977e8a2020-10-29 18:24:11 +0900781 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
782 cc_library {
783 name: "libvndkprop",
784 vendor_available: true,
785 product_available: true,
786 vndk: {
787 enabled: true,
788 },
789 nocrt: true,
790 target: {
791 vendor: {
792 cflags: ["-DTEST",],
793 },
794 },
795 }
796 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900797}
798
Logan Chiend3c59a22018-03-29 14:08:15 +0800799func TestVndkDepError(t *testing.T) {
800 // Check whether an error is emitted when a VNDK lib depends on a system lib.
801 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
802 cc_library {
803 name: "libvndk",
804 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900805 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800806 vndk: {
807 enabled: true,
808 },
809 shared_libs: ["libfwk"], // Cause error
810 nocrt: true,
811 }
812
813 cc_library {
814 name: "libfwk",
815 nocrt: true,
816 }
817 `)
818
819 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
820 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
821 cc_library {
822 name: "libvndk",
823 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900824 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800825 vndk: {
826 enabled: true,
827 },
828 shared_libs: ["libvendor"], // Cause error
829 nocrt: true,
830 }
831
832 cc_library {
833 name: "libvendor",
834 vendor: true,
835 nocrt: true,
836 }
837 `)
838
839 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
840 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
841 cc_library {
842 name: "libvndk_sp",
843 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900844 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800845 vndk: {
846 enabled: true,
847 support_system_process: true,
848 },
849 shared_libs: ["libfwk"], // Cause error
850 nocrt: true,
851 }
852
853 cc_library {
854 name: "libfwk",
855 nocrt: true,
856 }
857 `)
858
859 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
860 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
861 cc_library {
862 name: "libvndk_sp",
863 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900864 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800865 vndk: {
866 enabled: true,
867 support_system_process: true,
868 },
869 shared_libs: ["libvendor"], // Cause error
870 nocrt: true,
871 }
872
873 cc_library {
874 name: "libvendor",
875 vendor: true,
876 nocrt: true,
877 }
878 `)
879
880 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
881 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
882 cc_library {
883 name: "libvndk_sp",
884 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900885 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800886 vndk: {
887 enabled: true,
888 support_system_process: true,
889 },
890 shared_libs: ["libvndk"], // Cause error
891 nocrt: true,
892 }
893
894 cc_library {
895 name: "libvndk",
896 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900897 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800898 vndk: {
899 enabled: true,
900 },
901 nocrt: true,
902 }
903 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900904
905 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
906 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
907 cc_library {
908 name: "libvndk",
909 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900910 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900911 vndk: {
912 enabled: true,
913 },
914 shared_libs: ["libnonvndk"],
915 nocrt: true,
916 }
917
918 cc_library {
919 name: "libnonvndk",
920 vendor_available: true,
921 nocrt: true,
922 }
923 `)
924
925 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
926 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
927 cc_library {
928 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900929 vendor_available: true,
930 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900931 vndk: {
932 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900933 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900934 },
935 shared_libs: ["libnonvndk"],
936 nocrt: true,
937 }
938
939 cc_library {
940 name: "libnonvndk",
941 vendor_available: true,
942 nocrt: true,
943 }
944 `)
945
946 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
947 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
948 cc_library {
949 name: "libvndksp",
950 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900951 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900952 vndk: {
953 enabled: true,
954 support_system_process: true,
955 },
956 shared_libs: ["libnonvndk"],
957 nocrt: true,
958 }
959
960 cc_library {
961 name: "libnonvndk",
962 vendor_available: true,
963 nocrt: true,
964 }
965 `)
966
967 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
968 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
969 cc_library {
970 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900971 vendor_available: true,
972 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900973 vndk: {
974 enabled: true,
975 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900976 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900977 },
978 shared_libs: ["libnonvndk"],
979 nocrt: true,
980 }
981
982 cc_library {
983 name: "libnonvndk",
984 vendor_available: true,
985 nocrt: true,
986 }
987 `)
988}
989
990func TestDoubleLoadbleDep(t *testing.T) {
991 // okay to link : LLNDK -> double_loadable VNDK
992 testCc(t, `
993 cc_library {
994 name: "libllndk",
995 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -0700996 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +0900997 }
998
999 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001000 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001001 symbol_file: "",
1002 }
1003
1004 cc_library {
1005 name: "libdoubleloadable",
1006 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001007 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001008 vndk: {
1009 enabled: true,
1010 },
1011 double_loadable: true,
1012 }
1013 `)
1014 // okay to link : LLNDK -> VNDK-SP
1015 testCc(t, `
1016 cc_library {
1017 name: "libllndk",
1018 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001019 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001020 }
1021
1022 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001023 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001024 symbol_file: "",
1025 }
1026
1027 cc_library {
1028 name: "libvndksp",
1029 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001030 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001031 vndk: {
1032 enabled: true,
1033 support_system_process: true,
1034 },
1035 }
1036 `)
1037 // okay to link : double_loadable -> double_loadable
1038 testCc(t, `
1039 cc_library {
1040 name: "libdoubleloadable1",
1041 shared_libs: ["libdoubleloadable2"],
1042 vendor_available: true,
1043 double_loadable: true,
1044 }
1045
1046 cc_library {
1047 name: "libdoubleloadable2",
1048 vendor_available: true,
1049 double_loadable: true,
1050 }
1051 `)
1052 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1053 testCc(t, `
1054 cc_library {
1055 name: "libdoubleloadable",
1056 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001057 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001058 vndk: {
1059 enabled: true,
1060 },
1061 double_loadable: true,
1062 shared_libs: ["libnondoubleloadable"],
1063 }
1064
1065 cc_library {
1066 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001067 vendor_available: true,
1068 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001069 vndk: {
1070 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001071 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001072 },
1073 double_loadable: true,
1074 }
1075 `)
1076 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1077 testCc(t, `
1078 cc_library {
1079 name: "libllndk",
1080 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001081 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001082 }
1083
1084 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001085 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001086 symbol_file: "",
1087 }
1088
1089 cc_library {
1090 name: "libcoreonly",
1091 shared_libs: ["libvendoravailable"],
1092 }
1093
1094 // indirect dependency of LLNDK
1095 cc_library {
1096 name: "libvendoravailable",
1097 vendor_available: true,
1098 double_loadable: true,
1099 }
1100 `)
1101}
1102
Inseob Kim5f58ff72020-09-07 19:53:31 +09001103func TestVendorSnapshotCapture(t *testing.T) {
Inseob Kim8471cda2019-11-15 09:59:12 +09001104 bp := `
1105 cc_library {
1106 name: "libvndk",
1107 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001108 product_available: true,
Inseob Kim8471cda2019-11-15 09:59:12 +09001109 vndk: {
1110 enabled: true,
1111 },
1112 nocrt: true,
1113 }
1114
1115 cc_library {
1116 name: "libvendor",
1117 vendor: true,
1118 nocrt: true,
1119 }
1120
1121 cc_library {
1122 name: "libvendor_available",
1123 vendor_available: true,
1124 nocrt: true,
1125 }
1126
1127 cc_library_headers {
1128 name: "libvendor_headers",
1129 vendor_available: true,
1130 nocrt: true,
1131 }
1132
1133 cc_binary {
1134 name: "vendor_bin",
1135 vendor: true,
1136 nocrt: true,
1137 }
1138
1139 cc_binary {
1140 name: "vendor_available_bin",
1141 vendor_available: true,
1142 nocrt: true,
1143 }
Inseob Kim7f283f42020-06-01 21:53:49 +09001144
1145 toolchain_library {
1146 name: "libb",
1147 vendor_available: true,
1148 src: "libb.a",
1149 }
Inseob Kim1042d292020-06-01 23:23:05 +09001150
1151 cc_object {
1152 name: "obj",
1153 vendor_available: true,
1154 }
Colin Cross127bb8b2020-12-16 16:46:01 -08001155
1156 cc_library {
1157 name: "libllndk",
1158 llndk_stubs: "libllndk.llndk",
1159 }
1160
1161 llndk_library {
1162 name: "libllndk.llndk",
1163 symbol_file: "",
1164 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001165`
1166 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1167 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1168 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1169 ctx := testCcWithConfig(t, config)
1170
1171 // Check Vendor snapshot output.
1172
1173 snapshotDir := "vendor-snapshot"
1174 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
Inseob Kim7f283f42020-06-01 21:53:49 +09001175 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1176
1177 var jsonFiles []string
Inseob Kim8471cda2019-11-15 09:59:12 +09001178
1179 for _, arch := range [][]string{
1180 []string{"arm64", "armv8-a"},
1181 []string{"arm", "armv7-a-neon"},
1182 } {
1183 archType := arch[0]
1184 archVariant := arch[1]
1185 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1186
1187 // For shared libraries, only non-VNDK vendor_available modules are captured
1188 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1189 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
Inseob Kim7f283f42020-06-01 21:53:49 +09001190 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1191 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.so", sharedDir, sharedVariant)
1192 jsonFiles = append(jsonFiles,
1193 filepath.Join(sharedDir, "libvendor.so.json"),
1194 filepath.Join(sharedDir, "libvendor_available.so.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001195
Colin Cross127bb8b2020-12-16 16:46:01 -08001196 // LLNDK modules are not captured
1197 checkSnapshotExclude(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", sharedDir, sharedVariant)
1198
Inseob Kim8471cda2019-11-15 09:59:12 +09001199 // For static libraries, all vendor:true and vendor_available modules (including VNDK) are captured.
Inseob Kimc42f2f22020-07-29 20:32:10 +09001200 // Also cfi variants are captured, except for prebuilts like toolchain_library
Inseob Kim8471cda2019-11-15 09:59:12 +09001201 staticVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static", archType, archVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001202 staticCfiVariant := fmt.Sprintf("android_vendor.VER_%s_%s_static_cfi", archType, archVariant)
Inseob Kim8471cda2019-11-15 09:59:12 +09001203 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
Inseob Kim7f283f42020-06-01 21:53:49 +09001204 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1205 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001206 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001207 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001208 checkSnapshot(t, ctx, snapshotSingleton, "libvendor", "libvendor.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001209 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.a", staticDir, staticVariant)
Inseob Kimc42f2f22020-07-29 20:32:10 +09001210 checkSnapshot(t, ctx, snapshotSingleton, "libvendor_available", "libvendor_available.cfi.a", staticDir, staticCfiVariant)
Inseob Kim7f283f42020-06-01 21:53:49 +09001211 jsonFiles = append(jsonFiles,
1212 filepath.Join(staticDir, "libb.a.json"),
1213 filepath.Join(staticDir, "libvndk.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001214 filepath.Join(staticDir, "libvndk.cfi.a.json"),
Inseob Kim7f283f42020-06-01 21:53:49 +09001215 filepath.Join(staticDir, "libvendor.a.json"),
Inseob Kimc42f2f22020-07-29 20:32:10 +09001216 filepath.Join(staticDir, "libvendor.cfi.a.json"),
1217 filepath.Join(staticDir, "libvendor_available.a.json"),
1218 filepath.Join(staticDir, "libvendor_available.cfi.a.json"))
Inseob Kim8471cda2019-11-15 09:59:12 +09001219
Inseob Kim7f283f42020-06-01 21:53:49 +09001220 // For binary executables, all vendor:true and vendor_available modules are captured.
Inseob Kim8471cda2019-11-15 09:59:12 +09001221 if archType == "arm64" {
1222 binaryVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1223 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
Inseob Kim7f283f42020-06-01 21:53:49 +09001224 checkSnapshot(t, ctx, snapshotSingleton, "vendor_bin", "vendor_bin", binaryDir, binaryVariant)
1225 checkSnapshot(t, ctx, snapshotSingleton, "vendor_available_bin", "vendor_available_bin", binaryDir, binaryVariant)
1226 jsonFiles = append(jsonFiles,
1227 filepath.Join(binaryDir, "vendor_bin.json"),
1228 filepath.Join(binaryDir, "vendor_available_bin.json"))
1229 }
1230
1231 // For header libraries, all vendor:true and vendor_available modules are captured.
1232 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1233 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "libvendor_headers.json"))
Inseob Kim1042d292020-06-01 23:23:05 +09001234
1235 // For object modules, all vendor:true and vendor_available modules are captured.
1236 objectVariant := fmt.Sprintf("android_vendor.VER_%s_%s", archType, archVariant)
1237 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1238 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1239 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
Inseob Kim7f283f42020-06-01 21:53:49 +09001240 }
1241
1242 for _, jsonFile := range jsonFiles {
1243 // verify all json files exist
1244 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1245 t.Errorf("%q expected but not found", jsonFile)
Inseob Kim8471cda2019-11-15 09:59:12 +09001246 }
1247 }
Inseob Kime9aec6a2021-01-05 20:03:22 +09001248
1249 // fake snapshot should have all outputs in the normal snapshot.
1250 fakeSnapshotSingleton := ctx.SingletonForTests("vendor-fake-snapshot")
1251 for _, output := range snapshotSingleton.AllOutputs() {
1252 fakeOutput := strings.Replace(output, "/vendor-snapshot/", "/fake/vendor-snapshot/", 1)
1253 if fakeSnapshotSingleton.MaybeOutput(fakeOutput).Rule == nil {
1254 t.Errorf("%q expected but not found", fakeOutput)
1255 }
1256 }
Inseob Kim8471cda2019-11-15 09:59:12 +09001257}
1258
Inseob Kim5f58ff72020-09-07 19:53:31 +09001259func TestVendorSnapshotUse(t *testing.T) {
1260 frameworkBp := `
1261 cc_library {
1262 name: "libvndk",
1263 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001264 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001265 vndk: {
1266 enabled: true,
1267 },
1268 nocrt: true,
1269 compile_multilib: "64",
1270 }
1271
1272 cc_library {
1273 name: "libvendor",
1274 vendor: true,
1275 nocrt: true,
1276 no_libcrt: true,
1277 stl: "none",
1278 system_shared_libs: [],
1279 compile_multilib: "64",
1280 }
1281
1282 cc_binary {
1283 name: "bin",
1284 vendor: true,
1285 nocrt: true,
1286 no_libcrt: true,
1287 stl: "none",
1288 system_shared_libs: [],
1289 compile_multilib: "64",
1290 }
1291`
1292
1293 vndkBp := `
1294 vndk_prebuilt_shared {
1295 name: "libvndk",
1296 version: "BOARD",
1297 target_arch: "arm64",
1298 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001299 product_available: true,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001300 vndk: {
1301 enabled: true,
1302 },
1303 arch: {
1304 arm64: {
1305 srcs: ["libvndk.so"],
Inseob Kim67be7322020-10-19 10:15:28 +09001306 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001307 },
1308 },
1309 }
1310`
1311
1312 vendorProprietaryBp := `
1313 cc_library {
1314 name: "libvendor_without_snapshot",
1315 vendor: true,
1316 nocrt: true,
1317 no_libcrt: true,
1318 stl: "none",
1319 system_shared_libs: [],
1320 compile_multilib: "64",
1321 }
1322
1323 cc_library_shared {
1324 name: "libclient",
1325 vendor: true,
1326 nocrt: true,
1327 no_libcrt: true,
1328 stl: "none",
1329 system_shared_libs: [],
1330 shared_libs: ["libvndk"],
1331 static_libs: ["libvendor", "libvendor_without_snapshot"],
1332 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001333 srcs: ["client.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001334 }
1335
1336 cc_binary {
1337 name: "bin_without_snapshot",
1338 vendor: true,
1339 nocrt: true,
1340 no_libcrt: true,
1341 stl: "none",
1342 system_shared_libs: [],
1343 static_libs: ["libvndk"],
1344 compile_multilib: "64",
Inseob Kim67be7322020-10-19 10:15:28 +09001345 srcs: ["bin.cpp"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001346 }
1347
1348 vendor_snapshot_static {
1349 name: "libvndk",
1350 version: "BOARD",
1351 target_arch: "arm64",
1352 vendor: true,
1353 arch: {
1354 arm64: {
1355 src: "libvndk.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001356 export_include_dirs: ["include/libvndk"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001357 },
1358 },
1359 }
1360
1361 vendor_snapshot_shared {
1362 name: "libvendor",
1363 version: "BOARD",
1364 target_arch: "arm64",
1365 vendor: true,
1366 arch: {
1367 arm64: {
1368 src: "libvendor.so",
Inseob Kim67be7322020-10-19 10:15:28 +09001369 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001370 },
1371 },
1372 }
1373
1374 vendor_snapshot_static {
1375 name: "libvendor",
1376 version: "BOARD",
1377 target_arch: "arm64",
1378 vendor: true,
1379 arch: {
1380 arm64: {
1381 src: "libvendor.a",
Inseob Kim67be7322020-10-19 10:15:28 +09001382 export_include_dirs: ["include/libvendor"],
Inseob Kim5f58ff72020-09-07 19:53:31 +09001383 },
1384 },
1385 }
1386
1387 vendor_snapshot_binary {
1388 name: "bin",
1389 version: "BOARD",
1390 target_arch: "arm64",
1391 vendor: true,
1392 arch: {
1393 arm64: {
1394 src: "bin",
1395 },
1396 },
1397 }
1398`
1399 depsBp := GatherRequiredDepsForTest(android.Android)
1400
1401 mockFS := map[string][]byte{
Inseob Kim67be7322020-10-19 10:15:28 +09001402 "deps/Android.bp": []byte(depsBp),
1403 "framework/Android.bp": []byte(frameworkBp),
1404 "vendor/Android.bp": []byte(vendorProprietaryBp),
1405 "vendor/bin": nil,
1406 "vendor/bin.cpp": nil,
1407 "vendor/client.cpp": nil,
1408 "vendor/include/libvndk/a.h": nil,
1409 "vendor/include/libvendor/b.h": nil,
1410 "vendor/libvndk.a": nil,
1411 "vendor/libvendor.a": nil,
1412 "vendor/libvendor.so": nil,
1413 "vndk/Android.bp": []byte(vndkBp),
1414 "vndk/include/libvndk/a.h": nil,
1415 "vndk/libvndk.so": nil,
Inseob Kim5f58ff72020-09-07 19:53:31 +09001416 }
1417
1418 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1419 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1420 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001421 ctx := CreateTestContext(config)
1422 ctx.Register()
Inseob Kim5f58ff72020-09-07 19:53:31 +09001423
1424 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "vendor/Android.bp", "vndk/Android.bp"})
1425 android.FailIfErrored(t, errs)
1426 _, errs = ctx.PrepareBuildActions(config)
1427 android.FailIfErrored(t, errs)
1428
1429 sharedVariant := "android_vendor.BOARD_arm64_armv8-a_shared"
1430 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1431 binaryVariant := "android_vendor.BOARD_arm64_armv8-a"
1432
1433 // libclient uses libvndk.vndk.BOARD.arm64, libvendor.vendor_static.BOARD.arm64, libvendor_without_snapshot
Inseob Kim67be7322020-10-19 10:15:28 +09001434 libclientCcFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("cc").Args["cFlags"]
1435 for _, includeFlags := range []string{
1436 "-Ivndk/include/libvndk", // libvndk
1437 "-Ivendor/include/libvendor", // libvendor
1438 } {
1439 if !strings.Contains(libclientCcFlags, includeFlags) {
1440 t.Errorf("flags for libclient must contain %#v, but was %#v.",
1441 includeFlags, libclientCcFlags)
1442 }
1443 }
Inseob Kim5f58ff72020-09-07 19:53:31 +09001444
Inseob Kim67be7322020-10-19 10:15:28 +09001445 libclientLdFlags := ctx.ModuleForTests("libclient", sharedVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001446 for _, input := range [][]string{
1447 []string{sharedVariant, "libvndk.vndk.BOARD.arm64"},
1448 []string{staticVariant, "libvendor.vendor_static.BOARD.arm64"},
1449 []string{staticVariant, "libvendor_without_snapshot"},
1450 } {
1451 outputPaths := getOutputPaths(ctx, input[0] /* variant */, []string{input[1]} /* module name */)
Inseob Kim67be7322020-10-19 10:15:28 +09001452 if !strings.Contains(libclientLdFlags, outputPaths[0].String()) {
1453 t.Errorf("libflags for libclient must contain %#v, but was %#v", outputPaths[0], libclientLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001454 }
1455 }
1456
1457 // bin_without_snapshot uses libvndk.vendor_static.BOARD.arm64
Inseob Kim67be7322020-10-19 10:15:28 +09001458 binWithoutSnapshotCcFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("cc").Args["cFlags"]
1459 if !strings.Contains(binWithoutSnapshotCcFlags, "-Ivendor/include/libvndk") {
1460 t.Errorf("flags for bin_without_snapshot must contain %#v, but was %#v.",
1461 "-Ivendor/include/libvndk", binWithoutSnapshotCcFlags)
1462 }
1463
1464 binWithoutSnapshotLdFlags := ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Rule("ld").Args["libFlags"]
Inseob Kim5f58ff72020-09-07 19:53:31 +09001465 libVndkStaticOutputPaths := getOutputPaths(ctx, staticVariant, []string{"libvndk.vendor_static.BOARD.arm64"})
Inseob Kim67be7322020-10-19 10:15:28 +09001466 if !strings.Contains(binWithoutSnapshotLdFlags, libVndkStaticOutputPaths[0].String()) {
Inseob Kim5f58ff72020-09-07 19:53:31 +09001467 t.Errorf("libflags for bin_without_snapshot must contain %#v, but was %#v",
Inseob Kim67be7322020-10-19 10:15:28 +09001468 libVndkStaticOutputPaths[0], binWithoutSnapshotLdFlags)
Inseob Kim5f58ff72020-09-07 19:53:31 +09001469 }
1470
1471 // libvendor.so is installed by libvendor.vendor_shared.BOARD.arm64
1472 ctx.ModuleForTests("libvendor.vendor_shared.BOARD.arm64", sharedVariant).Output("libvendor.so")
1473
1474 // libvendor_without_snapshot.so is installed by libvendor_without_snapshot
1475 ctx.ModuleForTests("libvendor_without_snapshot", sharedVariant).Output("libvendor_without_snapshot.so")
1476
1477 // bin is installed by bin.vendor_binary.BOARD.arm64
1478 ctx.ModuleForTests("bin.vendor_binary.BOARD.arm64", binaryVariant).Output("bin")
1479
1480 // bin_without_snapshot is installed by bin_without_snapshot
1481 ctx.ModuleForTests("bin_without_snapshot", binaryVariant).Output("bin_without_snapshot")
1482
1483 // libvendor and bin don't have vendor.BOARD variant
1484 libvendorVariants := ctx.ModuleVariantsForTests("libvendor")
1485 if inList(sharedVariant, libvendorVariants) {
1486 t.Errorf("libvendor must not have variant %#v, but it does", sharedVariant)
1487 }
1488
1489 binVariants := ctx.ModuleVariantsForTests("bin")
1490 if inList(binaryVariant, binVariants) {
1491 t.Errorf("bin must not have variant %#v, but it does", sharedVariant)
1492 }
1493}
1494
Inseob Kimc42f2f22020-07-29 20:32:10 +09001495func TestVendorSnapshotSanitizer(t *testing.T) {
1496 bp := `
1497 vendor_snapshot_static {
1498 name: "libsnapshot",
1499 vendor: true,
1500 target_arch: "arm64",
1501 version: "BOARD",
1502 arch: {
1503 arm64: {
1504 src: "libsnapshot.a",
1505 cfi: {
1506 src: "libsnapshot.cfi.a",
1507 }
1508 },
1509 },
1510 }
1511`
1512 config := TestConfig(buildDir, android.Android, nil, bp, nil)
1513 config.TestProductVariables.DeviceVndkVersion = StringPtr("BOARD")
1514 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1515 ctx := testCcWithConfig(t, config)
1516
1517 // Check non-cfi and cfi variant.
1518 staticVariant := "android_vendor.BOARD_arm64_armv8-a_static"
1519 staticCfiVariant := "android_vendor.BOARD_arm64_armv8-a_static_cfi"
1520
1521 staticModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticVariant).Module().(*Module)
1522 assertString(t, staticModule.outputFile.Path().Base(), "libsnapshot.a")
1523
1524 staticCfiModule := ctx.ModuleForTests("libsnapshot.vendor_static.BOARD.arm64", staticCfiVariant).Module().(*Module)
1525 assertString(t, staticCfiModule.outputFile.Path().Base(), "libsnapshot.cfi.a")
1526}
1527
Bill Peckham945441c2020-08-31 16:07:58 -07001528func assertExcludeFromVendorSnapshotIs(t *testing.T, c *Module, expected bool) {
1529 t.Helper()
1530 if c.ExcludeFromVendorSnapshot() != expected {
1531 t.Errorf("expected %q ExcludeFromVendorSnapshot to be %t", c.String(), expected)
1532 }
1533}
1534
Jose Galmes6f843bc2020-12-11 13:36:29 -08001535func assertExcludeFromRecoverySnapshotIs(t *testing.T, c *Module, expected bool) {
1536 t.Helper()
1537 if c.ExcludeFromRecoverySnapshot() != expected {
1538 t.Errorf("expected %q ExcludeFromRecoverySnapshot to be %t", c.String(), expected)
1539 }
1540}
1541
Bill Peckham945441c2020-08-31 16:07:58 -07001542func TestVendorSnapshotExclude(t *testing.T) {
1543
1544 // This test verifies that the exclude_from_vendor_snapshot property
1545 // makes its way from the Android.bp source file into the module data
1546 // structure. It also verifies that modules are correctly included or
1547 // excluded in the vendor snapshot based on their path (framework or
1548 // vendor) and the exclude_from_vendor_snapshot property.
1549
1550 frameworkBp := `
1551 cc_library_shared {
1552 name: "libinclude",
1553 srcs: ["src/include.cpp"],
1554 vendor_available: true,
1555 }
1556 cc_library_shared {
1557 name: "libexclude",
1558 srcs: ["src/exclude.cpp"],
1559 vendor: true,
1560 exclude_from_vendor_snapshot: true,
1561 }
1562 `
1563
1564 vendorProprietaryBp := `
1565 cc_library_shared {
1566 name: "libvendor",
1567 srcs: ["vendor.cpp"],
1568 vendor: true,
1569 }
1570 `
1571
1572 depsBp := GatherRequiredDepsForTest(android.Android)
1573
1574 mockFS := map[string][]byte{
1575 "deps/Android.bp": []byte(depsBp),
1576 "framework/Android.bp": []byte(frameworkBp),
1577 "framework/include.cpp": nil,
1578 "framework/exclude.cpp": nil,
1579 "device/Android.bp": []byte(vendorProprietaryBp),
1580 "device/vendor.cpp": nil,
1581 }
1582
1583 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1584 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1585 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001586 ctx := CreateTestContext(config)
1587 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001588
1589 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1590 android.FailIfErrored(t, errs)
1591 _, errs = ctx.PrepareBuildActions(config)
1592 android.FailIfErrored(t, errs)
1593
1594 // Test an include and exclude framework module.
1595 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1596 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libinclude", vendorVariant).Module().(*Module), false)
1597 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libexclude", vendorVariant).Module().(*Module), true)
1598
1599 // A vendor module is excluded, but by its path, not the
1600 // exclude_from_vendor_snapshot property.
1601 assertExcludeFromVendorSnapshotIs(t, ctx.ModuleForTests("libvendor", vendorVariant).Module().(*Module), false)
1602
1603 // Verify the content of the vendor snapshot.
1604
1605 snapshotDir := "vendor-snapshot"
1606 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1607 snapshotSingleton := ctx.SingletonForTests("vendor-snapshot")
1608
1609 var includeJsonFiles []string
1610 var excludeJsonFiles []string
1611
1612 for _, arch := range [][]string{
1613 []string{"arm64", "armv8-a"},
1614 []string{"arm", "armv7-a-neon"},
1615 } {
1616 archType := arch[0]
1617 archVariant := arch[1]
1618 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1619
1620 sharedVariant := fmt.Sprintf("android_vendor.VER_%s_%s_shared", archType, archVariant)
1621 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1622
1623 // Included modules
1624 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1625 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1626
1627 // Excluded modules
1628 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1629 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1630 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1631 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1632 }
1633
1634 // Verify that each json file for an included module has a rule.
1635 for _, jsonFile := range includeJsonFiles {
1636 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1637 t.Errorf("include json file %q not found", jsonFile)
1638 }
1639 }
1640
1641 // Verify that each json file for an excluded module has no rule.
1642 for _, jsonFile := range excludeJsonFiles {
1643 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1644 t.Errorf("exclude json file %q found", jsonFile)
1645 }
1646 }
1647}
1648
1649func TestVendorSnapshotExcludeInVendorProprietaryPathErrors(t *testing.T) {
1650
1651 // This test verifies that using the exclude_from_vendor_snapshot
1652 // property on a module in a vendor proprietary path generates an
1653 // error. These modules are already excluded, so we prohibit using the
1654 // property in this way, which could add to confusion.
1655
1656 vendorProprietaryBp := `
1657 cc_library_shared {
1658 name: "libvendor",
1659 srcs: ["vendor.cpp"],
1660 vendor: true,
1661 exclude_from_vendor_snapshot: true,
1662 }
1663 `
1664
1665 depsBp := GatherRequiredDepsForTest(android.Android)
1666
1667 mockFS := map[string][]byte{
1668 "deps/Android.bp": []byte(depsBp),
1669 "device/Android.bp": []byte(vendorProprietaryBp),
1670 "device/vendor.cpp": nil,
1671 }
1672
1673 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1674 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1675 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001676 ctx := CreateTestContext(config)
1677 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001678
1679 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "device/Android.bp"})
1680 android.FailIfErrored(t, errs)
1681
1682 _, errs = ctx.PrepareBuildActions(config)
1683 android.CheckErrorsAgainstExpectations(t, errs, []string{
1684 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1685 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Jose Galmesf7294582020-11-13 12:07:36 -08001686 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1687 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Inseob Kime9aec6a2021-01-05 20:03:22 +09001688 `module "libvendor\{.+,image:vendor.+,arch:arm64_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
1689 `module "libvendor\{.+,image:vendor.+,arch:arm_.+\}" in vendor proprietary path "device" may not use "exclude_from_vendor_snapshot: true"`,
Bill Peckham945441c2020-08-31 16:07:58 -07001690 })
1691}
1692
1693func TestVendorSnapshotExcludeWithVendorAvailable(t *testing.T) {
1694
1695 // This test verifies that using the exclude_from_vendor_snapshot
1696 // property on a module that is vendor available generates an error. A
1697 // vendor available module must be captured in the vendor snapshot and
1698 // must not built from source when building the vendor image against
1699 // the vendor snapshot.
1700
1701 frameworkBp := `
1702 cc_library_shared {
1703 name: "libinclude",
1704 srcs: ["src/include.cpp"],
1705 vendor_available: true,
1706 exclude_from_vendor_snapshot: true,
1707 }
1708 `
1709
1710 depsBp := GatherRequiredDepsForTest(android.Android)
1711
1712 mockFS := map[string][]byte{
1713 "deps/Android.bp": []byte(depsBp),
1714 "framework/Android.bp": []byte(frameworkBp),
1715 "framework/include.cpp": nil,
1716 }
1717
1718 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1719 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1720 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Colin Crossae8600b2020-10-29 17:09:13 -07001721 ctx := CreateTestContext(config)
1722 ctx.Register()
Bill Peckham945441c2020-08-31 16:07:58 -07001723
1724 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp"})
1725 android.FailIfErrored(t, errs)
1726
1727 _, errs = ctx.PrepareBuildActions(config)
1728 android.CheckErrorsAgainstExpectations(t, errs, []string{
1729 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1730 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1731 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1732 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
Inseob Kime9aec6a2021-01-05 20:03:22 +09001733 `module "libinclude\{.+,image:,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1734 `module "libinclude\{.+,image:,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1735 `module "libinclude\{.+,image:vendor.+,arch:arm64_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
1736 `module "libinclude\{.+,image:vendor.+,arch:arm_.+\}" may not use both "vendor_available: true" and "exclude_from_vendor_snapshot: true"`,
Bill Peckham945441c2020-08-31 16:07:58 -07001737 })
1738}
1739
Jose Galmesf7294582020-11-13 12:07:36 -08001740func TestRecoverySnapshotCapture(t *testing.T) {
1741 bp := `
1742 cc_library {
1743 name: "libvndk",
1744 vendor_available: true,
1745 recovery_available: true,
1746 product_available: true,
1747 vndk: {
1748 enabled: true,
1749 },
1750 nocrt: true,
1751 }
1752
1753 cc_library {
1754 name: "librecovery",
1755 recovery: true,
1756 nocrt: true,
1757 }
1758
1759 cc_library {
1760 name: "librecovery_available",
1761 recovery_available: true,
1762 nocrt: true,
1763 }
1764
1765 cc_library_headers {
1766 name: "librecovery_headers",
1767 recovery_available: true,
1768 nocrt: true,
1769 }
1770
1771 cc_binary {
1772 name: "recovery_bin",
1773 recovery: true,
1774 nocrt: true,
1775 }
1776
1777 cc_binary {
1778 name: "recovery_available_bin",
1779 recovery_available: true,
1780 nocrt: true,
1781 }
1782
1783 toolchain_library {
1784 name: "libb",
1785 recovery_available: true,
1786 src: "libb.a",
1787 }
1788
1789 cc_object {
1790 name: "obj",
1791 recovery_available: true,
1792 }
1793`
1794 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jose Galmes6f843bc2020-12-11 13:36:29 -08001795 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
Jose Galmesf7294582020-11-13 12:07:36 -08001796 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1797 ctx := testCcWithConfig(t, config)
1798
1799 // Check Recovery snapshot output.
1800
1801 snapshotDir := "recovery-snapshot"
1802 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1803 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1804
1805 var jsonFiles []string
1806
1807 for _, arch := range [][]string{
1808 []string{"arm64", "armv8-a"},
1809 } {
1810 archType := arch[0]
1811 archVariant := arch[1]
1812 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1813
1814 // For shared libraries, only recovery_available modules are captured.
1815 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1816 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1817 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", sharedDir, sharedVariant)
1818 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.so", sharedDir, sharedVariant)
1819 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.so", sharedDir, sharedVariant)
1820 jsonFiles = append(jsonFiles,
1821 filepath.Join(sharedDir, "libvndk.so.json"),
1822 filepath.Join(sharedDir, "librecovery.so.json"),
1823 filepath.Join(sharedDir, "librecovery_available.so.json"))
1824
1825 // For static libraries, all recovery:true and recovery_available modules are captured.
1826 staticVariant := fmt.Sprintf("android_recovery_%s_%s_static", archType, archVariant)
1827 staticDir := filepath.Join(snapshotVariantPath, archDir, "static")
1828 checkSnapshot(t, ctx, snapshotSingleton, "libb", "libb.a", staticDir, staticVariant)
1829 checkSnapshot(t, ctx, snapshotSingleton, "librecovery", "librecovery.a", staticDir, staticVariant)
1830 checkSnapshot(t, ctx, snapshotSingleton, "librecovery_available", "librecovery_available.a", staticDir, staticVariant)
1831 jsonFiles = append(jsonFiles,
1832 filepath.Join(staticDir, "libb.a.json"),
1833 filepath.Join(staticDir, "librecovery.a.json"),
1834 filepath.Join(staticDir, "librecovery_available.a.json"))
1835
1836 // For binary executables, all recovery:true and recovery_available modules are captured.
1837 if archType == "arm64" {
1838 binaryVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1839 binaryDir := filepath.Join(snapshotVariantPath, archDir, "binary")
1840 checkSnapshot(t, ctx, snapshotSingleton, "recovery_bin", "recovery_bin", binaryDir, binaryVariant)
1841 checkSnapshot(t, ctx, snapshotSingleton, "recovery_available_bin", "recovery_available_bin", binaryDir, binaryVariant)
1842 jsonFiles = append(jsonFiles,
1843 filepath.Join(binaryDir, "recovery_bin.json"),
1844 filepath.Join(binaryDir, "recovery_available_bin.json"))
1845 }
1846
1847 // For header libraries, all vendor:true and vendor_available modules are captured.
1848 headerDir := filepath.Join(snapshotVariantPath, archDir, "header")
1849 jsonFiles = append(jsonFiles, filepath.Join(headerDir, "librecovery_headers.json"))
1850
1851 // For object modules, all vendor:true and vendor_available modules are captured.
1852 objectVariant := fmt.Sprintf("android_recovery_%s_%s", archType, archVariant)
1853 objectDir := filepath.Join(snapshotVariantPath, archDir, "object")
1854 checkSnapshot(t, ctx, snapshotSingleton, "obj", "obj.o", objectDir, objectVariant)
1855 jsonFiles = append(jsonFiles, filepath.Join(objectDir, "obj.o.json"))
1856 }
1857
1858 for _, jsonFile := range jsonFiles {
1859 // verify all json files exist
1860 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1861 t.Errorf("%q expected but not found", jsonFile)
1862 }
1863 }
1864}
1865
Jose Galmes6f843bc2020-12-11 13:36:29 -08001866func TestRecoverySnapshotExclude(t *testing.T) {
1867 // This test verifies that the exclude_from_recovery_snapshot property
1868 // makes its way from the Android.bp source file into the module data
1869 // structure. It also verifies that modules are correctly included or
1870 // excluded in the recovery snapshot based on their path (framework or
1871 // vendor) and the exclude_from_recovery_snapshot property.
1872
1873 frameworkBp := `
1874 cc_library_shared {
1875 name: "libinclude",
1876 srcs: ["src/include.cpp"],
1877 recovery_available: true,
1878 }
1879 cc_library_shared {
1880 name: "libexclude",
1881 srcs: ["src/exclude.cpp"],
1882 recovery: true,
1883 exclude_from_recovery_snapshot: true,
1884 }
1885 `
1886
1887 vendorProprietaryBp := `
1888 cc_library_shared {
1889 name: "libvendor",
1890 srcs: ["vendor.cpp"],
1891 recovery: true,
1892 }
1893 `
1894
1895 depsBp := GatherRequiredDepsForTest(android.Android)
1896
1897 mockFS := map[string][]byte{
1898 "deps/Android.bp": []byte(depsBp),
1899 "framework/Android.bp": []byte(frameworkBp),
1900 "framework/include.cpp": nil,
1901 "framework/exclude.cpp": nil,
1902 "device/Android.bp": []byte(vendorProprietaryBp),
1903 "device/vendor.cpp": nil,
1904 }
1905
1906 config := TestConfig(buildDir, android.Android, nil, "", mockFS)
1907 config.TestProductVariables.RecoverySnapshotVersion = StringPtr("current")
1908 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1909 ctx := CreateTestContext(config)
1910 ctx.Register()
1911
1912 _, errs := ctx.ParseFileList(".", []string{"deps/Android.bp", "framework/Android.bp", "device/Android.bp"})
1913 android.FailIfErrored(t, errs)
1914 _, errs = ctx.PrepareBuildActions(config)
1915 android.FailIfErrored(t, errs)
1916
1917 // Test an include and exclude framework module.
1918 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", coreVariant).Module().(*Module), false)
1919 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libinclude", recoveryVariant).Module().(*Module), false)
1920 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libexclude", recoveryVariant).Module().(*Module), true)
1921
1922 // A vendor module is excluded, but by its path, not the
1923 // exclude_from_recovery_snapshot property.
1924 assertExcludeFromRecoverySnapshotIs(t, ctx.ModuleForTests("libvendor", recoveryVariant).Module().(*Module), false)
1925
1926 // Verify the content of the recovery snapshot.
1927
1928 snapshotDir := "recovery-snapshot"
1929 snapshotVariantPath := filepath.Join(buildDir, snapshotDir, "arm64")
1930 snapshotSingleton := ctx.SingletonForTests("recovery-snapshot")
1931
1932 var includeJsonFiles []string
1933 var excludeJsonFiles []string
1934
1935 for _, arch := range [][]string{
1936 []string{"arm64", "armv8-a"},
1937 } {
1938 archType := arch[0]
1939 archVariant := arch[1]
1940 archDir := fmt.Sprintf("arch-%s-%s", archType, archVariant)
1941
1942 sharedVariant := fmt.Sprintf("android_recovery_%s_%s_shared", archType, archVariant)
1943 sharedDir := filepath.Join(snapshotVariantPath, archDir, "shared")
1944
1945 // Included modules
1946 checkSnapshot(t, ctx, snapshotSingleton, "libinclude", "libinclude.so", sharedDir, sharedVariant)
1947 includeJsonFiles = append(includeJsonFiles, filepath.Join(sharedDir, "libinclude.so.json"))
1948
1949 // Excluded modules
1950 checkSnapshotExclude(t, ctx, snapshotSingleton, "libexclude", "libexclude.so", sharedDir, sharedVariant)
1951 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libexclude.so.json"))
1952 checkSnapshotExclude(t, ctx, snapshotSingleton, "libvendor", "libvendor.so", sharedDir, sharedVariant)
1953 excludeJsonFiles = append(excludeJsonFiles, filepath.Join(sharedDir, "libvendor.so.json"))
1954 }
1955
1956 // Verify that each json file for an included module has a rule.
1957 for _, jsonFile := range includeJsonFiles {
1958 if snapshotSingleton.MaybeOutput(jsonFile).Rule == nil {
1959 t.Errorf("include json file %q not found", jsonFile)
1960 }
1961 }
1962
1963 // Verify that each json file for an excluded module has no rule.
1964 for _, jsonFile := range excludeJsonFiles {
1965 if snapshotSingleton.MaybeOutput(jsonFile).Rule != nil {
1966 t.Errorf("exclude json file %q found", jsonFile)
1967 }
1968 }
1969}
1970
Jooyung Hana70f0672019-01-18 15:20:43 +09001971func TestDoubleLoadableDepError(t *testing.T) {
1972 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1973 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1974 cc_library {
1975 name: "libllndk",
1976 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001977 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001978 }
1979
1980 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001981 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001982 symbol_file: "",
1983 }
1984
1985 cc_library {
1986 name: "libnondoubleloadable",
1987 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001988 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001989 vndk: {
1990 enabled: true,
1991 },
1992 }
1993 `)
1994
1995 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1996 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1997 cc_library {
1998 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001999 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002000 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07002001 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002002 }
2003
2004 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002005 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002006 symbol_file: "",
2007 }
2008
2009 cc_library {
2010 name: "libnondoubleloadable",
2011 vendor_available: true,
2012 }
2013 `)
2014
2015 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable vendor_available lib.
2016 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2017 cc_library {
2018 name: "libdoubleloadable",
2019 vendor_available: true,
2020 double_loadable: true,
2021 shared_libs: ["libnondoubleloadable"],
2022 }
2023
2024 cc_library {
2025 name: "libnondoubleloadable",
2026 vendor_available: true,
2027 }
2028 `)
2029
2030 // Check whether an error is emitted when a double_loadable lib depends on a non-double_loadable VNDK lib.
2031 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2032 cc_library {
2033 name: "libdoubleloadable",
2034 vendor_available: true,
2035 double_loadable: true,
2036 shared_libs: ["libnondoubleloadable"],
2037 }
2038
2039 cc_library {
2040 name: "libnondoubleloadable",
2041 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002042 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002043 vndk: {
2044 enabled: true,
2045 },
2046 }
2047 `)
2048
2049 // Check whether an error is emitted when a double_loadable VNDK depends on a non-double_loadable VNDK private lib.
2050 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2051 cc_library {
2052 name: "libdoubleloadable",
2053 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002054 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002055 vndk: {
2056 enabled: true,
2057 },
2058 double_loadable: true,
2059 shared_libs: ["libnondoubleloadable"],
2060 }
2061
2062 cc_library {
2063 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09002064 vendor_available: true,
2065 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002066 vndk: {
2067 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002068 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09002069 },
2070 }
2071 `)
2072
2073 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
2074 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
2075 cc_library {
2076 name: "libllndk",
2077 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07002078 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002079 }
2080
2081 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002082 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09002083 symbol_file: "",
2084 }
2085
2086 cc_library {
2087 name: "libcoreonly",
2088 shared_libs: ["libvendoravailable"],
2089 }
2090
2091 // indirect dependency of LLNDK
2092 cc_library {
2093 name: "libvendoravailable",
2094 vendor_available: true,
2095 }
2096 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08002097}
2098
Jooyung Han479ca172020-10-19 18:51:07 +09002099func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
2100 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
2101 cc_library {
2102 name: "libvndksp",
2103 shared_libs: ["libanothervndksp"],
2104 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002105 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09002106 vndk: {
2107 enabled: true,
2108 support_system_process: true,
2109 }
2110 }
2111
2112 cc_library {
2113 name: "libllndk",
2114 shared_libs: ["libanothervndksp"],
2115 }
2116
2117 llndk_library {
2118 name: "libllndk",
2119 symbol_file: "",
2120 }
2121
2122 cc_library {
2123 name: "libanothervndksp",
2124 vendor_available: true,
2125 }
2126 `)
2127}
2128
Logan Chienf3511742017-10-31 18:04:35 +08002129func TestVndkExt(t *testing.T) {
2130 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002131 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08002132 cc_library {
2133 name: "libvndk",
2134 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002135 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002136 vndk: {
2137 enabled: true,
2138 },
2139 nocrt: true,
2140 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002141 cc_library {
2142 name: "libvndk2",
2143 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002144 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09002145 vndk: {
2146 enabled: true,
2147 },
2148 target: {
2149 vendor: {
2150 suffix: "-suffix",
2151 },
Justin Yun63e9ec72020-10-29 16:49:43 +09002152 product: {
2153 suffix: "-suffix",
2154 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09002155 },
2156 nocrt: true,
2157 }
Logan Chienf3511742017-10-31 18:04:35 +08002158
2159 cc_library {
2160 name: "libvndk_ext",
2161 vendor: true,
2162 vndk: {
2163 enabled: true,
2164 extends: "libvndk",
2165 },
2166 nocrt: true,
2167 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002168
2169 cc_library {
2170 name: "libvndk2_ext",
2171 vendor: true,
2172 vndk: {
2173 enabled: true,
2174 extends: "libvndk2",
2175 },
2176 nocrt: true,
2177 }
Logan Chienf3511742017-10-31 18:04:35 +08002178
Justin Yun0ecf0b22020-02-28 15:07:59 +09002179 cc_library {
2180 name: "libvndk_ext_product",
2181 product_specific: true,
2182 vndk: {
2183 enabled: true,
2184 extends: "libvndk",
2185 },
2186 nocrt: true,
2187 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09002188
Justin Yun0ecf0b22020-02-28 15:07:59 +09002189 cc_library {
2190 name: "libvndk2_ext_product",
2191 product_specific: true,
2192 vndk: {
2193 enabled: true,
2194 extends: "libvndk2",
2195 },
2196 nocrt: true,
2197 }
2198 `
2199 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2200 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2201 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2202 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2203
2204 ctx := testCcWithConfig(t, config)
2205
2206 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
2207 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
2208
2209 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
2210 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
2211
2212 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
2213 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08002214}
2215
Logan Chiend3c59a22018-03-29 14:08:15 +08002216func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08002217 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
2218 ctx := testCcNoVndk(t, `
2219 cc_library {
2220 name: "libvndk",
2221 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002222 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002223 vndk: {
2224 enabled: true,
2225 },
2226 nocrt: true,
2227 }
2228
2229 cc_library {
2230 name: "libvndk_ext",
2231 vendor: true,
2232 vndk: {
2233 enabled: true,
2234 extends: "libvndk",
2235 },
2236 nocrt: true,
2237 }
2238 `)
2239
2240 // Ensures that the core variant of "libvndk_ext" can be found.
2241 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
2242 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2243 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
2244 }
2245}
2246
Justin Yun0ecf0b22020-02-28 15:07:59 +09002247func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
2248 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09002249 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002250 cc_library {
2251 name: "libvndk",
2252 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002253 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002254 vndk: {
2255 enabled: true,
2256 },
2257 nocrt: true,
2258 }
2259
2260 cc_library {
2261 name: "libvndk_ext_product",
2262 product_specific: true,
2263 vndk: {
2264 enabled: true,
2265 extends: "libvndk",
2266 },
2267 nocrt: true,
2268 }
2269 `)
2270
2271 // Ensures that the core variant of "libvndk_ext_product" can be found.
2272 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
2273 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
2274 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
2275 }
2276}
2277
Logan Chienf3511742017-10-31 18:04:35 +08002278func TestVndkExtError(t *testing.T) {
2279 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09002280 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08002281 cc_library {
2282 name: "libvndk",
2283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002284 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002285 vndk: {
2286 enabled: true,
2287 },
2288 nocrt: true,
2289 }
2290
2291 cc_library {
2292 name: "libvndk_ext",
2293 vndk: {
2294 enabled: true,
2295 extends: "libvndk",
2296 },
2297 nocrt: true,
2298 }
2299 `)
2300
2301 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2302 cc_library {
2303 name: "libvndk",
2304 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002305 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002306 vndk: {
2307 enabled: true,
2308 },
2309 nocrt: true,
2310 }
2311
2312 cc_library {
2313 name: "libvndk_ext",
2314 vendor: true,
2315 vndk: {
2316 enabled: true,
2317 },
2318 nocrt: true,
2319 }
2320 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002321
2322 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
2323 cc_library {
2324 name: "libvndk",
2325 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002326 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002327 vndk: {
2328 enabled: true,
2329 },
2330 nocrt: true,
2331 }
2332
2333 cc_library {
2334 name: "libvndk_ext_product",
2335 product_specific: true,
2336 vndk: {
2337 enabled: true,
2338 },
2339 nocrt: true,
2340 }
2341 `)
2342
2343 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
2344 cc_library {
2345 name: "libvndk",
2346 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002347 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002348 vndk: {
2349 enabled: true,
2350 },
2351 nocrt: true,
2352 }
2353
2354 cc_library {
2355 name: "libvndk_ext_product",
2356 product_specific: true,
2357 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002358 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002359 vndk: {
2360 enabled: true,
2361 extends: "libvndk",
2362 },
2363 nocrt: true,
2364 }
2365 `)
Logan Chienf3511742017-10-31 18:04:35 +08002366}
2367
2368func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
2369 // This test ensures an error is emitted for inconsistent support_system_process.
2370 testCcError(t, "module \".*\" with mismatched support_system_process", `
2371 cc_library {
2372 name: "libvndk",
2373 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002374 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002375 vndk: {
2376 enabled: true,
2377 },
2378 nocrt: true,
2379 }
2380
2381 cc_library {
2382 name: "libvndk_sp_ext",
2383 vendor: true,
2384 vndk: {
2385 enabled: true,
2386 extends: "libvndk",
2387 support_system_process: true,
2388 },
2389 nocrt: true,
2390 }
2391 `)
2392
2393 testCcError(t, "module \".*\" with mismatched support_system_process", `
2394 cc_library {
2395 name: "libvndk_sp",
2396 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002397 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002398 vndk: {
2399 enabled: true,
2400 support_system_process: true,
2401 },
2402 nocrt: true,
2403 }
2404
2405 cc_library {
2406 name: "libvndk_ext",
2407 vendor: true,
2408 vndk: {
2409 enabled: true,
2410 extends: "libvndk_sp",
2411 },
2412 nocrt: true,
2413 }
2414 `)
2415}
2416
2417func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08002418 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09002419 // with `private: true`.
2420 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08002421 cc_library {
2422 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002423 vendor_available: true,
2424 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002425 vndk: {
2426 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002427 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08002428 },
2429 nocrt: true,
2430 }
2431
2432 cc_library {
2433 name: "libvndk_ext",
2434 vendor: true,
2435 vndk: {
2436 enabled: true,
2437 extends: "libvndk",
2438 },
2439 nocrt: true,
2440 }
2441 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09002442
Justin Yunfd9e8042020-12-23 18:23:14 +09002443 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09002444 cc_library {
2445 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09002446 vendor_available: true,
2447 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002448 vndk: {
2449 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002450 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002451 },
2452 nocrt: true,
2453 }
2454
2455 cc_library {
2456 name: "libvndk_ext_product",
2457 product_specific: true,
2458 vndk: {
2459 enabled: true,
2460 extends: "libvndk",
2461 },
2462 nocrt: true,
2463 }
2464 `)
Logan Chienf3511742017-10-31 18:04:35 +08002465}
2466
Logan Chiend3c59a22018-03-29 14:08:15 +08002467func TestVendorModuleUseVndkExt(t *testing.T) {
2468 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002469 testCc(t, `
2470 cc_library {
2471 name: "libvndk",
2472 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002473 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002474 vndk: {
2475 enabled: true,
2476 },
2477 nocrt: true,
2478 }
2479
2480 cc_library {
2481 name: "libvndk_ext",
2482 vendor: true,
2483 vndk: {
2484 enabled: true,
2485 extends: "libvndk",
2486 },
2487 nocrt: true,
2488 }
2489
2490 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08002491 name: "libvndk_sp",
2492 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002493 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002494 vndk: {
2495 enabled: true,
2496 support_system_process: true,
2497 },
2498 nocrt: true,
2499 }
2500
2501 cc_library {
2502 name: "libvndk_sp_ext",
2503 vendor: true,
2504 vndk: {
2505 enabled: true,
2506 extends: "libvndk_sp",
2507 support_system_process: true,
2508 },
2509 nocrt: true,
2510 }
2511
2512 cc_library {
2513 name: "libvendor",
2514 vendor: true,
2515 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
2516 nocrt: true,
2517 }
2518 `)
2519}
2520
Logan Chiend3c59a22018-03-29 14:08:15 +08002521func TestVndkExtUseVendorLib(t *testing.T) {
2522 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08002523 testCc(t, `
2524 cc_library {
2525 name: "libvndk",
2526 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002527 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002528 vndk: {
2529 enabled: true,
2530 },
2531 nocrt: true,
2532 }
2533
2534 cc_library {
2535 name: "libvndk_ext",
2536 vendor: true,
2537 vndk: {
2538 enabled: true,
2539 extends: "libvndk",
2540 },
2541 shared_libs: ["libvendor"],
2542 nocrt: true,
2543 }
2544
2545 cc_library {
2546 name: "libvendor",
2547 vendor: true,
2548 nocrt: true,
2549 }
2550 `)
Logan Chienf3511742017-10-31 18:04:35 +08002551
Logan Chiend3c59a22018-03-29 14:08:15 +08002552 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
2553 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08002554 cc_library {
2555 name: "libvndk_sp",
2556 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002557 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002558 vndk: {
2559 enabled: true,
2560 support_system_process: true,
2561 },
2562 nocrt: true,
2563 }
2564
2565 cc_library {
2566 name: "libvndk_sp_ext",
2567 vendor: true,
2568 vndk: {
2569 enabled: true,
2570 extends: "libvndk_sp",
2571 support_system_process: true,
2572 },
2573 shared_libs: ["libvendor"], // Cause an error
2574 nocrt: true,
2575 }
2576
2577 cc_library {
2578 name: "libvendor",
2579 vendor: true,
2580 nocrt: true,
2581 }
2582 `)
2583}
2584
Justin Yun0ecf0b22020-02-28 15:07:59 +09002585func TestProductVndkExtDependency(t *testing.T) {
2586 bp := `
2587 cc_library {
2588 name: "libvndk",
2589 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002590 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002591 vndk: {
2592 enabled: true,
2593 },
2594 nocrt: true,
2595 }
2596
2597 cc_library {
2598 name: "libvndk_ext_product",
2599 product_specific: true,
2600 vndk: {
2601 enabled: true,
2602 extends: "libvndk",
2603 },
2604 shared_libs: ["libproduct_for_vndklibs"],
2605 nocrt: true,
2606 }
2607
2608 cc_library {
2609 name: "libvndk_sp",
2610 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002611 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09002612 vndk: {
2613 enabled: true,
2614 support_system_process: true,
2615 },
2616 nocrt: true,
2617 }
2618
2619 cc_library {
2620 name: "libvndk_sp_ext_product",
2621 product_specific: true,
2622 vndk: {
2623 enabled: true,
2624 extends: "libvndk_sp",
2625 support_system_process: true,
2626 },
2627 shared_libs: ["libproduct_for_vndklibs"],
2628 nocrt: true,
2629 }
2630
2631 cc_library {
2632 name: "libproduct",
2633 product_specific: true,
2634 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
2635 nocrt: true,
2636 }
2637
2638 cc_library {
2639 name: "libproduct_for_vndklibs",
2640 product_specific: true,
2641 nocrt: true,
2642 }
2643 `
2644 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2645 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2646 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2647 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2648
2649 testCcWithConfig(t, config)
2650}
2651
Logan Chiend3c59a22018-03-29 14:08:15 +08002652func TestVndkSpExtUseVndkError(t *testing.T) {
2653 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
2654 // library.
2655 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2656 cc_library {
2657 name: "libvndk",
2658 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002659 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002660 vndk: {
2661 enabled: true,
2662 },
2663 nocrt: true,
2664 }
2665
2666 cc_library {
2667 name: "libvndk_sp",
2668 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002669 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002670 vndk: {
2671 enabled: true,
2672 support_system_process: true,
2673 },
2674 nocrt: true,
2675 }
2676
2677 cc_library {
2678 name: "libvndk_sp_ext",
2679 vendor: true,
2680 vndk: {
2681 enabled: true,
2682 extends: "libvndk_sp",
2683 support_system_process: true,
2684 },
2685 shared_libs: ["libvndk"], // Cause an error
2686 nocrt: true,
2687 }
2688 `)
2689
2690 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
2691 // library.
2692 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
2693 cc_library {
2694 name: "libvndk",
2695 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002696 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002697 vndk: {
2698 enabled: true,
2699 },
2700 nocrt: true,
2701 }
2702
2703 cc_library {
2704 name: "libvndk_ext",
2705 vendor: true,
2706 vndk: {
2707 enabled: true,
2708 extends: "libvndk",
2709 },
2710 nocrt: true,
2711 }
2712
2713 cc_library {
2714 name: "libvndk_sp",
2715 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002716 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08002717 vndk: {
2718 enabled: true,
2719 support_system_process: true,
2720 },
2721 nocrt: true,
2722 }
2723
2724 cc_library {
2725 name: "libvndk_sp_ext",
2726 vendor: true,
2727 vndk: {
2728 enabled: true,
2729 extends: "libvndk_sp",
2730 support_system_process: true,
2731 },
2732 shared_libs: ["libvndk_ext"], // Cause an error
2733 nocrt: true,
2734 }
2735 `)
2736}
2737
2738func TestVndkUseVndkExtError(t *testing.T) {
2739 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
2740 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08002741 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2742 cc_library {
2743 name: "libvndk",
2744 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002745 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002746 vndk: {
2747 enabled: true,
2748 },
2749 nocrt: true,
2750 }
2751
2752 cc_library {
2753 name: "libvndk_ext",
2754 vendor: true,
2755 vndk: {
2756 enabled: true,
2757 extends: "libvndk",
2758 },
2759 nocrt: true,
2760 }
2761
2762 cc_library {
2763 name: "libvndk2",
2764 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002765 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002766 vndk: {
2767 enabled: true,
2768 },
2769 shared_libs: ["libvndk_ext"],
2770 nocrt: true,
2771 }
2772 `)
2773
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002774 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002775 cc_library {
2776 name: "libvndk",
2777 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002778 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002779 vndk: {
2780 enabled: true,
2781 },
2782 nocrt: true,
2783 }
2784
2785 cc_library {
2786 name: "libvndk_ext",
2787 vendor: true,
2788 vndk: {
2789 enabled: true,
2790 extends: "libvndk",
2791 },
2792 nocrt: true,
2793 }
2794
2795 cc_library {
2796 name: "libvndk2",
2797 vendor_available: true,
2798 vndk: {
2799 enabled: true,
2800 },
2801 target: {
2802 vendor: {
2803 shared_libs: ["libvndk_ext"],
2804 },
2805 },
2806 nocrt: true,
2807 }
2808 `)
2809
2810 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2811 cc_library {
2812 name: "libvndk_sp",
2813 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002814 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002815 vndk: {
2816 enabled: true,
2817 support_system_process: true,
2818 },
2819 nocrt: true,
2820 }
2821
2822 cc_library {
2823 name: "libvndk_sp_ext",
2824 vendor: true,
2825 vndk: {
2826 enabled: true,
2827 extends: "libvndk_sp",
2828 support_system_process: true,
2829 },
2830 nocrt: true,
2831 }
2832
2833 cc_library {
2834 name: "libvndk_sp_2",
2835 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002836 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002837 vndk: {
2838 enabled: true,
2839 support_system_process: true,
2840 },
2841 shared_libs: ["libvndk_sp_ext"],
2842 nocrt: true,
2843 }
2844 `)
2845
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002846 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002847 cc_library {
2848 name: "libvndk_sp",
2849 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002850 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002851 vndk: {
2852 enabled: true,
2853 },
2854 nocrt: true,
2855 }
2856
2857 cc_library {
2858 name: "libvndk_sp_ext",
2859 vendor: true,
2860 vndk: {
2861 enabled: true,
2862 extends: "libvndk_sp",
2863 },
2864 nocrt: true,
2865 }
2866
2867 cc_library {
2868 name: "libvndk_sp2",
2869 vendor_available: true,
2870 vndk: {
2871 enabled: true,
2872 },
2873 target: {
2874 vendor: {
2875 shared_libs: ["libvndk_sp_ext"],
2876 },
2877 },
2878 nocrt: true,
2879 }
2880 `)
2881}
2882
Justin Yun5f7f7e82019-11-18 19:52:14 +09002883func TestEnforceProductVndkVersion(t *testing.T) {
2884 bp := `
2885 cc_library {
2886 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002887 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002888 }
2889 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002890 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002891 symbol_file: "",
2892 }
2893 cc_library {
2894 name: "libvndk",
2895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002896 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002897 vndk: {
2898 enabled: true,
2899 },
2900 nocrt: true,
2901 }
2902 cc_library {
2903 name: "libvndk_sp",
2904 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002905 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002906 vndk: {
2907 enabled: true,
2908 support_system_process: true,
2909 },
2910 nocrt: true,
2911 }
2912 cc_library {
2913 name: "libva",
2914 vendor_available: true,
2915 nocrt: true,
2916 }
2917 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002918 name: "libpa",
2919 product_available: true,
2920 nocrt: true,
2921 }
2922 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002923 name: "libboth_available",
2924 vendor_available: true,
2925 product_available: true,
2926 nocrt: true,
2927 target: {
2928 vendor: {
2929 suffix: "-vendor",
2930 },
2931 product: {
2932 suffix: "-product",
2933 },
2934 }
2935 }
2936 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002937 name: "libproduct_va",
2938 product_specific: true,
2939 vendor_available: true,
2940 nocrt: true,
2941 }
2942 cc_library {
2943 name: "libprod",
2944 product_specific: true,
2945 shared_libs: [
2946 "libllndk",
2947 "libvndk",
2948 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002949 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002950 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002951 "libproduct_va",
2952 ],
2953 nocrt: true,
2954 }
2955 cc_library {
2956 name: "libvendor",
2957 vendor: true,
2958 shared_libs: [
2959 "libllndk",
2960 "libvndk",
2961 "libvndk_sp",
2962 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002963 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002964 "libproduct_va",
2965 ],
2966 nocrt: true,
2967 }
2968 `
2969
2970 config := TestConfig(buildDir, android.Android, nil, bp, nil)
2971 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2972 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
2973 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2974
2975 ctx := testCcWithConfig(t, config)
2976
Jooyung Han261e1582020-10-20 18:54:21 +09002977 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2978 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002979
2980 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2981 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2982
2983 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2984 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002985}
2986
2987func TestEnforceProductVndkVersionErrors(t *testing.T) {
2988 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2989 cc_library {
2990 name: "libprod",
2991 product_specific: true,
2992 shared_libs: [
2993 "libvendor",
2994 ],
2995 nocrt: true,
2996 }
2997 cc_library {
2998 name: "libvendor",
2999 vendor: true,
3000 nocrt: true,
3001 }
3002 `)
3003 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3004 cc_library {
3005 name: "libprod",
3006 product_specific: true,
3007 shared_libs: [
3008 "libsystem",
3009 ],
3010 nocrt: true,
3011 }
3012 cc_library {
3013 name: "libsystem",
3014 nocrt: true,
3015 }
3016 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09003017 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3018 cc_library {
3019 name: "libprod",
3020 product_specific: true,
3021 shared_libs: [
3022 "libva",
3023 ],
3024 nocrt: true,
3025 }
3026 cc_library {
3027 name: "libva",
3028 vendor_available: true,
3029 nocrt: true,
3030 }
3031 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09003032 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09003033 cc_library {
3034 name: "libprod",
3035 product_specific: true,
3036 shared_libs: [
3037 "libvndk_private",
3038 ],
3039 nocrt: true,
3040 }
3041 cc_library {
3042 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09003043 vendor_available: true,
3044 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003045 vndk: {
3046 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003047 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09003048 },
3049 nocrt: true,
3050 }
3051 `)
3052 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
3053 cc_library {
3054 name: "libprod",
3055 product_specific: true,
3056 shared_libs: [
3057 "libsystem_ext",
3058 ],
3059 nocrt: true,
3060 }
3061 cc_library {
3062 name: "libsystem_ext",
3063 system_ext_specific: true,
3064 nocrt: true,
3065 }
3066 `)
3067 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
3068 cc_library {
3069 name: "libsystem",
3070 shared_libs: [
3071 "libproduct_va",
3072 ],
3073 nocrt: true,
3074 }
3075 cc_library {
3076 name: "libproduct_va",
3077 product_specific: true,
3078 vendor_available: true,
3079 nocrt: true,
3080 }
3081 `)
3082}
3083
Jooyung Han38002912019-05-16 04:01:54 +09003084func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08003085 bp := `
3086 cc_library {
3087 name: "libvndk",
3088 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003089 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003090 vndk: {
3091 enabled: true,
3092 },
3093 }
3094 cc_library {
3095 name: "libvndksp",
3096 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003097 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003098 vndk: {
3099 enabled: true,
3100 support_system_process: true,
3101 },
3102 }
3103 cc_library {
3104 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09003105 vendor_available: true,
3106 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003107 vndk: {
3108 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09003109 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003110 },
3111 }
3112 cc_library {
3113 name: "libvendor",
3114 vendor: true,
3115 }
3116 cc_library {
3117 name: "libvndkext",
3118 vendor: true,
3119 vndk: {
3120 enabled: true,
3121 extends: "libvndk",
3122 },
3123 }
3124 vndk_prebuilt_shared {
3125 name: "prevndk",
3126 version: "27",
3127 target_arch: "arm",
3128 binder32bit: true,
3129 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09003130 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08003131 vndk: {
3132 enabled: true,
3133 },
3134 arch: {
3135 arm: {
3136 srcs: ["liba.so"],
3137 },
3138 },
3139 }
3140 cc_library {
3141 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07003142 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003143 }
3144 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003145 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003146 symbol_file: "",
3147 }
3148 cc_library {
3149 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07003150 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003151 }
3152 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003153 name: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08003154 vendor_available: false,
3155 symbol_file: "",
3156 }`
3157
3158 config := TestConfig(buildDir, android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09003159 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3160 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3161 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08003162 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09003163
Jooyung Han0302a842019-10-30 18:43:49 +09003164 assertMapKeys(t, vndkCoreLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003165 []string{"libvndk", "libvndkprivate"})
Jooyung Han0302a842019-10-30 18:43:49 +09003166 assertMapKeys(t, vndkSpLibraries(config),
Jooyung Han38002912019-05-16 04:01:54 +09003167 []string{"libc++", "libvndksp"})
Jooyung Han0302a842019-10-30 18:43:49 +09003168 assertMapKeys(t, llndkLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003169 []string{"libc", "libdl", "libft2", "libllndk", "libllndkprivate", "libm"})
Jooyung Han0302a842019-10-30 18:43:49 +09003170 assertMapKeys(t, vndkPrivateLibraries(config),
Jooyung Han097087b2019-10-22 19:32:18 +09003171 []string{"libft2", "libllndkprivate", "libvndkprivate"})
Jooyung Han38002912019-05-16 04:01:54 +09003172
Colin Crossfb0c16e2019-11-20 17:12:35 -08003173 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09003174
Jooyung Han38002912019-05-16 04:01:54 +09003175 tests := []struct {
3176 variant string
3177 name string
3178 expected string
3179 }{
3180 {vendorVariant, "libvndk", "native:vndk"},
3181 {vendorVariant, "libvndksp", "native:vndk"},
3182 {vendorVariant, "libvndkprivate", "native:vndk_private"},
3183 {vendorVariant, "libvendor", "native:vendor"},
3184 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08003185 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09003186 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09003187 {coreVariant, "libvndk", "native:platform"},
3188 {coreVariant, "libvndkprivate", "native:platform"},
3189 {coreVariant, "libllndk", "native:platform"},
3190 }
3191 for _, test := range tests {
3192 t.Run(test.name, func(t *testing.T) {
3193 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
3194 assertString(t, module.makeLinkType, test.expected)
3195 })
3196 }
3197}
3198
Jeff Gaston294356f2017-09-27 17:05:30 -07003199var staticLinkDepOrderTestCases = []struct {
3200 // This is a string representation of a map[moduleName][]moduleDependency .
3201 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003202 inStatic string
3203
3204 // This is a string representation of a map[moduleName][]moduleDependency .
3205 // It models the dependencies declared in an Android.bp file.
3206 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07003207
3208 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
3209 // The keys of allOrdered specify which modules we would like to check.
3210 // The values of allOrdered specify the expected result (of the transitive closure of all
3211 // dependencies) for each module to test
3212 allOrdered string
3213
3214 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
3215 // The keys of outOrdered specify which modules we would like to check.
3216 // The values of outOrdered specify the expected result (of the ordered linker command line)
3217 // for each module to test.
3218 outOrdered string
3219}{
3220 // Simple tests
3221 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003222 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07003223 outOrdered: "",
3224 },
3225 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003226 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003227 outOrdered: "a:",
3228 },
3229 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003230 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003231 outOrdered: "a:b; b:",
3232 },
3233 // Tests of reordering
3234 {
3235 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003236 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07003237 outOrdered: "a:b,c,d; b:d; c:d; d:",
3238 },
3239 {
3240 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003241 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003242 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
3243 },
3244 {
3245 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003246 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07003247 outOrdered: "a:d,b,e,c; d:b; e:c",
3248 },
3249 {
3250 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003251 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07003252 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
3253 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
3254 },
3255 {
3256 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003257 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 -07003258 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3259 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
3260 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003261 // shared dependencies
3262 {
3263 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
3264 // So, we don't actually have to check that a shared dependency of c will change the order
3265 // of a library that depends statically on b and on c. We only need to check that if c has
3266 // a shared dependency on b, that that shows up in allOrdered.
3267 inShared: "c:b",
3268 allOrdered: "c:b",
3269 outOrdered: "c:",
3270 },
3271 {
3272 // This test doesn't actually include any shared dependencies but it's a reminder of what
3273 // the second phase of the above test would look like
3274 inStatic: "a:b,c; c:b",
3275 allOrdered: "a:c,b; c:b",
3276 outOrdered: "a:c,b; c:b",
3277 },
Jeff Gaston294356f2017-09-27 17:05:30 -07003278 // tiebreakers for when two modules specifying different orderings and there is no dependency
3279 // to dictate an order
3280 {
3281 // 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 -08003282 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07003283 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
3284 },
3285 {
3286 // 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 -08003287 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 -07003288 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
3289 },
3290 // Tests involving duplicate dependencies
3291 {
3292 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003293 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003294 outOrdered: "a:c,b",
3295 },
3296 {
3297 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003298 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003299 outOrdered: "a:d,c,b",
3300 },
3301 // Tests to confirm the nonexistence of infinite loops.
3302 // These cases should never happen, so as long as the test terminates and the
3303 // result is deterministic then that should be fine.
3304 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003305 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003306 outOrdered: "a:a",
3307 },
3308 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003309 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07003310 allOrdered: "a:b,c; b:c,a; c:a,b",
3311 outOrdered: "a:b; b:c; c:a",
3312 },
3313 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003314 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07003315 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
3316 outOrdered: "a:c,b; b:a,c; c:b,a",
3317 },
3318}
3319
3320// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
3321func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
3322 // convert from "a:b,c; d:e" to "a:b,c;d:e"
3323 strippedText := strings.Replace(text, " ", "", -1)
3324 if len(strippedText) < 1 {
3325 return []android.Path{}, make(map[android.Path][]android.Path, 0)
3326 }
3327 allDeps = make(map[android.Path][]android.Path, 0)
3328
3329 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
3330 moduleTexts := strings.Split(strippedText, ";")
3331
3332 outputForModuleName := func(moduleName string) android.Path {
3333 return android.PathForTesting(moduleName)
3334 }
3335
3336 for _, moduleText := range moduleTexts {
3337 // convert from "a:b,c" to ["a", "b,c"]
3338 components := strings.Split(moduleText, ":")
3339 if len(components) != 2 {
3340 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
3341 }
3342 moduleName := components[0]
3343 moduleOutput := outputForModuleName(moduleName)
3344 modulesInOrder = append(modulesInOrder, moduleOutput)
3345
3346 depString := components[1]
3347 // convert from "b,c" to ["b", "c"]
3348 depNames := strings.Split(depString, ",")
3349 if len(depString) < 1 {
3350 depNames = []string{}
3351 }
3352 var deps []android.Path
3353 for _, depName := range depNames {
3354 deps = append(deps, outputForModuleName(depName))
3355 }
3356 allDeps[moduleOutput] = deps
3357 }
3358 return modulesInOrder, allDeps
3359}
3360
Jeff Gaston294356f2017-09-27 17:05:30 -07003361func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
3362 for _, moduleName := range moduleNames {
3363 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
3364 output := module.outputFile.Path()
3365 paths = append(paths, output)
3366 }
3367 return paths
3368}
3369
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003370func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07003371 ctx := testCc(t, `
3372 cc_library {
3373 name: "a",
3374 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09003375 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003376 }
3377 cc_library {
3378 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003379 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003380 }
3381 cc_library {
3382 name: "c",
3383 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003384 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003385 }
3386 cc_library {
3387 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09003388 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07003389 }
3390
3391 `)
3392
Colin Cross7113d202019-11-20 16:39:12 -08003393 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07003394 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003395 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3396 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07003397
3398 if !reflect.DeepEqual(actual, expected) {
3399 t.Errorf("staticDeps orderings were not propagated correctly"+
3400 "\nactual: %v"+
3401 "\nexpected: %v",
3402 actual,
3403 expected,
3404 )
3405 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09003406}
Jeff Gaston294356f2017-09-27 17:05:30 -07003407
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003408func TestStaticLibDepReorderingWithShared(t *testing.T) {
3409 ctx := testCc(t, `
3410 cc_library {
3411 name: "a",
3412 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09003413 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003414 }
3415 cc_library {
3416 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09003417 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003418 }
3419 cc_library {
3420 name: "c",
3421 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09003422 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003423 }
3424
3425 `)
3426
Colin Cross7113d202019-11-20 16:39:12 -08003427 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003428 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Colin Cross0de8a1e2020-09-18 14:15:30 -07003429 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).TransitiveStaticLibrariesForOrdering.ToList()
3430 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08003431
3432 if !reflect.DeepEqual(actual, expected) {
3433 t.Errorf("staticDeps orderings did not account for shared libs"+
3434 "\nactual: %v"+
3435 "\nexpected: %v",
3436 actual,
3437 expected,
3438 )
3439 }
3440}
3441
Jooyung Hanb04a4992020-03-13 18:57:35 +09003442func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07003443 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003444 if !reflect.DeepEqual(actual, expected) {
3445 t.Errorf(message+
3446 "\nactual: %v"+
3447 "\nexpected: %v",
3448 actual,
3449 expected,
3450 )
3451 }
3452}
3453
Jooyung Han61b66e92020-03-21 14:21:46 +00003454func TestLlndkLibrary(t *testing.T) {
3455 ctx := testCc(t, `
3456 cc_library {
3457 name: "libllndk",
3458 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07003459 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003460 }
3461 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003462 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00003463 }
Colin Cross127bb8b2020-12-16 16:46:01 -08003464
3465 cc_prebuilt_library_shared {
3466 name: "libllndkprebuilt",
3467 stubs: { versions: ["1", "2"] },
3468 llndk_stubs: "libllndkprebuilt.llndk",
3469 }
3470 llndk_library {
3471 name: "libllndkprebuilt.llndk",
3472 }
3473
3474 cc_library {
3475 name: "libllndk_with_external_headers",
3476 stubs: { versions: ["1", "2"] },
3477 llndk_stubs: "libllndk_with_external_headers.llndk",
3478 header_libs: ["libexternal_headers"],
3479 export_header_lib_headers: ["libexternal_headers"],
3480 }
3481 llndk_library {
3482 name: "libllndk_with_external_headers.llndk",
3483 }
3484 cc_library_headers {
3485 name: "libexternal_headers",
3486 export_include_dirs: ["include"],
3487 vendor_available: true,
3488 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003489 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08003490 actual := ctx.ModuleVariantsForTests("libllndk")
3491 for i := 0; i < len(actual); i++ {
3492 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
3493 actual = append(actual[:i], actual[i+1:]...)
3494 i--
3495 }
3496 }
Jooyung Han61b66e92020-03-21 14:21:46 +00003497 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00003498 "android_vendor.VER_arm64_armv8-a_shared_1",
3499 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003500 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003501 "android_vendor.VER_arm_armv7-a-neon_shared_1",
3502 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07003503 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00003504 }
3505 checkEquals(t, "variants for llndk stubs", expected, actual)
3506
Colin Cross127bb8b2020-12-16 16:46:01 -08003507 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003508 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
3509
Colin Cross127bb8b2020-12-16 16:46:01 -08003510 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00003511 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
3512}
3513
Jiyong Parka46a4d52017-12-14 19:54:34 +09003514func TestLlndkHeaders(t *testing.T) {
3515 ctx := testCc(t, `
3516 llndk_headers {
3517 name: "libllndk_headers",
3518 export_include_dirs: ["my_include"],
3519 }
3520 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07003521 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09003522 export_llndk_headers: ["libllndk_headers"],
3523 }
3524 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07003525 name: "libllndk",
3526 llndk_stubs: "libllndk.llndk",
3527 }
3528
3529 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09003530 name: "libvendor",
3531 shared_libs: ["libllndk"],
3532 vendor: true,
3533 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003534 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08003535 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09003536 }
3537 `)
3538
3539 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08003540 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09003541 cflags := cc.Args["cFlags"]
3542 if !strings.Contains(cflags, "-Imy_include") {
3543 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
3544 }
3545}
3546
Logan Chien43d34c32017-12-20 01:17:32 +08003547func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
3548 actual := module.Properties.AndroidMkRuntimeLibs
3549 if !reflect.DeepEqual(actual, expected) {
3550 t.Errorf("incorrect runtime_libs for shared libs"+
3551 "\nactual: %v"+
3552 "\nexpected: %v",
3553 actual,
3554 expected,
3555 )
3556 }
3557}
3558
3559const runtimeLibAndroidBp = `
3560 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09003561 name: "liball_available",
3562 vendor_available: true,
3563 product_available: true,
3564 no_libcrt : true,
3565 nocrt : true,
3566 system_shared_libs : [],
3567 }
3568 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08003569 name: "libvendor_available1",
3570 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003571 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003572 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003573 nocrt : true,
3574 system_shared_libs : [],
3575 }
3576 cc_library {
3577 name: "libvendor_available2",
3578 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003579 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003580 target: {
3581 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09003582 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08003583 }
3584 },
Yi Konge7fe9912019-06-02 00:53:50 -07003585 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003586 nocrt : true,
3587 system_shared_libs : [],
3588 }
3589 cc_library {
3590 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09003591 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07003592 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003593 nocrt : true,
3594 system_shared_libs : [],
3595 }
3596 cc_library {
3597 name: "libvendor1",
3598 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003599 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003600 nocrt : true,
3601 system_shared_libs : [],
3602 }
3603 cc_library {
3604 name: "libvendor2",
3605 vendor: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09003606 runtime_libs: ["liball_available", "libvendor1"],
3607 no_libcrt : true,
3608 nocrt : true,
3609 system_shared_libs : [],
3610 }
3611 cc_library {
3612 name: "libproduct_available1",
3613 product_available: true,
3614 runtime_libs: ["liball_available"],
3615 no_libcrt : true,
3616 nocrt : true,
3617 system_shared_libs : [],
3618 }
3619 cc_library {
3620 name: "libproduct1",
3621 product_specific: true,
3622 no_libcrt : true,
3623 nocrt : true,
3624 system_shared_libs : [],
3625 }
3626 cc_library {
3627 name: "libproduct2",
3628 product_specific: true,
3629 runtime_libs: ["liball_available", "libproduct1"],
Yi Konge7fe9912019-06-02 00:53:50 -07003630 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08003631 nocrt : true,
3632 system_shared_libs : [],
3633 }
3634`
3635
3636func TestRuntimeLibs(t *testing.T) {
3637 ctx := testCc(t, runtimeLibAndroidBp)
3638
3639 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08003640 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003641
Justin Yun8a2600c2020-12-07 12:44:03 +09003642 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3643 checkRuntimeLibs(t, []string{"liball_available"}, module)
3644
3645 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3646 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003647
3648 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003649 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003650
3651 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
3652 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08003653 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003654
Justin Yun8a2600c2020-12-07 12:44:03 +09003655 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3656 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003657
3658 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003659 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1"}, module)
3660
3661 // runtime_libs for product variants have '.product' suffixes if the modules have both core
3662 // and product variants.
3663 variant = "android_product.VER_arm64_armv8-a_shared"
3664
3665 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
3666 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
3667
3668 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3669 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003670}
3671
3672func TestExcludeRuntimeLibs(t *testing.T) {
3673 ctx := testCc(t, runtimeLibAndroidBp)
3674
Colin Cross7113d202019-11-20 16:39:12 -08003675 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003676 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
3677 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003678
Colin Crossfb0c16e2019-11-20 17:12:35 -08003679 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09003680 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08003681 checkRuntimeLibs(t, nil, module)
3682}
3683
3684func TestRuntimeLibsNoVndk(t *testing.T) {
3685 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3686
3687 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3688
Colin Cross7113d202019-11-20 16:39:12 -08003689 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003690
Justin Yun8a2600c2020-12-07 12:44:03 +09003691 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3692 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003693
3694 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003695 checkRuntimeLibs(t, []string{"liball_available", "libvendor1"}, module)
3696
3697 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
3698 checkRuntimeLibs(t, []string{"liball_available", "libproduct1"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003699}
3700
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003701func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003702 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003703 actual := module.Properties.AndroidMkStaticLibs
3704 if !reflect.DeepEqual(actual, expected) {
3705 t.Errorf("incorrect static_libs"+
3706 "\nactual: %v"+
3707 "\nexpected: %v",
3708 actual,
3709 expected,
3710 )
3711 }
3712}
3713
3714const staticLibAndroidBp = `
3715 cc_library {
3716 name: "lib1",
3717 }
3718 cc_library {
3719 name: "lib2",
3720 static_libs: ["lib1"],
3721 }
3722`
3723
3724func TestStaticLibDepExport(t *testing.T) {
3725 ctx := testCc(t, staticLibAndroidBp)
3726
3727 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003728 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003729 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003730 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003731
3732 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003733 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003734 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3735 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08003736 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003737}
3738
Jiyong Parkd08b6972017-09-26 10:50:54 +09003739var compilerFlagsTestCases = []struct {
3740 in string
3741 out bool
3742}{
3743 {
3744 in: "a",
3745 out: false,
3746 },
3747 {
3748 in: "-a",
3749 out: true,
3750 },
3751 {
3752 in: "-Ipath/to/something",
3753 out: false,
3754 },
3755 {
3756 in: "-isystempath/to/something",
3757 out: false,
3758 },
3759 {
3760 in: "--coverage",
3761 out: false,
3762 },
3763 {
3764 in: "-include a/b",
3765 out: true,
3766 },
3767 {
3768 in: "-include a/b c/d",
3769 out: false,
3770 },
3771 {
3772 in: "-DMACRO",
3773 out: true,
3774 },
3775 {
3776 in: "-DMAC RO",
3777 out: false,
3778 },
3779 {
3780 in: "-a -b",
3781 out: false,
3782 },
3783 {
3784 in: "-DMACRO=definition",
3785 out: true,
3786 },
3787 {
3788 in: "-DMACRO=defi nition",
3789 out: true, // TODO(jiyong): this should be false
3790 },
3791 {
3792 in: "-DMACRO(x)=x + 1",
3793 out: true,
3794 },
3795 {
3796 in: "-DMACRO=\"defi nition\"",
3797 out: true,
3798 },
3799}
3800
3801type mockContext struct {
3802 BaseModuleContext
3803 result bool
3804}
3805
3806func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3807 // CheckBadCompilerFlags calls this function when the flag should be rejected
3808 ctx.result = false
3809}
3810
3811func TestCompilerFlags(t *testing.T) {
3812 for _, testCase := range compilerFlagsTestCases {
3813 ctx := &mockContext{result: true}
3814 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3815 if ctx.result != testCase.out {
3816 t.Errorf("incorrect output:")
3817 t.Errorf(" input: %#v", testCase.in)
3818 t.Errorf(" expected: %#v", testCase.out)
3819 t.Errorf(" got: %#v", ctx.result)
3820 }
3821 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003822}
Jiyong Park374510b2018-03-19 18:23:01 +09003823
3824func TestVendorPublicLibraries(t *testing.T) {
3825 ctx := testCc(t, `
3826 cc_library_headers {
3827 name: "libvendorpublic_headers",
3828 export_include_dirs: ["my_include"],
3829 }
3830 vendor_public_library {
3831 name: "libvendorpublic",
3832 symbol_file: "",
3833 export_public_headers: ["libvendorpublic_headers"],
3834 }
3835 cc_library {
3836 name: "libvendorpublic",
3837 srcs: ["foo.c"],
3838 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003839 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003840 nocrt: true,
3841 }
3842
3843 cc_library {
3844 name: "libsystem",
3845 shared_libs: ["libvendorpublic"],
3846 vendor: false,
3847 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003848 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003849 nocrt: true,
3850 }
3851 cc_library {
3852 name: "libvendor",
3853 shared_libs: ["libvendorpublic"],
3854 vendor: true,
3855 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003856 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003857 nocrt: true,
3858 }
3859 `)
3860
Colin Cross7113d202019-11-20 16:39:12 -08003861 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003862 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003863
3864 // test if header search paths are correctly added
3865 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003866 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003867 cflags := cc.Args["cFlags"]
3868 if !strings.Contains(cflags, "-Imy_include") {
3869 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3870 }
3871
3872 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003873 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003874 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003875 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003876 if !strings.Contains(libflags, stubPaths[0].String()) {
3877 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3878 }
3879
3880 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003881 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003882 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003883 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003884 if !strings.Contains(libflags, stubPaths[0].String()) {
3885 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3886 }
3887
3888}
Jiyong Park37b25202018-07-11 10:49:27 +09003889
3890func TestRecovery(t *testing.T) {
3891 ctx := testCc(t, `
3892 cc_library_shared {
3893 name: "librecovery",
3894 recovery: true,
3895 }
3896 cc_library_shared {
3897 name: "librecovery32",
3898 recovery: true,
3899 compile_multilib:"32",
3900 }
Jiyong Park5baac542018-08-28 09:55:37 +09003901 cc_library_shared {
3902 name: "libHalInRecovery",
3903 recovery_available: true,
3904 vendor: true,
3905 }
Jiyong Park37b25202018-07-11 10:49:27 +09003906 `)
3907
3908 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003909 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003910 if len(variants) != 1 || !android.InList(arm64, variants) {
3911 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3912 }
3913
3914 variants = ctx.ModuleVariantsForTests("librecovery32")
3915 if android.InList(arm64, variants) {
3916 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3917 }
Jiyong Park5baac542018-08-28 09:55:37 +09003918
3919 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3920 if !recoveryModule.Platform() {
3921 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3922 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003923}
Jiyong Park5baac542018-08-28 09:55:37 +09003924
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003925func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3926 bp := `
3927 cc_prebuilt_test_library_shared {
3928 name: "test_lib",
3929 relative_install_path: "foo/bar/baz",
3930 srcs: ["srcpath/dontusethispath/baz.so"],
3931 }
3932
3933 cc_test {
3934 name: "main_test",
3935 data_libs: ["test_lib"],
3936 gtest: false,
3937 }
3938 `
3939
3940 config := TestConfig(buildDir, android.Android, nil, bp, nil)
3941 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3942 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3943 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3944
3945 ctx := testCcWithConfig(t, config)
3946 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3947 testBinary := module.(*Module).linker.(*testBinary)
3948 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3949 if err != nil {
3950 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3951 }
3952 if len(outputFiles) != 1 {
3953 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3954 }
3955 if len(testBinary.dataPaths()) != 1 {
3956 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3957 }
3958
3959 outputPath := outputFiles[0].String()
3960
3961 if !strings.HasSuffix(outputPath, "/main_test") {
3962 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3963 }
3964 entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
3965 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3966 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3967 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3968 }
3969}
3970
Jiyong Park7ed9de32018-10-15 22:25:07 +09003971func TestVersionedStubs(t *testing.T) {
3972 ctx := testCc(t, `
3973 cc_library_shared {
3974 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003975 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003976 stubs: {
3977 symbol_file: "foo.map.txt",
3978 versions: ["1", "2", "3"],
3979 },
3980 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003981
Jiyong Park7ed9de32018-10-15 22:25:07 +09003982 cc_library_shared {
3983 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003984 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003985 shared_libs: ["libFoo#1"],
3986 }`)
3987
3988 variants := ctx.ModuleVariantsForTests("libFoo")
3989 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003990 "android_arm64_armv8-a_shared",
3991 "android_arm64_armv8-a_shared_1",
3992 "android_arm64_armv8-a_shared_2",
3993 "android_arm64_armv8-a_shared_3",
3994 "android_arm_armv7-a-neon_shared",
3995 "android_arm_armv7-a-neon_shared_1",
3996 "android_arm_armv7-a-neon_shared_2",
3997 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003998 }
3999 variantsMismatch := false
4000 if len(variants) != len(expectedVariants) {
4001 variantsMismatch = true
4002 } else {
4003 for _, v := range expectedVariants {
4004 if !inList(v, variants) {
4005 variantsMismatch = false
4006 }
4007 }
4008 }
4009 if variantsMismatch {
4010 t.Errorf("variants of libFoo expected:\n")
4011 for _, v := range expectedVariants {
4012 t.Errorf("%q\n", v)
4013 }
4014 t.Errorf(", but got:\n")
4015 for _, v := range variants {
4016 t.Errorf("%q\n", v)
4017 }
4018 }
4019
Colin Cross7113d202019-11-20 16:39:12 -08004020 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09004021 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08004022 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09004023 if !strings.Contains(libFlags, libFoo1StubPath) {
4024 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
4025 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09004026
Colin Cross7113d202019-11-20 16:39:12 -08004027 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09004028 cFlags := libBarCompileRule.Args["cFlags"]
4029 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
4030 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
4031 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
4032 }
Jiyong Park37b25202018-07-11 10:49:27 +09004033}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004034
Jooyung Hanb04a4992020-03-13 18:57:35 +09004035func TestVersioningMacro(t *testing.T) {
4036 for _, tc := range []struct{ moduleName, expected string }{
4037 {"libc", "__LIBC_API__"},
4038 {"libfoo", "__LIBFOO_API__"},
4039 {"libfoo@1", "__LIBFOO_1_API__"},
4040 {"libfoo-v1", "__LIBFOO_V1_API__"},
4041 {"libfoo.v1", "__LIBFOO_V1_API__"},
4042 } {
4043 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
4044 }
4045}
4046
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004047func TestStaticExecutable(t *testing.T) {
4048 ctx := testCc(t, `
4049 cc_binary {
4050 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01004051 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004052 static_executable: true,
4053 }`)
4054
Colin Cross7113d202019-11-20 16:39:12 -08004055 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004056 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
4057 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07004058 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08004059 for _, lib := range systemStaticLibs {
4060 if !strings.Contains(libFlags, lib) {
4061 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
4062 }
4063 }
4064 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
4065 for _, lib := range systemSharedLibs {
4066 if strings.Contains(libFlags, lib) {
4067 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
4068 }
4069 }
4070}
Jiyong Parke4bb9862019-02-01 00:31:10 +09004071
4072func TestStaticDepsOrderWithStubs(t *testing.T) {
4073 ctx := testCc(t, `
4074 cc_binary {
4075 name: "mybin",
4076 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07004077 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004078 static_executable: true,
4079 stl: "none",
4080 }
4081
4082 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004083 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004084 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08004085 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09004086 stl: "none",
4087 }
4088
4089 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08004090 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09004091 srcs: ["foo.c"],
4092 stl: "none",
4093 stubs: {
4094 versions: ["1"],
4095 },
4096 }`)
4097
Colin Cross0de8a1e2020-09-18 14:15:30 -07004098 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
4099 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08004100 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09004101
4102 if !reflect.DeepEqual(actual, expected) {
4103 t.Errorf("staticDeps orderings were not propagated correctly"+
4104 "\nactual: %v"+
4105 "\nexpected: %v",
4106 actual,
4107 expected,
4108 )
4109 }
4110}
Jooyung Han38002912019-05-16 04:01:54 +09004111
Jooyung Hand48f3c32019-08-23 11:18:57 +09004112func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
4113 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
4114 cc_library {
4115 name: "libA",
4116 srcs: ["foo.c"],
4117 shared_libs: ["libB"],
4118 stl: "none",
4119 }
4120
4121 cc_library {
4122 name: "libB",
4123 srcs: ["foo.c"],
4124 enabled: false,
4125 stl: "none",
4126 }
4127 `)
4128}
4129
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004130// Simple smoke test for the cc_fuzz target that ensures the rule compiles
4131// correctly.
4132func TestFuzzTarget(t *testing.T) {
4133 ctx := testCc(t, `
4134 cc_fuzz {
4135 name: "fuzz_smoke_test",
4136 srcs: ["foo.c"],
4137 }`)
4138
Paul Duffin075c4172019-12-19 19:06:13 +00004139 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07004140 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
4141}
4142
Jiyong Park29074592019-07-07 16:27:47 +09004143func TestAidl(t *testing.T) {
4144}
4145
Jooyung Han38002912019-05-16 04:01:54 +09004146func assertString(t *testing.T, got, expected string) {
4147 t.Helper()
4148 if got != expected {
4149 t.Errorf("expected %q got %q", expected, got)
4150 }
4151}
4152
4153func assertArrayString(t *testing.T, got, expected []string) {
4154 t.Helper()
4155 if len(got) != len(expected) {
4156 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
4157 return
4158 }
4159 for i := range got {
4160 if got[i] != expected[i] {
4161 t.Errorf("expected %d-th %q (%q) got %q (%q)",
4162 i, expected[i], expected, got[i], got)
4163 return
4164 }
4165 }
4166}
Colin Crosse1bb5d02019-09-24 14:55:04 -07004167
Jooyung Han0302a842019-10-30 18:43:49 +09004168func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
4169 t.Helper()
4170 assertArrayString(t, android.SortedStringKeys(m), expected)
4171}
4172
Colin Crosse1bb5d02019-09-24 14:55:04 -07004173func TestDefaults(t *testing.T) {
4174 ctx := testCc(t, `
4175 cc_defaults {
4176 name: "defaults",
4177 srcs: ["foo.c"],
4178 static: {
4179 srcs: ["bar.c"],
4180 },
4181 shared: {
4182 srcs: ["baz.c"],
4183 },
4184 }
4185
4186 cc_library_static {
4187 name: "libstatic",
4188 defaults: ["defaults"],
4189 }
4190
4191 cc_library_shared {
4192 name: "libshared",
4193 defaults: ["defaults"],
4194 }
4195
4196 cc_library {
4197 name: "libboth",
4198 defaults: ["defaults"],
4199 }
4200
4201 cc_binary {
4202 name: "binary",
4203 defaults: ["defaults"],
4204 }`)
4205
4206 pathsToBase := func(paths android.Paths) []string {
4207 var ret []string
4208 for _, p := range paths {
4209 ret = append(ret, p.Base())
4210 }
4211 return ret
4212 }
4213
Colin Cross7113d202019-11-20 16:39:12 -08004214 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004215 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4216 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
4217 }
Colin Cross7113d202019-11-20 16:39:12 -08004218 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004219 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
4220 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
4221 }
Colin Cross7113d202019-11-20 16:39:12 -08004222 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004223 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
4224 t.Errorf("binary ld rule wanted %q, got %q", w, g)
4225 }
4226
Colin Cross7113d202019-11-20 16:39:12 -08004227 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004228 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4229 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
4230 }
Colin Cross7113d202019-11-20 16:39:12 -08004231 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07004232 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
4233 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
4234 }
4235}
Colin Crosseabaedd2020-02-06 17:01:55 -08004236
4237func TestProductVariableDefaults(t *testing.T) {
4238 bp := `
4239 cc_defaults {
4240 name: "libfoo_defaults",
4241 srcs: ["foo.c"],
4242 cppflags: ["-DFOO"],
4243 product_variables: {
4244 debuggable: {
4245 cppflags: ["-DBAR"],
4246 },
4247 },
4248 }
4249
4250 cc_library {
4251 name: "libfoo",
4252 defaults: ["libfoo_defaults"],
4253 }
4254 `
4255
4256 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4257 config.TestProductVariables.Debuggable = BoolPtr(true)
4258
Colin Crossae8600b2020-10-29 17:09:13 -07004259 ctx := CreateTestContext(config)
Colin Crosseabaedd2020-02-06 17:01:55 -08004260 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
4261 ctx.BottomUp("variable", android.VariableMutator).Parallel()
4262 })
Colin Crossae8600b2020-10-29 17:09:13 -07004263 ctx.Register()
Colin Crosseabaedd2020-02-06 17:01:55 -08004264
4265 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4266 android.FailIfErrored(t, errs)
4267 _, errs = ctx.PrepareBuildActions(config)
4268 android.FailIfErrored(t, errs)
4269
4270 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
4271 if !android.InList("-DBAR", libfoo.flags.Local.CppFlags) {
4272 t.Errorf("expected -DBAR in cppflags, got %q", libfoo.flags.Local.CppFlags)
4273 }
4274}
Colin Crosse4f6eba2020-09-22 18:11:25 -07004275
4276func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
4277 t.Parallel()
4278 bp := `
4279 cc_library_static {
4280 name: "libfoo",
4281 srcs: ["foo.c"],
4282 whole_static_libs: ["libbar"],
4283 }
4284
4285 cc_library_static {
4286 name: "libbar",
4287 whole_static_libs: ["libmissing"],
4288 }
4289 `
4290
4291 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4292 config.TestProductVariables.Allow_missing_dependencies = BoolPtr(true)
4293
Colin Crossae8600b2020-10-29 17:09:13 -07004294 ctx := CreateTestContext(config)
Colin Crosse4f6eba2020-09-22 18:11:25 -07004295 ctx.SetAllowMissingDependencies(true)
Colin Crossae8600b2020-10-29 17:09:13 -07004296 ctx.Register()
Colin Crosse4f6eba2020-09-22 18:11:25 -07004297
4298 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
4299 android.FailIfErrored(t, errs)
4300 _, errs = ctx.PrepareBuildActions(config)
4301 android.FailIfErrored(t, errs)
4302
4303 libbar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
4304 if g, w := libbar.Rule, android.ErrorRule; g != w {
4305 t.Fatalf("Expected libbar rule to be %q, got %q", w, g)
4306 }
4307
4308 if g, w := libbar.Args["error"], "missing dependencies: libmissing"; !strings.Contains(g, w) {
4309 t.Errorf("Expected libbar error to contain %q, was %q", w, g)
4310 }
4311
4312 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
4313 if g, w := libfoo.Inputs.Strings(), libbar.Output.String(); !android.InList(w, g) {
4314 t.Errorf("Expected libfoo.a to depend on %q, got %q", w, g)
4315 }
4316
4317}
Colin Crosse9fe2942020-11-10 18:12:15 -08004318
4319func TestInstallSharedLibs(t *testing.T) {
4320 bp := `
4321 cc_binary {
4322 name: "bin",
4323 host_supported: true,
4324 shared_libs: ["libshared"],
4325 runtime_libs: ["libruntime"],
4326 srcs: [":gen"],
4327 }
4328
4329 cc_library_shared {
4330 name: "libshared",
4331 host_supported: true,
4332 shared_libs: ["libtransitive"],
4333 }
4334
4335 cc_library_shared {
4336 name: "libtransitive",
4337 host_supported: true,
4338 }
4339
4340 cc_library_shared {
4341 name: "libruntime",
4342 host_supported: true,
4343 }
4344
4345 cc_binary_host {
4346 name: "tool",
4347 srcs: ["foo.cpp"],
4348 }
4349
4350 genrule {
4351 name: "gen",
4352 tools: ["tool"],
4353 out: ["gen.cpp"],
4354 cmd: "$(location tool) $(out)",
4355 }
4356 `
4357
4358 config := TestConfig(buildDir, android.Android, nil, bp, nil)
4359 ctx := testCcWithConfig(t, config)
4360
4361 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
4362 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
4363 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
4364 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
4365 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
4366
4367 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
4368 t.Errorf("expected host bin dependency %q, got %q", w, g)
4369 }
4370
4371 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4372 t.Errorf("expected host bin dependency %q, got %q", w, g)
4373 }
4374
4375 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
4376 t.Errorf("expected host bin dependency %q, got %q", w, g)
4377 }
4378
4379 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
4380 t.Errorf("expected host bin dependency %q, got %q", w, g)
4381 }
4382
4383 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
4384 t.Errorf("expected no host bin dependency %q, got %q", w, g)
4385 }
4386
4387 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
4388 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
4389 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
4390 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
4391
4392 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
4393 t.Errorf("expected device bin dependency %q, got %q", w, g)
4394 }
4395
4396 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4397 t.Errorf("expected device bin dependency %q, got %q", w, g)
4398 }
4399
4400 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
4401 t.Errorf("expected device bin dependency %q, got %q", w, g)
4402 }
4403
4404 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
4405 t.Errorf("expected device bin dependency %q, got %q", w, g)
4406 }
4407
4408 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
4409 t.Errorf("expected no device bin dependency %q, got %q", w, g)
4410 }
4411
4412}
Jiyong Park1ad8e162020-12-01 23:40:09 +09004413
4414func TestStubsLibReexportsHeaders(t *testing.T) {
4415 ctx := testCc(t, `
4416 cc_library_shared {
4417 name: "libclient",
4418 srcs: ["foo.c"],
4419 shared_libs: ["libfoo#1"],
4420 }
4421
4422 cc_library_shared {
4423 name: "libfoo",
4424 srcs: ["foo.c"],
4425 shared_libs: ["libbar"],
4426 export_shared_lib_headers: ["libbar"],
4427 stubs: {
4428 symbol_file: "foo.map.txt",
4429 versions: ["1", "2", "3"],
4430 },
4431 }
4432
4433 cc_library_shared {
4434 name: "libbar",
4435 export_include_dirs: ["include/libbar"],
4436 srcs: ["foo.c"],
4437 }`)
4438
4439 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4440
4441 if !strings.Contains(cFlags, "-Iinclude/libbar") {
4442 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
4443 }
4444}
Jooyung Hane197d8b2021-01-05 10:33:16 +09004445
4446func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
4447 ctx := testCc(t, `
4448 cc_library {
4449 name: "libfoo",
4450 srcs: ["a/Foo.aidl"],
4451 aidl: { flags: ["-Werror"], },
4452 }
4453 `)
4454
4455 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
4456 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
4457 aidlCommand := manifest.Commands[0].GetCommand()
4458 expectedAidlFlag := "-Werror"
4459 if !strings.Contains(aidlCommand, expectedAidlFlag) {
4460 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
4461 }
4462}