blob: c00dfb46cfc1ea1cb36d8aca1bce0a39ec3ffb45 [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 "os"
Inseob Kim1f086e22019-05-09 13:29:15 +090020 "path/filepath"
Colin Cross74d1ec02015-04-28 13:30:13 -070021 "reflect"
Paul Duffin3cb603e2021-02-19 13:57:10 +000022 "regexp"
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 +090029func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000030 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090031}
32
Paul Duffin02a3d652021-02-24 18:51:54 +000033var ccFixtureFactory = android.NewFixtureFactory(
Paul Duffinc3e6ce02021-03-22 23:21:32 +000034 nil,
Paul Duffin2e6f90e2021-03-22 23:20:25 +000035 prepareForCcTest,
36)
37
38var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000039 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000040 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
41 variables.DeviceVndkVersion = StringPtr("current")
42 variables.ProductVndkVersion = StringPtr("current")
43 variables.Platform_vndk_version = StringPtr("VER")
44 }),
45)
46
47// testCcWithConfig runs tests using the ccFixtureFactory
48//
49// See testCc for an explanation as to how to stop using this deprecated method.
50//
51// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080052func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070053 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000054 result := ccFixtureFactory.RunTestWithConfig(t, config)
55 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090056}
57
Paul Duffin02a3d652021-02-24 18:51:54 +000058// testCc runs tests using the ccFixtureFactory
59//
60// Do not add any new usages of this, instead use the ccFixtureFactory directly as it makes it much
61// easier to customize the test behavior.
62//
63// If it is necessary to customize the behavior of an existing test that uses this then please first
64// convert the test to using ccFixtureFactory first and then in a following change add the
65// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
66// that it did not change the test behavior unexpectedly.
67//
68// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080069func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080070 t.Helper()
Paul Duffin02a3d652021-02-24 18:51:54 +000071 result := ccFixtureFactory.RunTestWithBp(t, bp)
72 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080073}
74
Paul Duffin02a3d652021-02-24 18:51:54 +000075// testCcNoVndk runs tests using the ccFixtureFactory
76//
77// See testCc for an explanation as to how to stop using this deprecated method.
78//
79// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080080func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080081 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000082 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -070083 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
Logan Chienf3511742017-10-31 18:04:35 +080084
Colin Cross98be1bb2019-12-13 20:41:13 -080085 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080086}
87
Paul Duffin02a3d652021-02-24 18:51:54 +000088// testCcNoProductVndk runs tests using the ccFixtureFactory
89//
90// See testCc for an explanation as to how to stop using this deprecated method.
91//
92// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090093func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
94 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000095 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090096 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
97 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
98
99 return testCcWithConfig(t, config)
100}
101
Paul Duffin02a3d652021-02-24 18:51:54 +0000102// testCcErrorWithConfig runs tests using the ccFixtureFactory
103//
104// See testCc for an explanation as to how to stop using this deprecated method.
105//
106// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900107func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800108 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800109
Paul Duffin02a3d652021-02-24 18:51:54 +0000110 ccFixtureFactory.Extend().
111 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
112 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800113}
114
Paul Duffin02a3d652021-02-24 18:51:54 +0000115// testCcError runs tests using the ccFixtureFactory
116//
117// See testCc for an explanation as to how to stop using this deprecated method.
118//
119// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900121 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000122 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900123 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
124 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
125 testCcErrorWithConfig(t, pattern, config)
126 return
127}
128
Paul Duffin02a3d652021-02-24 18:51:54 +0000129// testCcErrorProductVndk runs tests using the ccFixtureFactory
130//
131// See testCc for an explanation as to how to stop using this deprecated method.
132//
133// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900134func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900135 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000136 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900137 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
138 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
139 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
140 testCcErrorWithConfig(t, pattern, config)
141 return
142}
143
Logan Chienf3511742017-10-31 18:04:35 +0800144const (
Colin Cross7113d202019-11-20 16:39:12 -0800145 coreVariant = "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800146 vendorVariant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun5f7f7e82019-11-18 19:52:14 +0900147 productVariant = "android_product.VER_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800148 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800149)
150
Paul Duffindb462dd2021-03-21 22:01:55 +0000151// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
152// running it in a fixture that requires all source files to exist.
153func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
154 android.GroupFixturePreparers(
155 PrepareForTestWithCcDefaultModules,
156 android.PrepareForTestDisallowNonExistentPaths,
157 ).RunTest(t)
158}
159
Doug Hornc32c6b02019-01-17 14:44:05 -0800160func TestFuchsiaDeps(t *testing.T) {
161 t.Helper()
162
163 bp := `
164 cc_library {
165 name: "libTest",
166 srcs: ["foo.c"],
167 target: {
168 fuchsia: {
169 srcs: ["bar.c"],
170 },
171 },
172 }`
173
Paul Duffinecdac8a2021-02-24 19:18:42 +0000174 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
Doug Hornc32c6b02019-01-17 14:44:05 -0800175
176 rt := false
177 fb := false
178
Paul Duffinecdac8a2021-02-24 19:18:42 +0000179 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800180 implicits := ld.Implicits
181 for _, lib := range implicits {
182 if strings.Contains(lib.Rel(), "libcompiler_rt") {
183 rt = true
184 }
185
186 if strings.Contains(lib.Rel(), "libbioniccompat") {
187 fb = true
188 }
189 }
190
191 if !rt || !fb {
192 t.Errorf("fuchsia libs must link libcompiler_rt and libbioniccompat")
193 }
194}
195
196func TestFuchsiaTargetDecl(t *testing.T) {
197 t.Helper()
198
199 bp := `
200 cc_library {
201 name: "libTest",
202 srcs: ["foo.c"],
203 target: {
204 fuchsia: {
205 srcs: ["bar.c"],
206 },
207 },
208 }`
209
Paul Duffinecdac8a2021-02-24 19:18:42 +0000210 result := ccFixtureFactory.Extend(PrepareForTestOnFuchsia).RunTestWithBp(t, bp)
211 ld := result.ModuleForTests("libTest", "fuchsia_arm64_shared").Rule("ld")
Doug Hornc32c6b02019-01-17 14:44:05 -0800212 var objs []string
213 for _, o := range ld.Inputs {
214 objs = append(objs, o.Base())
215 }
Paul Duffine84b1332021-03-12 11:59:43 +0000216 android.AssertArrayString(t, "libTest inputs", []string{"foo.o", "bar.o"}, objs)
Doug Hornc32c6b02019-01-17 14:44:05 -0800217}
218
Jiyong Park6a43f042017-10-12 23:05:00 +0900219func TestVendorSrc(t *testing.T) {
220 ctx := testCc(t, `
221 cc_library {
222 name: "libTest",
223 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700224 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800225 nocrt: true,
226 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900227 vendor_available: true,
228 target: {
229 vendor: {
230 srcs: ["bar.c"],
231 },
232 },
233 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900234 `)
235
Logan Chienf3511742017-10-31 18:04:35 +0800236 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900237 var objs []string
238 for _, o := range ld.Inputs {
239 objs = append(objs, o.Base())
240 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800241 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900242 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
243 }
244}
245
Logan Chienf3511742017-10-31 18:04:35 +0800246func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900247 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800248
Logan Chiend3c59a22018-03-29 14:08:15 +0800249 t.Helper()
250
Justin Yun0ecf0b22020-02-28 15:07:59 +0900251 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800252
253 // Check library properties.
254 lib, ok := mod.compiler.(*libraryDecorator)
255 if !ok {
256 t.Errorf("%q must have libraryDecorator", name)
257 } else if lib.baseInstaller.subDir != subDir {
258 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
259 lib.baseInstaller.subDir)
260 }
261
262 // Check VNDK properties.
263 if mod.vndkdep == nil {
264 t.Fatalf("%q must have `vndkdep`", name)
265 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700266 if !mod.IsVndk() {
267 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800268 }
269 if mod.isVndkSp() != isVndkSp {
270 t.Errorf("%q isVndkSp() must equal to %t", name, isVndkSp)
271 }
272
273 // Check VNDK extension properties.
274 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500275 if mod.IsVndkExt() != isVndkExt {
276 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800277 }
278
279 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
280 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
281 }
282}
283
Jose Galmes0a942a02021-02-03 14:23:15 -0800284func checkSnapshotIncludeExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string, include bool, fake bool) {
Bill Peckham945441c2020-08-31 16:07:58 -0700285 t.Helper()
Paul Duffine8366da2021-03-24 10:40:38 +0000286 mod := ctx.ModuleForTests(moduleName, variant)
287 outputFiles := mod.OutputFiles(t, "")
288 if len(outputFiles) != 1 {
Jooyung Han39edb6c2019-11-06 16:53:07 +0900289 t.Errorf("%q must have single output\n", moduleName)
290 return
291 }
292 snapshotPath := filepath.Join(subDir, snapshotFilename)
Inseob Kim1f086e22019-05-09 13:29:15 +0900293
Bill Peckham945441c2020-08-31 16:07:58 -0700294 if include {
295 out := singleton.Output(snapshotPath)
Jose Galmes0a942a02021-02-03 14:23:15 -0800296 if fake {
297 if out.Rule == nil {
298 t.Errorf("Missing rule for module %q output file %q", moduleName, outputFiles[0])
299 }
300 } else {
301 if out.Input.String() != outputFiles[0].String() {
302 t.Errorf("The input of snapshot %q must be %q, but %q", moduleName, out.Input.String(), outputFiles[0])
303 }
Bill Peckham945441c2020-08-31 16:07:58 -0700304 }
305 } else {
306 out := singleton.MaybeOutput(snapshotPath)
307 if out.Rule != nil {
308 t.Errorf("There must be no rule for module %q output file %q", moduleName, outputFiles[0])
309 }
Inseob Kim1f086e22019-05-09 13:29:15 +0900310 }
311}
312
Bill Peckham945441c2020-08-31 16:07:58 -0700313func checkSnapshot(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000314 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800315 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, false)
Bill Peckham945441c2020-08-31 16:07:58 -0700316}
317
318func checkSnapshotExclude(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000319 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800320 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, false, false)
321}
322
323func checkSnapshotRule(t *testing.T, ctx *android.TestContext, singleton android.TestingSingleton, moduleName, snapshotFilename, subDir, variant string) {
Paul Duffine8366da2021-03-24 10:40:38 +0000324 t.Helper()
Jose Galmes0a942a02021-02-03 14:23:15 -0800325 checkSnapshotIncludeExclude(t, ctx, singleton, moduleName, snapshotFilename, subDir, variant, true, true)
Bill Peckham945441c2020-08-31 16:07:58 -0700326}
327
Jooyung Han2216fb12019-11-06 16:46:15 +0900328func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
329 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800330 content := android.ContentFromFileRuleForTests(t, params)
331 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900332 assertArrayString(t, actual, expected)
333}
334
Jooyung Han097087b2019-10-22 19:32:18 +0900335func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
336 t.Helper()
337 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900338 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
339}
340
341func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
342 t.Helper()
Colin Cross78212242021-01-06 14:51:30 -0800343 got := ctx.ModuleForTests(module, "").Module().(*vndkLibrariesTxt).fileNames
344 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900345}
346
Logan Chienf3511742017-10-31 18:04:35 +0800347func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800348 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800349 cc_library {
350 name: "libvndk",
351 vendor_available: true,
352 vndk: {
353 enabled: true,
354 },
355 nocrt: true,
356 }
357
358 cc_library {
359 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900360 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800361 vndk: {
362 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900363 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800364 },
365 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900366 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800367 }
368
369 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900370 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800371 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900372 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800373 vndk: {
374 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900375 },
376 nocrt: true,
377 target: {
378 vendor: {
379 cflags: ["-DTEST"],
380 },
381 product: {
382 cflags: ["-DTEST"],
383 },
384 },
385 }
386
387 cc_library {
388 name: "libvndk_sp",
389 vendor_available: true,
390 vndk: {
391 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800392 support_system_process: true,
393 },
394 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900395 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800396 }
397
398 cc_library {
399 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900400 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800401 vndk: {
402 enabled: true,
403 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900404 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800405 },
406 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900407 target: {
408 vendor: {
409 suffix: "-x",
410 },
411 },
Logan Chienf3511742017-10-31 18:04:35 +0800412 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900413
414 cc_library {
415 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900416 vendor_available: true,
417 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900418 vndk: {
419 enabled: true,
420 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900421 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900422 },
423 nocrt: true,
424 target: {
425 vendor: {
426 suffix: "-x",
427 },
428 product: {
429 suffix: "-x",
430 },
431 },
432 }
433
Colin Crosse4e44bc2020-12-28 13:50:21 -0800434 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900435 name: "llndk.libraries.txt",
436 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800437 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900438 name: "vndkcore.libraries.txt",
439 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800440 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900441 name: "vndksp.libraries.txt",
442 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800443 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900444 name: "vndkprivate.libraries.txt",
445 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800446 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900447 name: "vndkproduct.libraries.txt",
448 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800449 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900450 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800451 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900452 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800453 `
454
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000455 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800456 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900457 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Colin Cross98be1bb2019-12-13 20:41:13 -0800458 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
459
460 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800461
Jooyung Han261e1582020-10-20 18:54:21 +0900462 // subdir == "" because VNDK libs are not supposed to be installed separately.
463 // They are installed as part of VNDK APEX instead.
464 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
465 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900466 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900467 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
468 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900469 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900470
Justin Yun6977e8a2020-10-29 18:24:11 +0900471 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
472 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900473
Inseob Kim1f086e22019-05-09 13:29:15 +0900474 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900475 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000476 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900477
478 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
479 "arm64", "armv8-a"))
480 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
481 "arm", "armv7-a-neon"))
482
483 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
484 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
485 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
486 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
487
Colin Crossfb0c16e2019-11-20 17:12:35 -0800488 variant := "android_vendor.VER_arm64_armv8-a_shared"
489 variant2nd := "android_vendor.VER_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900490
Inseob Kim7f283f42020-06-01 21:53:49 +0900491 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
492
493 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
494 checkSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
Justin Yun6977e8a2020-10-29 18:24:11 +0900495 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
496 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
Inseob Kim7f283f42020-06-01 21:53:49 +0900497 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
498 checkSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900499
Jooyung Han39edb6c2019-11-06 16:53:07 +0900500 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Inseob Kim7f283f42020-06-01 21:53:49 +0900501 checkSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "")
502 checkSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "")
503 checkSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "")
504 checkSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "")
Justin Yun8a2600c2020-12-07 12:44:03 +0900505 checkSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900506
Jooyung Han097087b2019-10-22 19:32:18 +0900507 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
508 "LLNDK: libc.so",
509 "LLNDK: libdl.so",
510 "LLNDK: libft2.so",
511 "LLNDK: libm.so",
512 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900513 "VNDK-SP: libvndk_sp-x.so",
514 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900515 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900516 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900517 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900518 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900519 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900520 "VNDK-private: libvndk-private.so",
521 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900522 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900523 "VNDK-product: libc++.so",
524 "VNDK-product: libvndk_product.so",
525 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900526 })
Jooyung Han2216fb12019-11-06 16:46:15 +0900527 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libdl.so", "libft2.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900528 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
529 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
530 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 +0900531 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 +0900532 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
533}
534
Yo Chiangbba545e2020-06-09 16:15:37 +0800535func TestVndkWithHostSupported(t *testing.T) {
536 ctx := testCc(t, `
537 cc_library {
538 name: "libvndk_host_supported",
539 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900540 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800541 vndk: {
542 enabled: true,
543 },
544 host_supported: true,
545 }
546
547 cc_library {
548 name: "libvndk_host_supported_but_disabled_on_device",
549 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900550 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800551 vndk: {
552 enabled: true,
553 },
554 host_supported: true,
555 enabled: false,
556 target: {
557 host: {
558 enabled: true,
559 }
560 }
561 }
562
Colin Crosse4e44bc2020-12-28 13:50:21 -0800563 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800564 name: "vndkcore.libraries.txt",
565 }
566 `)
567
568 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
569}
570
Jooyung Han2216fb12019-11-06 16:46:15 +0900571func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800572 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800573 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900574 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800575 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800576 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000577 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800578 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
579 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
580 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900581
582 module := ctx.ModuleForTests("llndk.libraries.txt", "")
Colin Crossaa255532020-07-03 13:18:24 -0700583 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jooyung Han2216fb12019-11-06 16:46:15 +0900584 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.VER.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900585}
586
587func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800588 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900589 cc_library {
590 name: "libvndk",
591 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900592 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900593 vndk: {
594 enabled: true,
595 },
596 nocrt: true,
597 }
598
599 cc_library {
600 name: "libvndk_sp",
601 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900602 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900603 vndk: {
604 enabled: true,
605 support_system_process: true,
606 },
607 nocrt: true,
608 }
609
610 cc_library {
611 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900612 vendor_available: true,
613 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900614 vndk: {
615 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900616 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900617 },
618 nocrt: true,
619 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900620
Colin Crosse4e44bc2020-12-28 13:50:21 -0800621 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900622 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800623 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900624 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800625 `
626
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000627 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800628 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
629 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
630 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
631
632 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
633
634 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900635
Jooyung Han2216fb12019-11-06 16:46:15 +0900636 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900637}
638
Chris Parsons79d66a52020-06-05 17:26:16 -0400639func TestDataLibs(t *testing.T) {
640 bp := `
641 cc_test_library {
642 name: "test_lib",
643 srcs: ["test_lib.cpp"],
644 gtest: false,
645 }
646
647 cc_test {
648 name: "main_test",
649 data_libs: ["test_lib"],
650 gtest: false,
651 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400652 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400653
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000654 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400655 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
656 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
657 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
658
659 ctx := testCcWithConfig(t, config)
660 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
661 testBinary := module.(*Module).linker.(*testBinary)
662 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
663 if err != nil {
664 t.Errorf("Expected cc_test to produce output files, error: %s", err)
665 return
666 }
667 if len(outputFiles) != 1 {
668 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
669 return
670 }
671 if len(testBinary.dataPaths()) != 1 {
672 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
673 return
674 }
675
676 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400677 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400678
679 if !strings.HasSuffix(outputPath, "/main_test") {
680 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
681 return
682 }
683 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
684 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
685 return
686 }
687}
688
Chris Parsons216e10a2020-07-09 17:12:52 -0400689func TestDataLibsRelativeInstallPath(t *testing.T) {
690 bp := `
691 cc_test_library {
692 name: "test_lib",
693 srcs: ["test_lib.cpp"],
694 relative_install_path: "foo/bar/baz",
695 gtest: false,
696 }
697
698 cc_test {
699 name: "main_test",
700 data_libs: ["test_lib"],
701 gtest: false,
702 }
703 `
704
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000705 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400706 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
707 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
708 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
709
710 ctx := testCcWithConfig(t, config)
711 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
712 testBinary := module.(*Module).linker.(*testBinary)
713 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
714 if err != nil {
715 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
716 }
717 if len(outputFiles) != 1 {
718 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
719 }
720 if len(testBinary.dataPaths()) != 1 {
721 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
722 }
723
724 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400725
726 if !strings.HasSuffix(outputPath, "/main_test") {
727 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
728 }
Colin Crossaa255532020-07-03 13:18:24 -0700729 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400730 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
731 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400732 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400733 }
734}
735
Jooyung Han0302a842019-10-30 18:43:49 +0900736func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900737 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900738 cc_library {
739 name: "libvndk",
740 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900741 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900742 vndk: {
743 enabled: true,
744 },
745 nocrt: true,
746 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900747 cc_library {
748 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900749 vendor_available: true,
750 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900751 vndk: {
752 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900753 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900754 },
755 nocrt: true,
756 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800757
758 cc_library {
759 name: "libllndk",
760 llndk_stubs: "libllndk.llndk",
761 }
762
763 llndk_library {
764 name: "libllndk.llndk",
765 symbol_file: "",
766 export_llndk_headers: ["libllndk_headers"],
767 }
768
769 llndk_headers {
770 name: "libllndk_headers",
771 export_include_dirs: ["include"],
772 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900773 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900774
775 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
776 "LLNDK: libc.so",
777 "LLNDK: libdl.so",
778 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800779 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900780 "LLNDK: libm.so",
781 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900782 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900783 "VNDK-core: libvndk.so",
784 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900785 "VNDK-private: libvndk-private.so",
786 "VNDK-product: libc++.so",
787 "VNDK-product: libvndk-private.so",
788 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900789 })
Logan Chienf3511742017-10-31 18:04:35 +0800790}
791
Justin Yun63e9ec72020-10-29 16:49:43 +0900792func TestVndkModuleError(t *testing.T) {
793 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900794 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900795 cc_library {
796 name: "libvndk",
797 vndk: {
798 enabled: true,
799 },
800 nocrt: true,
801 }
802 `)
803
Justin Yunc0d8c492021-01-07 17:45:31 +0900804 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900805 cc_library {
806 name: "libvndk",
807 product_available: true,
808 vndk: {
809 enabled: true,
810 },
811 nocrt: true,
812 }
813 `)
814
Justin Yun6977e8a2020-10-29 18:24:11 +0900815 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
816 cc_library {
817 name: "libvndkprop",
818 vendor_available: true,
819 product_available: true,
820 vndk: {
821 enabled: true,
822 },
823 nocrt: true,
824 target: {
825 vendor: {
826 cflags: ["-DTEST",],
827 },
828 },
829 }
830 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900831}
832
Logan Chiend3c59a22018-03-29 14:08:15 +0800833func TestVndkDepError(t *testing.T) {
834 // Check whether an error is emitted when a VNDK lib depends on a system lib.
835 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
836 cc_library {
837 name: "libvndk",
838 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900839 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800840 vndk: {
841 enabled: true,
842 },
843 shared_libs: ["libfwk"], // Cause error
844 nocrt: true,
845 }
846
847 cc_library {
848 name: "libfwk",
849 nocrt: true,
850 }
851 `)
852
853 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
854 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
855 cc_library {
856 name: "libvndk",
857 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900858 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800859 vndk: {
860 enabled: true,
861 },
862 shared_libs: ["libvendor"], // Cause error
863 nocrt: true,
864 }
865
866 cc_library {
867 name: "libvendor",
868 vendor: true,
869 nocrt: true,
870 }
871 `)
872
873 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
874 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
875 cc_library {
876 name: "libvndk_sp",
877 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900878 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800879 vndk: {
880 enabled: true,
881 support_system_process: true,
882 },
883 shared_libs: ["libfwk"], // Cause error
884 nocrt: true,
885 }
886
887 cc_library {
888 name: "libfwk",
889 nocrt: true,
890 }
891 `)
892
893 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
894 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
895 cc_library {
896 name: "libvndk_sp",
897 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900898 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800899 vndk: {
900 enabled: true,
901 support_system_process: true,
902 },
903 shared_libs: ["libvendor"], // Cause error
904 nocrt: true,
905 }
906
907 cc_library {
908 name: "libvendor",
909 vendor: true,
910 nocrt: true,
911 }
912 `)
913
914 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
915 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
916 cc_library {
917 name: "libvndk_sp",
918 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900919 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800920 vndk: {
921 enabled: true,
922 support_system_process: true,
923 },
924 shared_libs: ["libvndk"], // Cause error
925 nocrt: true,
926 }
927
928 cc_library {
929 name: "libvndk",
930 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900931 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800932 vndk: {
933 enabled: true,
934 },
935 nocrt: true,
936 }
937 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900938
939 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
940 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
941 cc_library {
942 name: "libvndk",
943 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900944 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900945 vndk: {
946 enabled: true,
947 },
948 shared_libs: ["libnonvndk"],
949 nocrt: true,
950 }
951
952 cc_library {
953 name: "libnonvndk",
954 vendor_available: true,
955 nocrt: true,
956 }
957 `)
958
959 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
960 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
961 cc_library {
962 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900963 vendor_available: true,
964 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900965 vndk: {
966 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900967 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900968 },
969 shared_libs: ["libnonvndk"],
970 nocrt: true,
971 }
972
973 cc_library {
974 name: "libnonvndk",
975 vendor_available: true,
976 nocrt: true,
977 }
978 `)
979
980 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
981 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
982 cc_library {
983 name: "libvndksp",
984 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900985 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900986 vndk: {
987 enabled: true,
988 support_system_process: true,
989 },
990 shared_libs: ["libnonvndk"],
991 nocrt: true,
992 }
993
994 cc_library {
995 name: "libnonvndk",
996 vendor_available: true,
997 nocrt: true,
998 }
999 `)
1000
1001 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1002 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1003 cc_library {
1004 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001005 vendor_available: true,
1006 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001007 vndk: {
1008 enabled: true,
1009 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001010 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001011 },
1012 shared_libs: ["libnonvndk"],
1013 nocrt: true,
1014 }
1015
1016 cc_library {
1017 name: "libnonvndk",
1018 vendor_available: true,
1019 nocrt: true,
1020 }
1021 `)
1022}
1023
1024func TestDoubleLoadbleDep(t *testing.T) {
1025 // okay to link : LLNDK -> double_loadable VNDK
1026 testCc(t, `
1027 cc_library {
1028 name: "libllndk",
1029 shared_libs: ["libdoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001030 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001031 }
1032
1033 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001034 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001035 symbol_file: "",
1036 }
1037
1038 cc_library {
1039 name: "libdoubleloadable",
1040 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001041 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001042 vndk: {
1043 enabled: true,
1044 },
1045 double_loadable: true,
1046 }
1047 `)
1048 // okay to link : LLNDK -> VNDK-SP
1049 testCc(t, `
1050 cc_library {
1051 name: "libllndk",
1052 shared_libs: ["libvndksp"],
Colin Cross0477b422020-10-13 18:43:54 -07001053 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001054 }
1055
1056 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001057 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001058 symbol_file: "",
1059 }
1060
1061 cc_library {
1062 name: "libvndksp",
1063 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001064 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001065 vndk: {
1066 enabled: true,
1067 support_system_process: true,
1068 },
1069 }
1070 `)
1071 // okay to link : double_loadable -> double_loadable
1072 testCc(t, `
1073 cc_library {
1074 name: "libdoubleloadable1",
1075 shared_libs: ["libdoubleloadable2"],
1076 vendor_available: true,
1077 double_loadable: true,
1078 }
1079
1080 cc_library {
1081 name: "libdoubleloadable2",
1082 vendor_available: true,
1083 double_loadable: true,
1084 }
1085 `)
1086 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1087 testCc(t, `
1088 cc_library {
1089 name: "libdoubleloadable",
1090 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001091 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001092 vndk: {
1093 enabled: true,
1094 },
1095 double_loadable: true,
1096 shared_libs: ["libnondoubleloadable"],
1097 }
1098
1099 cc_library {
1100 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001101 vendor_available: true,
1102 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001103 vndk: {
1104 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001105 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001106 },
1107 double_loadable: true,
1108 }
1109 `)
1110 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1111 testCc(t, `
1112 cc_library {
1113 name: "libllndk",
1114 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001115 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001116 }
1117
1118 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001119 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001120 symbol_file: "",
1121 }
1122
1123 cc_library {
1124 name: "libcoreonly",
1125 shared_libs: ["libvendoravailable"],
1126 }
1127
1128 // indirect dependency of LLNDK
1129 cc_library {
1130 name: "libvendoravailable",
1131 vendor_available: true,
1132 double_loadable: true,
1133 }
1134 `)
1135}
1136
1137func TestDoubleLoadableDepError(t *testing.T) {
1138 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1139 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1140 cc_library {
1141 name: "libllndk",
1142 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001143 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001144 }
1145
1146 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001147 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 symbol_file: "",
1149 }
1150
1151 cc_library {
1152 name: "libnondoubleloadable",
1153 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001154 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001155 vndk: {
1156 enabled: true,
1157 },
1158 }
1159 `)
1160
1161 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1162 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1163 cc_library {
1164 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001165 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001166 shared_libs: ["libnondoubleloadable"],
Colin Cross0477b422020-10-13 18:43:54 -07001167 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001168 }
1169
1170 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001171 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001172 symbol_file: "",
1173 }
1174
1175 cc_library {
1176 name: "libnondoubleloadable",
1177 vendor_available: true,
1178 }
1179 `)
1180
Jooyung Hana70f0672019-01-18 15:20:43 +09001181 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1182 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1183 cc_library {
1184 name: "libllndk",
1185 shared_libs: ["libcoreonly"],
Colin Cross0477b422020-10-13 18:43:54 -07001186 llndk_stubs: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001187 }
1188
1189 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07001190 name: "libllndk.llndk",
Jooyung Hana70f0672019-01-18 15:20:43 +09001191 symbol_file: "",
1192 }
1193
1194 cc_library {
1195 name: "libcoreonly",
1196 shared_libs: ["libvendoravailable"],
1197 }
1198
1199 // indirect dependency of LLNDK
1200 cc_library {
1201 name: "libvendoravailable",
1202 vendor_available: true,
1203 }
1204 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001205
1206 // The error is not from 'client' but from 'libllndk'
1207 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1208 cc_library {
1209 name: "client",
1210 vendor_available: true,
1211 double_loadable: true,
1212 shared_libs: ["libllndk"],
1213 }
1214 cc_library {
1215 name: "libllndk",
1216 shared_libs: ["libnondoubleloadable"],
1217 llndk_stubs: "libllndk.llndk",
1218 }
1219 llndk_library {
1220 name: "libllndk.llndk",
1221 symbol_file: "",
1222 }
1223 cc_library {
1224 name: "libnondoubleloadable",
1225 vendor_available: true,
1226 }
1227 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001228}
1229
Jooyung Han479ca172020-10-19 18:51:07 +09001230func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1231 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1232 cc_library {
1233 name: "libvndksp",
1234 shared_libs: ["libanothervndksp"],
1235 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001236 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001237 vndk: {
1238 enabled: true,
1239 support_system_process: true,
1240 }
1241 }
1242
1243 cc_library {
1244 name: "libllndk",
1245 shared_libs: ["libanothervndksp"],
1246 }
1247
1248 llndk_library {
1249 name: "libllndk",
1250 symbol_file: "",
1251 }
1252
1253 cc_library {
1254 name: "libanothervndksp",
1255 vendor_available: true,
1256 }
1257 `)
1258}
1259
Logan Chienf3511742017-10-31 18:04:35 +08001260func TestVndkExt(t *testing.T) {
1261 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001262 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001263 cc_library {
1264 name: "libvndk",
1265 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001266 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001267 vndk: {
1268 enabled: true,
1269 },
1270 nocrt: true,
1271 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001272 cc_library {
1273 name: "libvndk2",
1274 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001275 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001276 vndk: {
1277 enabled: true,
1278 },
1279 target: {
1280 vendor: {
1281 suffix: "-suffix",
1282 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001283 product: {
1284 suffix: "-suffix",
1285 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001286 },
1287 nocrt: true,
1288 }
Logan Chienf3511742017-10-31 18:04:35 +08001289
1290 cc_library {
1291 name: "libvndk_ext",
1292 vendor: true,
1293 vndk: {
1294 enabled: true,
1295 extends: "libvndk",
1296 },
1297 nocrt: true,
1298 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001299
1300 cc_library {
1301 name: "libvndk2_ext",
1302 vendor: true,
1303 vndk: {
1304 enabled: true,
1305 extends: "libvndk2",
1306 },
1307 nocrt: true,
1308 }
Logan Chienf3511742017-10-31 18:04:35 +08001309
Justin Yun0ecf0b22020-02-28 15:07:59 +09001310 cc_library {
1311 name: "libvndk_ext_product",
1312 product_specific: true,
1313 vndk: {
1314 enabled: true,
1315 extends: "libvndk",
1316 },
1317 nocrt: true,
1318 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001319
Justin Yun0ecf0b22020-02-28 15:07:59 +09001320 cc_library {
1321 name: "libvndk2_ext_product",
1322 product_specific: true,
1323 vndk: {
1324 enabled: true,
1325 extends: "libvndk2",
1326 },
1327 nocrt: true,
1328 }
1329 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001330 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001331 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1332 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1333 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1334
1335 ctx := testCcWithConfig(t, config)
1336
1337 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1338 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1339
1340 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1341 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1342
1343 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1344 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001345}
1346
Logan Chiend3c59a22018-03-29 14:08:15 +08001347func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001348 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1349 ctx := testCcNoVndk(t, `
1350 cc_library {
1351 name: "libvndk",
1352 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001353 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001354 vndk: {
1355 enabled: true,
1356 },
1357 nocrt: true,
1358 }
1359
1360 cc_library {
1361 name: "libvndk_ext",
1362 vendor: true,
1363 vndk: {
1364 enabled: true,
1365 extends: "libvndk",
1366 },
1367 nocrt: true,
1368 }
1369 `)
1370
1371 // Ensures that the core variant of "libvndk_ext" can be found.
1372 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1373 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1374 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1375 }
1376}
1377
Justin Yun0ecf0b22020-02-28 15:07:59 +09001378func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1379 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001380 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001381 cc_library {
1382 name: "libvndk",
1383 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001384 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001385 vndk: {
1386 enabled: true,
1387 },
1388 nocrt: true,
1389 }
1390
1391 cc_library {
1392 name: "libvndk_ext_product",
1393 product_specific: true,
1394 vndk: {
1395 enabled: true,
1396 extends: "libvndk",
1397 },
1398 nocrt: true,
1399 }
1400 `)
1401
1402 // Ensures that the core variant of "libvndk_ext_product" can be found.
1403 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1404 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1405 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1406 }
1407}
1408
Logan Chienf3511742017-10-31 18:04:35 +08001409func TestVndkExtError(t *testing.T) {
1410 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001411 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001412 cc_library {
1413 name: "libvndk",
1414 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001415 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001416 vndk: {
1417 enabled: true,
1418 },
1419 nocrt: true,
1420 }
1421
1422 cc_library {
1423 name: "libvndk_ext",
1424 vndk: {
1425 enabled: true,
1426 extends: "libvndk",
1427 },
1428 nocrt: true,
1429 }
1430 `)
1431
1432 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1433 cc_library {
1434 name: "libvndk",
1435 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001436 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001437 vndk: {
1438 enabled: true,
1439 },
1440 nocrt: true,
1441 }
1442
1443 cc_library {
1444 name: "libvndk_ext",
1445 vendor: true,
1446 vndk: {
1447 enabled: true,
1448 },
1449 nocrt: true,
1450 }
1451 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001452
1453 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1454 cc_library {
1455 name: "libvndk",
1456 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001457 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001458 vndk: {
1459 enabled: true,
1460 },
1461 nocrt: true,
1462 }
1463
1464 cc_library {
1465 name: "libvndk_ext_product",
1466 product_specific: true,
1467 vndk: {
1468 enabled: true,
1469 },
1470 nocrt: true,
1471 }
1472 `)
1473
1474 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1475 cc_library {
1476 name: "libvndk",
1477 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001478 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001479 vndk: {
1480 enabled: true,
1481 },
1482 nocrt: true,
1483 }
1484
1485 cc_library {
1486 name: "libvndk_ext_product",
1487 product_specific: true,
1488 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001489 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001490 vndk: {
1491 enabled: true,
1492 extends: "libvndk",
1493 },
1494 nocrt: true,
1495 }
1496 `)
Logan Chienf3511742017-10-31 18:04:35 +08001497}
1498
1499func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1500 // This test ensures an error is emitted for inconsistent support_system_process.
1501 testCcError(t, "module \".*\" with mismatched support_system_process", `
1502 cc_library {
1503 name: "libvndk",
1504 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001505 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001506 vndk: {
1507 enabled: true,
1508 },
1509 nocrt: true,
1510 }
1511
1512 cc_library {
1513 name: "libvndk_sp_ext",
1514 vendor: true,
1515 vndk: {
1516 enabled: true,
1517 extends: "libvndk",
1518 support_system_process: true,
1519 },
1520 nocrt: true,
1521 }
1522 `)
1523
1524 testCcError(t, "module \".*\" with mismatched support_system_process", `
1525 cc_library {
1526 name: "libvndk_sp",
1527 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001528 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001529 vndk: {
1530 enabled: true,
1531 support_system_process: true,
1532 },
1533 nocrt: true,
1534 }
1535
1536 cc_library {
1537 name: "libvndk_ext",
1538 vendor: true,
1539 vndk: {
1540 enabled: true,
1541 extends: "libvndk_sp",
1542 },
1543 nocrt: true,
1544 }
1545 `)
1546}
1547
1548func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001549 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001550 // with `private: true`.
1551 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001552 cc_library {
1553 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001554 vendor_available: true,
1555 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001556 vndk: {
1557 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001558 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001559 },
1560 nocrt: true,
1561 }
1562
1563 cc_library {
1564 name: "libvndk_ext",
1565 vendor: true,
1566 vndk: {
1567 enabled: true,
1568 extends: "libvndk",
1569 },
1570 nocrt: true,
1571 }
1572 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001573
Justin Yunfd9e8042020-12-23 18:23:14 +09001574 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001575 cc_library {
1576 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001577 vendor_available: true,
1578 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001579 vndk: {
1580 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001581 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001582 },
1583 nocrt: true,
1584 }
1585
1586 cc_library {
1587 name: "libvndk_ext_product",
1588 product_specific: true,
1589 vndk: {
1590 enabled: true,
1591 extends: "libvndk",
1592 },
1593 nocrt: true,
1594 }
1595 `)
Logan Chienf3511742017-10-31 18:04:35 +08001596}
1597
Logan Chiend3c59a22018-03-29 14:08:15 +08001598func TestVendorModuleUseVndkExt(t *testing.T) {
1599 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001600 testCc(t, `
1601 cc_library {
1602 name: "libvndk",
1603 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001604 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001605 vndk: {
1606 enabled: true,
1607 },
1608 nocrt: true,
1609 }
1610
1611 cc_library {
1612 name: "libvndk_ext",
1613 vendor: true,
1614 vndk: {
1615 enabled: true,
1616 extends: "libvndk",
1617 },
1618 nocrt: true,
1619 }
1620
1621 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001622 name: "libvndk_sp",
1623 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001624 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001625 vndk: {
1626 enabled: true,
1627 support_system_process: true,
1628 },
1629 nocrt: true,
1630 }
1631
1632 cc_library {
1633 name: "libvndk_sp_ext",
1634 vendor: true,
1635 vndk: {
1636 enabled: true,
1637 extends: "libvndk_sp",
1638 support_system_process: true,
1639 },
1640 nocrt: true,
1641 }
1642
1643 cc_library {
1644 name: "libvendor",
1645 vendor: true,
1646 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1647 nocrt: true,
1648 }
1649 `)
1650}
1651
Logan Chiend3c59a22018-03-29 14:08:15 +08001652func TestVndkExtUseVendorLib(t *testing.T) {
1653 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001654 testCc(t, `
1655 cc_library {
1656 name: "libvndk",
1657 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001658 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001659 vndk: {
1660 enabled: true,
1661 },
1662 nocrt: true,
1663 }
1664
1665 cc_library {
1666 name: "libvndk_ext",
1667 vendor: true,
1668 vndk: {
1669 enabled: true,
1670 extends: "libvndk",
1671 },
1672 shared_libs: ["libvendor"],
1673 nocrt: true,
1674 }
1675
1676 cc_library {
1677 name: "libvendor",
1678 vendor: true,
1679 nocrt: true,
1680 }
1681 `)
Logan Chienf3511742017-10-31 18:04:35 +08001682
Logan Chiend3c59a22018-03-29 14:08:15 +08001683 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1684 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001685 cc_library {
1686 name: "libvndk_sp",
1687 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001688 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001689 vndk: {
1690 enabled: true,
1691 support_system_process: true,
1692 },
1693 nocrt: true,
1694 }
1695
1696 cc_library {
1697 name: "libvndk_sp_ext",
1698 vendor: true,
1699 vndk: {
1700 enabled: true,
1701 extends: "libvndk_sp",
1702 support_system_process: true,
1703 },
1704 shared_libs: ["libvendor"], // Cause an error
1705 nocrt: true,
1706 }
1707
1708 cc_library {
1709 name: "libvendor",
1710 vendor: true,
1711 nocrt: true,
1712 }
1713 `)
1714}
1715
Justin Yun0ecf0b22020-02-28 15:07:59 +09001716func TestProductVndkExtDependency(t *testing.T) {
1717 bp := `
1718 cc_library {
1719 name: "libvndk",
1720 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001721 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001722 vndk: {
1723 enabled: true,
1724 },
1725 nocrt: true,
1726 }
1727
1728 cc_library {
1729 name: "libvndk_ext_product",
1730 product_specific: true,
1731 vndk: {
1732 enabled: true,
1733 extends: "libvndk",
1734 },
1735 shared_libs: ["libproduct_for_vndklibs"],
1736 nocrt: true,
1737 }
1738
1739 cc_library {
1740 name: "libvndk_sp",
1741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001742 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001743 vndk: {
1744 enabled: true,
1745 support_system_process: true,
1746 },
1747 nocrt: true,
1748 }
1749
1750 cc_library {
1751 name: "libvndk_sp_ext_product",
1752 product_specific: true,
1753 vndk: {
1754 enabled: true,
1755 extends: "libvndk_sp",
1756 support_system_process: true,
1757 },
1758 shared_libs: ["libproduct_for_vndklibs"],
1759 nocrt: true,
1760 }
1761
1762 cc_library {
1763 name: "libproduct",
1764 product_specific: true,
1765 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1766 nocrt: true,
1767 }
1768
1769 cc_library {
1770 name: "libproduct_for_vndklibs",
1771 product_specific: true,
1772 nocrt: true,
1773 }
1774 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001775 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001776 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1777 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
1778 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
1779
1780 testCcWithConfig(t, config)
1781}
1782
Logan Chiend3c59a22018-03-29 14:08:15 +08001783func TestVndkSpExtUseVndkError(t *testing.T) {
1784 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1785 // library.
1786 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1787 cc_library {
1788 name: "libvndk",
1789 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001790 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001791 vndk: {
1792 enabled: true,
1793 },
1794 nocrt: true,
1795 }
1796
1797 cc_library {
1798 name: "libvndk_sp",
1799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001800 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001801 vndk: {
1802 enabled: true,
1803 support_system_process: true,
1804 },
1805 nocrt: true,
1806 }
1807
1808 cc_library {
1809 name: "libvndk_sp_ext",
1810 vendor: true,
1811 vndk: {
1812 enabled: true,
1813 extends: "libvndk_sp",
1814 support_system_process: true,
1815 },
1816 shared_libs: ["libvndk"], // Cause an error
1817 nocrt: true,
1818 }
1819 `)
1820
1821 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1822 // library.
1823 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1824 cc_library {
1825 name: "libvndk",
1826 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001827 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001828 vndk: {
1829 enabled: true,
1830 },
1831 nocrt: true,
1832 }
1833
1834 cc_library {
1835 name: "libvndk_ext",
1836 vendor: true,
1837 vndk: {
1838 enabled: true,
1839 extends: "libvndk",
1840 },
1841 nocrt: true,
1842 }
1843
1844 cc_library {
1845 name: "libvndk_sp",
1846 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001847 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001848 vndk: {
1849 enabled: true,
1850 support_system_process: true,
1851 },
1852 nocrt: true,
1853 }
1854
1855 cc_library {
1856 name: "libvndk_sp_ext",
1857 vendor: true,
1858 vndk: {
1859 enabled: true,
1860 extends: "libvndk_sp",
1861 support_system_process: true,
1862 },
1863 shared_libs: ["libvndk_ext"], // Cause an error
1864 nocrt: true,
1865 }
1866 `)
1867}
1868
1869func TestVndkUseVndkExtError(t *testing.T) {
1870 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1871 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001872 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1873 cc_library {
1874 name: "libvndk",
1875 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001876 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001877 vndk: {
1878 enabled: true,
1879 },
1880 nocrt: true,
1881 }
1882
1883 cc_library {
1884 name: "libvndk_ext",
1885 vendor: true,
1886 vndk: {
1887 enabled: true,
1888 extends: "libvndk",
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk2",
1895 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001896 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001897 vndk: {
1898 enabled: true,
1899 },
1900 shared_libs: ["libvndk_ext"],
1901 nocrt: true,
1902 }
1903 `)
1904
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001905 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001906 cc_library {
1907 name: "libvndk",
1908 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001909 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001910 vndk: {
1911 enabled: true,
1912 },
1913 nocrt: true,
1914 }
1915
1916 cc_library {
1917 name: "libvndk_ext",
1918 vendor: true,
1919 vndk: {
1920 enabled: true,
1921 extends: "libvndk",
1922 },
1923 nocrt: true,
1924 }
1925
1926 cc_library {
1927 name: "libvndk2",
1928 vendor_available: true,
1929 vndk: {
1930 enabled: true,
1931 },
1932 target: {
1933 vendor: {
1934 shared_libs: ["libvndk_ext"],
1935 },
1936 },
1937 nocrt: true,
1938 }
1939 `)
1940
1941 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1942 cc_library {
1943 name: "libvndk_sp",
1944 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001945 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001946 vndk: {
1947 enabled: true,
1948 support_system_process: true,
1949 },
1950 nocrt: true,
1951 }
1952
1953 cc_library {
1954 name: "libvndk_sp_ext",
1955 vendor: true,
1956 vndk: {
1957 enabled: true,
1958 extends: "libvndk_sp",
1959 support_system_process: true,
1960 },
1961 nocrt: true,
1962 }
1963
1964 cc_library {
1965 name: "libvndk_sp_2",
1966 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001967 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001968 vndk: {
1969 enabled: true,
1970 support_system_process: true,
1971 },
1972 shared_libs: ["libvndk_sp_ext"],
1973 nocrt: true,
1974 }
1975 `)
1976
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001977 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001978 cc_library {
1979 name: "libvndk_sp",
1980 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001981 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001982 vndk: {
1983 enabled: true,
1984 },
1985 nocrt: true,
1986 }
1987
1988 cc_library {
1989 name: "libvndk_sp_ext",
1990 vendor: true,
1991 vndk: {
1992 enabled: true,
1993 extends: "libvndk_sp",
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp2",
2000 vendor_available: true,
2001 vndk: {
2002 enabled: true,
2003 },
2004 target: {
2005 vendor: {
2006 shared_libs: ["libvndk_sp_ext"],
2007 },
2008 },
2009 nocrt: true,
2010 }
2011 `)
2012}
2013
Justin Yun5f7f7e82019-11-18 19:52:14 +09002014func TestEnforceProductVndkVersion(t *testing.T) {
2015 bp := `
2016 cc_library {
2017 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002018 llndk_stubs: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002019 }
2020 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002021 name: "libllndk.llndk",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002022 symbol_file: "",
2023 }
2024 cc_library {
2025 name: "libvndk",
2026 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002027 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002028 vndk: {
2029 enabled: true,
2030 },
2031 nocrt: true,
2032 }
2033 cc_library {
2034 name: "libvndk_sp",
2035 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002036 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002037 vndk: {
2038 enabled: true,
2039 support_system_process: true,
2040 },
2041 nocrt: true,
2042 }
2043 cc_library {
2044 name: "libva",
2045 vendor_available: true,
2046 nocrt: true,
2047 }
2048 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002049 name: "libpa",
2050 product_available: true,
2051 nocrt: true,
2052 }
2053 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002054 name: "libboth_available",
2055 vendor_available: true,
2056 product_available: true,
2057 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002058 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002059 target: {
2060 vendor: {
2061 suffix: "-vendor",
2062 },
2063 product: {
2064 suffix: "-product",
2065 },
2066 }
2067 }
2068 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002069 name: "libproduct_va",
2070 product_specific: true,
2071 vendor_available: true,
2072 nocrt: true,
2073 }
2074 cc_library {
2075 name: "libprod",
2076 product_specific: true,
2077 shared_libs: [
2078 "libllndk",
2079 "libvndk",
2080 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002081 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002082 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002083 "libproduct_va",
2084 ],
2085 nocrt: true,
2086 }
2087 cc_library {
2088 name: "libvendor",
2089 vendor: true,
2090 shared_libs: [
2091 "libllndk",
2092 "libvndk",
2093 "libvndk_sp",
2094 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002095 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002096 "libproduct_va",
2097 ],
2098 nocrt: true,
2099 }
2100 `
2101
Justin Yun13decfb2021-03-08 19:25:55 +09002102 ctx := ccFixtureFactory.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002103
Jooyung Han261e1582020-10-20 18:54:21 +09002104 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2105 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002106
2107 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2108 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2109
2110 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2111 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002112
2113 ensureStringContains := func(t *testing.T, str string, substr string) {
2114 t.Helper()
2115 if !strings.Contains(str, substr) {
2116 t.Errorf("%q is not found in %v", substr, str)
2117 }
2118 }
2119 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2120 t.Helper()
2121 if strings.Contains(str, substr) {
2122 t.Errorf("%q is found in %v", substr, str)
2123 }
2124 }
2125
2126 // _static variant is used since _shared reuses *.o from the static variant
2127 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2128 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2129
2130 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2131 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2132 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2133 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2134
2135 product_cflags := product_static.Rule("cc").Args["cFlags"]
2136 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2137 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2138 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002139}
2140
2141func TestEnforceProductVndkVersionErrors(t *testing.T) {
2142 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2143 cc_library {
2144 name: "libprod",
2145 product_specific: true,
2146 shared_libs: [
2147 "libvendor",
2148 ],
2149 nocrt: true,
2150 }
2151 cc_library {
2152 name: "libvendor",
2153 vendor: true,
2154 nocrt: true,
2155 }
2156 `)
2157 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2158 cc_library {
2159 name: "libprod",
2160 product_specific: true,
2161 shared_libs: [
2162 "libsystem",
2163 ],
2164 nocrt: true,
2165 }
2166 cc_library {
2167 name: "libsystem",
2168 nocrt: true,
2169 }
2170 `)
Justin Yun6977e8a2020-10-29 18:24:11 +09002171 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2172 cc_library {
2173 name: "libprod",
2174 product_specific: true,
2175 shared_libs: [
2176 "libva",
2177 ],
2178 nocrt: true,
2179 }
2180 cc_library {
2181 name: "libva",
2182 vendor_available: true,
2183 nocrt: true,
2184 }
2185 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002186 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002187 cc_library {
2188 name: "libprod",
2189 product_specific: true,
2190 shared_libs: [
2191 "libvndk_private",
2192 ],
2193 nocrt: true,
2194 }
2195 cc_library {
2196 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002197 vendor_available: true,
2198 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002199 vndk: {
2200 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002201 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002202 },
2203 nocrt: true,
2204 }
2205 `)
2206 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.VER", `
2207 cc_library {
2208 name: "libprod",
2209 product_specific: true,
2210 shared_libs: [
2211 "libsystem_ext",
2212 ],
2213 nocrt: true,
2214 }
2215 cc_library {
2216 name: "libsystem_ext",
2217 system_ext_specific: true,
2218 nocrt: true,
2219 }
2220 `)
2221 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2222 cc_library {
2223 name: "libsystem",
2224 shared_libs: [
2225 "libproduct_va",
2226 ],
2227 nocrt: true,
2228 }
2229 cc_library {
2230 name: "libproduct_va",
2231 product_specific: true,
2232 vendor_available: true,
2233 nocrt: true,
2234 }
2235 `)
2236}
2237
Jooyung Han38002912019-05-16 04:01:54 +09002238func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002239 bp := `
2240 cc_library {
2241 name: "libvndk",
2242 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002243 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002244 vndk: {
2245 enabled: true,
2246 },
2247 }
2248 cc_library {
2249 name: "libvndksp",
2250 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002251 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002252 vndk: {
2253 enabled: true,
2254 support_system_process: true,
2255 },
2256 }
2257 cc_library {
2258 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002259 vendor_available: true,
2260 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002261 vndk: {
2262 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002263 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002264 },
2265 }
2266 cc_library {
2267 name: "libvendor",
2268 vendor: true,
2269 }
2270 cc_library {
2271 name: "libvndkext",
2272 vendor: true,
2273 vndk: {
2274 enabled: true,
2275 extends: "libvndk",
2276 },
2277 }
2278 vndk_prebuilt_shared {
2279 name: "prevndk",
2280 version: "27",
2281 target_arch: "arm",
2282 binder32bit: true,
2283 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002284 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002285 vndk: {
2286 enabled: true,
2287 },
2288 arch: {
2289 arm: {
2290 srcs: ["liba.so"],
2291 },
2292 },
2293 }
2294 cc_library {
2295 name: "libllndk",
Colin Cross0477b422020-10-13 18:43:54 -07002296 llndk_stubs: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002297 }
2298 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002299 name: "libllndk.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002300 symbol_file: "",
2301 }
2302 cc_library {
2303 name: "libllndkprivate",
Colin Cross0477b422020-10-13 18:43:54 -07002304 llndk_stubs: "libllndkprivate.llndk",
Colin Cross98be1bb2019-12-13 20:41:13 -08002305 }
2306 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002307 name: "libllndkprivate.llndk",
Justin Yunc0d8c492021-01-07 17:45:31 +09002308 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002309 symbol_file: "",
Colin Cross78212242021-01-06 14:51:30 -08002310 }
2311
2312 llndk_libraries_txt {
2313 name: "llndk.libraries.txt",
2314 }
2315 vndkcore_libraries_txt {
2316 name: "vndkcore.libraries.txt",
2317 }
2318 vndksp_libraries_txt {
2319 name: "vndksp.libraries.txt",
2320 }
2321 vndkprivate_libraries_txt {
2322 name: "vndkprivate.libraries.txt",
2323 }
2324 vndkcorevariant_libraries_txt {
2325 name: "vndkcorevariant.libraries.txt",
2326 insert_vndk_version: false,
2327 }
2328 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002329
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002330 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002331 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
2332 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
2333 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002334 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002335
Colin Cross78212242021-01-06 14:51:30 -08002336 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2337 []string{"libvndk.so", "libvndkprivate.so"})
2338 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2339 []string{"libc++.so", "libvndksp.so"})
2340 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2341 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2342 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2343 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002344
Colin Crossfb0c16e2019-11-20 17:12:35 -08002345 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002346
Jooyung Han38002912019-05-16 04:01:54 +09002347 tests := []struct {
2348 variant string
2349 name string
2350 expected string
2351 }{
2352 {vendorVariant, "libvndk", "native:vndk"},
2353 {vendorVariant, "libvndksp", "native:vndk"},
2354 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2355 {vendorVariant, "libvendor", "native:vendor"},
2356 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002357 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002358 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002359 {coreVariant, "libvndk", "native:platform"},
2360 {coreVariant, "libvndkprivate", "native:platform"},
2361 {coreVariant, "libllndk", "native:platform"},
2362 }
2363 for _, test := range tests {
2364 t.Run(test.name, func(t *testing.T) {
2365 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2366 assertString(t, module.makeLinkType, test.expected)
2367 })
2368 }
2369}
2370
Jeff Gaston294356f2017-09-27 17:05:30 -07002371var staticLinkDepOrderTestCases = []struct {
2372 // This is a string representation of a map[moduleName][]moduleDependency .
2373 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002374 inStatic string
2375
2376 // This is a string representation of a map[moduleName][]moduleDependency .
2377 // It models the dependencies declared in an Android.bp file.
2378 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002379
2380 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2381 // The keys of allOrdered specify which modules we would like to check.
2382 // The values of allOrdered specify the expected result (of the transitive closure of all
2383 // dependencies) for each module to test
2384 allOrdered string
2385
2386 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2387 // The keys of outOrdered specify which modules we would like to check.
2388 // The values of outOrdered specify the expected result (of the ordered linker command line)
2389 // for each module to test.
2390 outOrdered string
2391}{
2392 // Simple tests
2393 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002394 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002395 outOrdered: "",
2396 },
2397 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002398 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002399 outOrdered: "a:",
2400 },
2401 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002402 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002403 outOrdered: "a:b; b:",
2404 },
2405 // Tests of reordering
2406 {
2407 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002408 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002409 outOrdered: "a:b,c,d; b:d; c:d; d:",
2410 },
2411 {
2412 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002413 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002414 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2415 },
2416 {
2417 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002418 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002419 outOrdered: "a:d,b,e,c; d:b; e:c",
2420 },
2421 {
2422 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002423 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002424 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2425 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2426 },
2427 {
2428 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002429 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 -07002430 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2431 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2432 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002433 // shared dependencies
2434 {
2435 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2436 // So, we don't actually have to check that a shared dependency of c will change the order
2437 // of a library that depends statically on b and on c. We only need to check that if c has
2438 // a shared dependency on b, that that shows up in allOrdered.
2439 inShared: "c:b",
2440 allOrdered: "c:b",
2441 outOrdered: "c:",
2442 },
2443 {
2444 // This test doesn't actually include any shared dependencies but it's a reminder of what
2445 // the second phase of the above test would look like
2446 inStatic: "a:b,c; c:b",
2447 allOrdered: "a:c,b; c:b",
2448 outOrdered: "a:c,b; c:b",
2449 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002450 // tiebreakers for when two modules specifying different orderings and there is no dependency
2451 // to dictate an order
2452 {
2453 // 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 -08002454 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002455 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2456 },
2457 {
2458 // 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 -08002459 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 -07002460 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2461 },
2462 // Tests involving duplicate dependencies
2463 {
2464 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002465 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002466 outOrdered: "a:c,b",
2467 },
2468 {
2469 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002470 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002471 outOrdered: "a:d,c,b",
2472 },
2473 // Tests to confirm the nonexistence of infinite loops.
2474 // These cases should never happen, so as long as the test terminates and the
2475 // result is deterministic then that should be fine.
2476 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002477 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002478 outOrdered: "a:a",
2479 },
2480 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002481 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002482 allOrdered: "a:b,c; b:c,a; c:a,b",
2483 outOrdered: "a:b; b:c; c:a",
2484 },
2485 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002486 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002487 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2488 outOrdered: "a:c,b; b:a,c; c:b,a",
2489 },
2490}
2491
2492// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2493func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2494 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2495 strippedText := strings.Replace(text, " ", "", -1)
2496 if len(strippedText) < 1 {
2497 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2498 }
2499 allDeps = make(map[android.Path][]android.Path, 0)
2500
2501 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2502 moduleTexts := strings.Split(strippedText, ";")
2503
2504 outputForModuleName := func(moduleName string) android.Path {
2505 return android.PathForTesting(moduleName)
2506 }
2507
2508 for _, moduleText := range moduleTexts {
2509 // convert from "a:b,c" to ["a", "b,c"]
2510 components := strings.Split(moduleText, ":")
2511 if len(components) != 2 {
2512 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2513 }
2514 moduleName := components[0]
2515 moduleOutput := outputForModuleName(moduleName)
2516 modulesInOrder = append(modulesInOrder, moduleOutput)
2517
2518 depString := components[1]
2519 // convert from "b,c" to ["b", "c"]
2520 depNames := strings.Split(depString, ",")
2521 if len(depString) < 1 {
2522 depNames = []string{}
2523 }
2524 var deps []android.Path
2525 for _, depName := range depNames {
2526 deps = append(deps, outputForModuleName(depName))
2527 }
2528 allDeps[moduleOutput] = deps
2529 }
2530 return modulesInOrder, allDeps
2531}
2532
Jeff Gaston294356f2017-09-27 17:05:30 -07002533func getOutputPaths(ctx *android.TestContext, variant string, moduleNames []string) (paths android.Paths) {
2534 for _, moduleName := range moduleNames {
2535 module := ctx.ModuleForTests(moduleName, variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002536 output := module.outputFile.Path().RelativeToTop()
Jeff Gaston294356f2017-09-27 17:05:30 -07002537 paths = append(paths, output)
2538 }
2539 return paths
2540}
2541
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002542func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002543 ctx := testCc(t, `
2544 cc_library {
2545 name: "a",
2546 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002547 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002548 }
2549 cc_library {
2550 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002551 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 }
2553 cc_library {
2554 name: "c",
2555 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002556 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002557 }
2558 cc_library {
2559 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002560 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 }
2562
2563 `)
2564
Colin Cross7113d202019-11-20 16:39:12 -08002565 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002566 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002567 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2568 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002569 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002570
2571 if !reflect.DeepEqual(actual, expected) {
2572 t.Errorf("staticDeps orderings were not propagated correctly"+
2573 "\nactual: %v"+
2574 "\nexpected: %v",
2575 actual,
2576 expected,
2577 )
2578 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002579}
Jeff Gaston294356f2017-09-27 17:05:30 -07002580
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002581func TestStaticLibDepReorderingWithShared(t *testing.T) {
2582 ctx := testCc(t, `
2583 cc_library {
2584 name: "a",
2585 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002586 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002587 }
2588 cc_library {
2589 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002590 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002591 }
2592 cc_library {
2593 name: "c",
2594 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002595 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596 }
2597
2598 `)
2599
Colin Cross7113d202019-11-20 16:39:12 -08002600 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002601 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002602 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2603 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Colin Cross0de8a1e2020-09-18 14:15:30 -07002604 expected := getOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002605
2606 if !reflect.DeepEqual(actual, expected) {
2607 t.Errorf("staticDeps orderings did not account for shared libs"+
2608 "\nactual: %v"+
2609 "\nexpected: %v",
2610 actual,
2611 expected,
2612 )
2613 }
2614}
2615
Jooyung Hanb04a4992020-03-13 18:57:35 +09002616func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002617 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002618 if !reflect.DeepEqual(actual, expected) {
2619 t.Errorf(message+
2620 "\nactual: %v"+
2621 "\nexpected: %v",
2622 actual,
2623 expected,
2624 )
2625 }
2626}
2627
Jooyung Han61b66e92020-03-21 14:21:46 +00002628func TestLlndkLibrary(t *testing.T) {
2629 ctx := testCc(t, `
2630 cc_library {
2631 name: "libllndk",
2632 stubs: { versions: ["1", "2"] },
Colin Cross0477b422020-10-13 18:43:54 -07002633 llndk_stubs: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002634 }
2635 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002636 name: "libllndk.llndk",
Jooyung Han61b66e92020-03-21 14:21:46 +00002637 }
Colin Cross127bb8b2020-12-16 16:46:01 -08002638
2639 cc_prebuilt_library_shared {
2640 name: "libllndkprebuilt",
2641 stubs: { versions: ["1", "2"] },
2642 llndk_stubs: "libllndkprebuilt.llndk",
2643 }
2644 llndk_library {
2645 name: "libllndkprebuilt.llndk",
2646 }
2647
2648 cc_library {
2649 name: "libllndk_with_external_headers",
2650 stubs: { versions: ["1", "2"] },
2651 llndk_stubs: "libllndk_with_external_headers.llndk",
2652 header_libs: ["libexternal_headers"],
2653 export_header_lib_headers: ["libexternal_headers"],
2654 }
2655 llndk_library {
2656 name: "libllndk_with_external_headers.llndk",
2657 }
2658 cc_library_headers {
2659 name: "libexternal_headers",
2660 export_include_dirs: ["include"],
2661 vendor_available: true,
2662 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002663 `)
Colin Cross127bb8b2020-12-16 16:46:01 -08002664 actual := ctx.ModuleVariantsForTests("libllndk")
2665 for i := 0; i < len(actual); i++ {
2666 if !strings.HasPrefix(actual[i], "android_vendor.VER_") {
2667 actual = append(actual[:i], actual[i+1:]...)
2668 i--
2669 }
2670 }
Jooyung Han61b66e92020-03-21 14:21:46 +00002671 expected := []string{
Jooyung Han61b66e92020-03-21 14:21:46 +00002672 "android_vendor.VER_arm64_armv8-a_shared_1",
2673 "android_vendor.VER_arm64_armv8-a_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002674 "android_vendor.VER_arm64_armv8-a_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002675 "android_vendor.VER_arm_armv7-a-neon_shared_1",
2676 "android_vendor.VER_arm_armv7-a-neon_shared_2",
Colin Cross0de8a1e2020-09-18 14:15:30 -07002677 "android_vendor.VER_arm_armv7-a-neon_shared",
Jooyung Han61b66e92020-03-21 14:21:46 +00002678 }
2679 checkEquals(t, "variants for llndk stubs", expected, actual)
2680
Colin Cross127bb8b2020-12-16 16:46:01 -08002681 params := ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002682 checkEquals(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2683
Colin Cross127bb8b2020-12-16 16:46:01 -08002684 params = ctx.ModuleForTests("libllndk", "android_vendor.VER_arm_armv7-a-neon_shared_1").Description("generate stub")
Jooyung Han61b66e92020-03-21 14:21:46 +00002685 checkEquals(t, "override apiLevel for versioned stubs", "1", params.Args["apiLevel"])
2686}
2687
Jiyong Parka46a4d52017-12-14 19:54:34 +09002688func TestLlndkHeaders(t *testing.T) {
2689 ctx := testCc(t, `
2690 llndk_headers {
2691 name: "libllndk_headers",
2692 export_include_dirs: ["my_include"],
2693 }
2694 llndk_library {
Colin Cross0477b422020-10-13 18:43:54 -07002695 name: "libllndk.llndk",
Jiyong Parka46a4d52017-12-14 19:54:34 +09002696 export_llndk_headers: ["libllndk_headers"],
2697 }
2698 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002699 name: "libllndk",
2700 llndk_stubs: "libllndk.llndk",
2701 }
2702
2703 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002704 name: "libvendor",
2705 shared_libs: ["libllndk"],
2706 vendor: true,
2707 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002708 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002709 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710 }
2711 `)
2712
2713 // _static variant is used since _shared reuses *.o from the static variant
Colin Crossfb0c16e2019-11-20 17:12:35 -08002714 cc := ctx.ModuleForTests("libvendor", "android_vendor.VER_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002715 cflags := cc.Args["cFlags"]
2716 if !strings.Contains(cflags, "-Imy_include") {
2717 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2718 }
2719}
2720
Logan Chien43d34c32017-12-20 01:17:32 +08002721func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2722 actual := module.Properties.AndroidMkRuntimeLibs
2723 if !reflect.DeepEqual(actual, expected) {
2724 t.Errorf("incorrect runtime_libs for shared libs"+
2725 "\nactual: %v"+
2726 "\nexpected: %v",
2727 actual,
2728 expected,
2729 )
2730 }
2731}
2732
2733const runtimeLibAndroidBp = `
2734 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002735 name: "liball_available",
2736 vendor_available: true,
2737 product_available: true,
2738 no_libcrt : true,
2739 nocrt : true,
2740 system_shared_libs : [],
2741 }
2742 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002743 name: "libvendor_available1",
2744 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002745 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002746 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002747 nocrt : true,
2748 system_shared_libs : [],
2749 }
2750 cc_library {
2751 name: "libvendor_available2",
2752 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002753 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002754 target: {
2755 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002756 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002757 }
2758 },
Yi Konge7fe9912019-06-02 00:53:50 -07002759 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002760 nocrt : true,
2761 system_shared_libs : [],
2762 }
2763 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002764 name: "libproduct_vendor",
2765 product_specific: true,
2766 vendor_available: true,
2767 no_libcrt : true,
2768 nocrt : true,
2769 system_shared_libs : [],
2770 }
2771 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002772 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002773 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002774 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002775 nocrt : true,
2776 system_shared_libs : [],
2777 }
2778 cc_library {
2779 name: "libvendor1",
2780 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002781 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002782 nocrt : true,
2783 system_shared_libs : [],
2784 }
2785 cc_library {
2786 name: "libvendor2",
2787 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002788 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002789 no_libcrt : true,
2790 nocrt : true,
2791 system_shared_libs : [],
2792 }
2793 cc_library {
2794 name: "libproduct_available1",
2795 product_available: true,
2796 runtime_libs: ["liball_available"],
2797 no_libcrt : true,
2798 nocrt : true,
2799 system_shared_libs : [],
2800 }
2801 cc_library {
2802 name: "libproduct1",
2803 product_specific: true,
2804 no_libcrt : true,
2805 nocrt : true,
2806 system_shared_libs : [],
2807 }
2808 cc_library {
2809 name: "libproduct2",
2810 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002811 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002812 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002813 nocrt : true,
2814 system_shared_libs : [],
2815 }
2816`
2817
2818func TestRuntimeLibs(t *testing.T) {
2819 ctx := testCc(t, runtimeLibAndroidBp)
2820
2821 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002822 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002823
Justin Yun8a2600c2020-12-07 12:44:03 +09002824 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2825 checkRuntimeLibs(t, []string{"liball_available"}, module)
2826
2827 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2828 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002829
2830 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002831 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002832
2833 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2834 // and vendor variants.
Colin Crossfb0c16e2019-11-20 17:12:35 -08002835 variant = "android_vendor.VER_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002836
Justin Yun8a2600c2020-12-07 12:44:03 +09002837 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2838 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002839
2840 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002841 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002842
2843 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2844 // and product variants.
2845 variant = "android_product.VER_arm64_armv8-a_shared"
2846
2847 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2848 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2849
2850 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002851 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002852}
2853
2854func TestExcludeRuntimeLibs(t *testing.T) {
2855 ctx := testCc(t, runtimeLibAndroidBp)
2856
Colin Cross7113d202019-11-20 16:39:12 -08002857 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002858 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2859 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002860
Colin Crossfb0c16e2019-11-20 17:12:35 -08002861 variant = "android_vendor.VER_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002862 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002863 checkRuntimeLibs(t, nil, module)
2864}
2865
2866func TestRuntimeLibsNoVndk(t *testing.T) {
2867 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2868
2869 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2870
Colin Cross7113d202019-11-20 16:39:12 -08002871 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002872
Justin Yun8a2600c2020-12-07 12:44:03 +09002873 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2874 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002875
2876 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002877 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002878
2879 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002880 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002881}
2882
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002883func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002884 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002885 actual := module.Properties.AndroidMkStaticLibs
2886 if !reflect.DeepEqual(actual, expected) {
2887 t.Errorf("incorrect static_libs"+
2888 "\nactual: %v"+
2889 "\nexpected: %v",
2890 actual,
2891 expected,
2892 )
2893 }
2894}
2895
2896const staticLibAndroidBp = `
2897 cc_library {
2898 name: "lib1",
2899 }
2900 cc_library {
2901 name: "lib2",
2902 static_libs: ["lib1"],
2903 }
2904`
2905
2906func TestStaticLibDepExport(t *testing.T) {
2907 ctx := testCc(t, staticLibAndroidBp)
2908
2909 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002910 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002911 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002912 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002913
2914 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002915 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002916 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2917 // libc++_static is linked additionally.
Peter Collingbournee5ba2862019-12-10 18:37:45 -08002918 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android", "libatomic"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002919}
2920
Jiyong Parkd08b6972017-09-26 10:50:54 +09002921var compilerFlagsTestCases = []struct {
2922 in string
2923 out bool
2924}{
2925 {
2926 in: "a",
2927 out: false,
2928 },
2929 {
2930 in: "-a",
2931 out: true,
2932 },
2933 {
2934 in: "-Ipath/to/something",
2935 out: false,
2936 },
2937 {
2938 in: "-isystempath/to/something",
2939 out: false,
2940 },
2941 {
2942 in: "--coverage",
2943 out: false,
2944 },
2945 {
2946 in: "-include a/b",
2947 out: true,
2948 },
2949 {
2950 in: "-include a/b c/d",
2951 out: false,
2952 },
2953 {
2954 in: "-DMACRO",
2955 out: true,
2956 },
2957 {
2958 in: "-DMAC RO",
2959 out: false,
2960 },
2961 {
2962 in: "-a -b",
2963 out: false,
2964 },
2965 {
2966 in: "-DMACRO=definition",
2967 out: true,
2968 },
2969 {
2970 in: "-DMACRO=defi nition",
2971 out: true, // TODO(jiyong): this should be false
2972 },
2973 {
2974 in: "-DMACRO(x)=x + 1",
2975 out: true,
2976 },
2977 {
2978 in: "-DMACRO=\"defi nition\"",
2979 out: true,
2980 },
2981}
2982
2983type mockContext struct {
2984 BaseModuleContext
2985 result bool
2986}
2987
2988func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
2989 // CheckBadCompilerFlags calls this function when the flag should be rejected
2990 ctx.result = false
2991}
2992
2993func TestCompilerFlags(t *testing.T) {
2994 for _, testCase := range compilerFlagsTestCases {
2995 ctx := &mockContext{result: true}
2996 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
2997 if ctx.result != testCase.out {
2998 t.Errorf("incorrect output:")
2999 t.Errorf(" input: %#v", testCase.in)
3000 t.Errorf(" expected: %#v", testCase.out)
3001 t.Errorf(" got: %#v", ctx.result)
3002 }
3003 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003004}
Jiyong Park374510b2018-03-19 18:23:01 +09003005
3006func TestVendorPublicLibraries(t *testing.T) {
3007 ctx := testCc(t, `
3008 cc_library_headers {
3009 name: "libvendorpublic_headers",
3010 export_include_dirs: ["my_include"],
3011 }
3012 vendor_public_library {
3013 name: "libvendorpublic",
3014 symbol_file: "",
3015 export_public_headers: ["libvendorpublic_headers"],
3016 }
3017 cc_library {
3018 name: "libvendorpublic",
3019 srcs: ["foo.c"],
3020 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07003021 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003022 nocrt: true,
3023 }
3024
3025 cc_library {
3026 name: "libsystem",
3027 shared_libs: ["libvendorpublic"],
3028 vendor: false,
3029 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003030 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003031 nocrt: true,
3032 }
3033 cc_library {
3034 name: "libvendor",
3035 shared_libs: ["libvendorpublic"],
3036 vendor: true,
3037 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07003038 no_libcrt: true,
Jiyong Park374510b2018-03-19 18:23:01 +09003039 nocrt: true,
3040 }
3041 `)
3042
Colin Cross7113d202019-11-20 16:39:12 -08003043 coreVariant := "android_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -08003044 vendorVariant := "android_vendor.VER_arm64_armv8-a_shared"
Jiyong Park374510b2018-03-19 18:23:01 +09003045
3046 // test if header search paths are correctly added
3047 // _static variant is used since _shared reuses *.o from the static variant
Colin Cross7113d202019-11-20 16:39:12 -08003048 cc := ctx.ModuleForTests("libsystem", strings.Replace(coreVariant, "_shared", "_static", 1)).Rule("cc")
Jiyong Park374510b2018-03-19 18:23:01 +09003049 cflags := cc.Args["cFlags"]
3050 if !strings.Contains(cflags, "-Imy_include") {
3051 t.Errorf("cflags for libsystem must contain -Imy_include, but was %#v.", cflags)
3052 }
3053
3054 // test if libsystem is linked to the stub
Colin Cross7113d202019-11-20 16:39:12 -08003055 ld := ctx.ModuleForTests("libsystem", coreVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003056 libflags := ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003057 stubPaths := getOutputPaths(ctx, coreVariant, []string{"libvendorpublic" + vendorPublicLibrarySuffix})
Jiyong Park374510b2018-03-19 18:23:01 +09003058 if !strings.Contains(libflags, stubPaths[0].String()) {
3059 t.Errorf("libflags for libsystem must contain %#v, but was %#v", stubPaths[0], libflags)
3060 }
3061
3062 // test if libvendor is linked to the real shared lib
Colin Cross7113d202019-11-20 16:39:12 -08003063 ld = ctx.ModuleForTests("libvendor", vendorVariant).Rule("ld")
Jiyong Park374510b2018-03-19 18:23:01 +09003064 libflags = ld.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003065 stubPaths = getOutputPaths(ctx, vendorVariant, []string{"libvendorpublic"})
Jiyong Park374510b2018-03-19 18:23:01 +09003066 if !strings.Contains(libflags, stubPaths[0].String()) {
3067 t.Errorf("libflags for libvendor must contain %#v, but was %#v", stubPaths[0], libflags)
3068 }
3069
3070}
Jiyong Park37b25202018-07-11 10:49:27 +09003071
3072func TestRecovery(t *testing.T) {
3073 ctx := testCc(t, `
3074 cc_library_shared {
3075 name: "librecovery",
3076 recovery: true,
3077 }
3078 cc_library_shared {
3079 name: "librecovery32",
3080 recovery: true,
3081 compile_multilib:"32",
3082 }
Jiyong Park5baac542018-08-28 09:55:37 +09003083 cc_library_shared {
3084 name: "libHalInRecovery",
3085 recovery_available: true,
3086 vendor: true,
3087 }
Jiyong Park37b25202018-07-11 10:49:27 +09003088 `)
3089
3090 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003091 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003092 if len(variants) != 1 || !android.InList(arm64, variants) {
3093 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3094 }
3095
3096 variants = ctx.ModuleVariantsForTests("librecovery32")
3097 if android.InList(arm64, variants) {
3098 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3099 }
Jiyong Park5baac542018-08-28 09:55:37 +09003100
3101 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3102 if !recoveryModule.Platform() {
3103 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3104 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003105}
Jiyong Park5baac542018-08-28 09:55:37 +09003106
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003107func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3108 bp := `
3109 cc_prebuilt_test_library_shared {
3110 name: "test_lib",
3111 relative_install_path: "foo/bar/baz",
3112 srcs: ["srcpath/dontusethispath/baz.so"],
3113 }
3114
3115 cc_test {
3116 name: "main_test",
3117 data_libs: ["test_lib"],
3118 gtest: false,
3119 }
3120 `
3121
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003122 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003123 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
3124 config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
3125 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3126
3127 ctx := testCcWithConfig(t, config)
3128 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3129 testBinary := module.(*Module).linker.(*testBinary)
3130 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3131 if err != nil {
3132 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3133 }
3134 if len(outputFiles) != 1 {
3135 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3136 }
3137 if len(testBinary.dataPaths()) != 1 {
3138 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3139 }
3140
3141 outputPath := outputFiles[0].String()
3142
3143 if !strings.HasSuffix(outputPath, "/main_test") {
3144 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3145 }
Colin Crossaa255532020-07-03 13:18:24 -07003146 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003147 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3148 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3149 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3150 }
3151}
3152
Jiyong Park7ed9de32018-10-15 22:25:07 +09003153func TestVersionedStubs(t *testing.T) {
3154 ctx := testCc(t, `
3155 cc_library_shared {
3156 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003157 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003158 stubs: {
3159 symbol_file: "foo.map.txt",
3160 versions: ["1", "2", "3"],
3161 },
3162 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003163
Jiyong Park7ed9de32018-10-15 22:25:07 +09003164 cc_library_shared {
3165 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003166 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003167 shared_libs: ["libFoo#1"],
3168 }`)
3169
3170 variants := ctx.ModuleVariantsForTests("libFoo")
3171 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003172 "android_arm64_armv8-a_shared",
3173 "android_arm64_armv8-a_shared_1",
3174 "android_arm64_armv8-a_shared_2",
3175 "android_arm64_armv8-a_shared_3",
3176 "android_arm_armv7-a-neon_shared",
3177 "android_arm_armv7-a-neon_shared_1",
3178 "android_arm_armv7-a-neon_shared_2",
3179 "android_arm_armv7-a-neon_shared_3",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003180 }
3181 variantsMismatch := false
3182 if len(variants) != len(expectedVariants) {
3183 variantsMismatch = true
3184 } else {
3185 for _, v := range expectedVariants {
3186 if !inList(v, variants) {
3187 variantsMismatch = false
3188 }
3189 }
3190 }
3191 if variantsMismatch {
3192 t.Errorf("variants of libFoo expected:\n")
3193 for _, v := range expectedVariants {
3194 t.Errorf("%q\n", v)
3195 }
3196 t.Errorf(", but got:\n")
3197 for _, v := range variants {
3198 t.Errorf("%q\n", v)
3199 }
3200 }
3201
Colin Cross7113d202019-11-20 16:39:12 -08003202 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003203 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003204 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003205 if !strings.Contains(libFlags, libFoo1StubPath) {
3206 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3207 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003208
Colin Cross7113d202019-11-20 16:39:12 -08003209 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003210 cFlags := libBarCompileRule.Args["cFlags"]
3211 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3212 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3213 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3214 }
Jiyong Park37b25202018-07-11 10:49:27 +09003215}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003216
Jooyung Hanb04a4992020-03-13 18:57:35 +09003217func TestVersioningMacro(t *testing.T) {
3218 for _, tc := range []struct{ moduleName, expected string }{
3219 {"libc", "__LIBC_API__"},
3220 {"libfoo", "__LIBFOO_API__"},
3221 {"libfoo@1", "__LIBFOO_1_API__"},
3222 {"libfoo-v1", "__LIBFOO_V1_API__"},
3223 {"libfoo.v1", "__LIBFOO_V1_API__"},
3224 } {
3225 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3226 }
3227}
3228
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003229func TestStaticExecutable(t *testing.T) {
3230 ctx := testCc(t, `
3231 cc_binary {
3232 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003233 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003234 static_executable: true,
3235 }`)
3236
Colin Cross7113d202019-11-20 16:39:12 -08003237 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003238 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3239 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003240 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003241 for _, lib := range systemStaticLibs {
3242 if !strings.Contains(libFlags, lib) {
3243 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3244 }
3245 }
3246 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3247 for _, lib := range systemSharedLibs {
3248 if strings.Contains(libFlags, lib) {
3249 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3250 }
3251 }
3252}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003253
3254func TestStaticDepsOrderWithStubs(t *testing.T) {
3255 ctx := testCc(t, `
3256 cc_binary {
3257 name: "mybin",
3258 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003259 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003260 static_executable: true,
3261 stl: "none",
3262 }
3263
3264 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003265 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003266 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003267 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003268 stl: "none",
3269 }
3270
3271 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003272 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003273 srcs: ["foo.c"],
3274 stl: "none",
3275 stubs: {
3276 versions: ["1"],
3277 },
3278 }`)
3279
Colin Cross0de8a1e2020-09-18 14:15:30 -07003280 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3281 actual := mybin.Implicits[:2]
Colin Crossf9aabd72020-02-15 11:29:50 -08003282 expected := getOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003283
3284 if !reflect.DeepEqual(actual, expected) {
3285 t.Errorf("staticDeps orderings were not propagated correctly"+
3286 "\nactual: %v"+
3287 "\nexpected: %v",
3288 actual,
3289 expected,
3290 )
3291 }
3292}
Jooyung Han38002912019-05-16 04:01:54 +09003293
Jooyung Hand48f3c32019-08-23 11:18:57 +09003294func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3295 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3296 cc_library {
3297 name: "libA",
3298 srcs: ["foo.c"],
3299 shared_libs: ["libB"],
3300 stl: "none",
3301 }
3302
3303 cc_library {
3304 name: "libB",
3305 srcs: ["foo.c"],
3306 enabled: false,
3307 stl: "none",
3308 }
3309 `)
3310}
3311
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003312// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3313// correctly.
3314func TestFuzzTarget(t *testing.T) {
3315 ctx := testCc(t, `
3316 cc_fuzz {
3317 name: "fuzz_smoke_test",
3318 srcs: ["foo.c"],
3319 }`)
3320
Paul Duffin075c4172019-12-19 19:06:13 +00003321 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003322 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3323}
3324
Jiyong Park29074592019-07-07 16:27:47 +09003325func TestAidl(t *testing.T) {
3326}
3327
Jooyung Han38002912019-05-16 04:01:54 +09003328func assertString(t *testing.T, got, expected string) {
3329 t.Helper()
3330 if got != expected {
3331 t.Errorf("expected %q got %q", expected, got)
3332 }
3333}
3334
3335func assertArrayString(t *testing.T, got, expected []string) {
3336 t.Helper()
3337 if len(got) != len(expected) {
3338 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3339 return
3340 }
3341 for i := range got {
3342 if got[i] != expected[i] {
3343 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3344 i, expected[i], expected, got[i], got)
3345 return
3346 }
3347 }
3348}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003349
Jooyung Han0302a842019-10-30 18:43:49 +09003350func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3351 t.Helper()
3352 assertArrayString(t, android.SortedStringKeys(m), expected)
3353}
3354
Colin Crosse1bb5d02019-09-24 14:55:04 -07003355func TestDefaults(t *testing.T) {
3356 ctx := testCc(t, `
3357 cc_defaults {
3358 name: "defaults",
3359 srcs: ["foo.c"],
3360 static: {
3361 srcs: ["bar.c"],
3362 },
3363 shared: {
3364 srcs: ["baz.c"],
3365 },
3366 }
3367
3368 cc_library_static {
3369 name: "libstatic",
3370 defaults: ["defaults"],
3371 }
3372
3373 cc_library_shared {
3374 name: "libshared",
3375 defaults: ["defaults"],
3376 }
3377
3378 cc_library {
3379 name: "libboth",
3380 defaults: ["defaults"],
3381 }
3382
3383 cc_binary {
3384 name: "binary",
3385 defaults: ["defaults"],
3386 }`)
3387
3388 pathsToBase := func(paths android.Paths) []string {
3389 var ret []string
3390 for _, p := range paths {
3391 ret = append(ret, p.Base())
3392 }
3393 return ret
3394 }
3395
Colin Cross7113d202019-11-20 16:39:12 -08003396 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003397 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3398 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3399 }
Colin Cross7113d202019-11-20 16:39:12 -08003400 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003401 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3402 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3403 }
Colin Cross7113d202019-11-20 16:39:12 -08003404 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003405 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3406 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3407 }
3408
Colin Cross7113d202019-11-20 16:39:12 -08003409 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003410 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3411 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3412 }
Colin Cross7113d202019-11-20 16:39:12 -08003413 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003414 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3415 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3416 }
3417}
Colin Crosseabaedd2020-02-06 17:01:55 -08003418
3419func TestProductVariableDefaults(t *testing.T) {
3420 bp := `
3421 cc_defaults {
3422 name: "libfoo_defaults",
3423 srcs: ["foo.c"],
3424 cppflags: ["-DFOO"],
3425 product_variables: {
3426 debuggable: {
3427 cppflags: ["-DBAR"],
3428 },
3429 },
3430 }
3431
3432 cc_library {
3433 name: "libfoo",
3434 defaults: ["libfoo_defaults"],
3435 }
3436 `
3437
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003438 result := ccFixtureFactory.Extend(
3439 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003440
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003441 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3442 variables.Debuggable = BoolPtr(true)
3443 }),
3444 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003445
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003446 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003447 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003448}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003449
3450func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3451 t.Parallel()
3452 bp := `
3453 cc_library_static {
3454 name: "libfoo",
3455 srcs: ["foo.c"],
3456 whole_static_libs: ["libbar"],
3457 }
3458
3459 cc_library_static {
3460 name: "libbar",
3461 whole_static_libs: ["libmissing"],
3462 }
3463 `
3464
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003465 result := ccFixtureFactory.Extend(
3466 android.PrepareForTestWithAllowMissingDependencies,
3467 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003468
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003469 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003470 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003471
Paul Duffine84b1332021-03-12 11:59:43 +00003472 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003473
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003474 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003475 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003476}
Colin Crosse9fe2942020-11-10 18:12:15 -08003477
3478func TestInstallSharedLibs(t *testing.T) {
3479 bp := `
3480 cc_binary {
3481 name: "bin",
3482 host_supported: true,
3483 shared_libs: ["libshared"],
3484 runtime_libs: ["libruntime"],
3485 srcs: [":gen"],
3486 }
3487
3488 cc_library_shared {
3489 name: "libshared",
3490 host_supported: true,
3491 shared_libs: ["libtransitive"],
3492 }
3493
3494 cc_library_shared {
3495 name: "libtransitive",
3496 host_supported: true,
3497 }
3498
3499 cc_library_shared {
3500 name: "libruntime",
3501 host_supported: true,
3502 }
3503
3504 cc_binary_host {
3505 name: "tool",
3506 srcs: ["foo.cpp"],
3507 }
3508
3509 genrule {
3510 name: "gen",
3511 tools: ["tool"],
3512 out: ["gen.cpp"],
3513 cmd: "$(location tool) $(out)",
3514 }
3515 `
3516
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003517 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003518 ctx := testCcWithConfig(t, config)
3519
3520 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3521 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3522 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3523 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3524 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3525
3526 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3527 t.Errorf("expected host bin dependency %q, got %q", w, g)
3528 }
3529
3530 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3531 t.Errorf("expected host bin dependency %q, got %q", w, g)
3532 }
3533
3534 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3535 t.Errorf("expected host bin dependency %q, got %q", w, g)
3536 }
3537
3538 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3539 t.Errorf("expected host bin dependency %q, got %q", w, g)
3540 }
3541
3542 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3543 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3544 }
3545
3546 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3547 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3548 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3549 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3550
3551 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3552 t.Errorf("expected device bin dependency %q, got %q", w, g)
3553 }
3554
3555 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3556 t.Errorf("expected device bin dependency %q, got %q", w, g)
3557 }
3558
3559 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3560 t.Errorf("expected device bin dependency %q, got %q", w, g)
3561 }
3562
3563 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3564 t.Errorf("expected device bin dependency %q, got %q", w, g)
3565 }
3566
3567 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3568 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3569 }
3570
3571}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003572
3573func TestStubsLibReexportsHeaders(t *testing.T) {
3574 ctx := testCc(t, `
3575 cc_library_shared {
3576 name: "libclient",
3577 srcs: ["foo.c"],
3578 shared_libs: ["libfoo#1"],
3579 }
3580
3581 cc_library_shared {
3582 name: "libfoo",
3583 srcs: ["foo.c"],
3584 shared_libs: ["libbar"],
3585 export_shared_lib_headers: ["libbar"],
3586 stubs: {
3587 symbol_file: "foo.map.txt",
3588 versions: ["1", "2", "3"],
3589 },
3590 }
3591
3592 cc_library_shared {
3593 name: "libbar",
3594 export_include_dirs: ["include/libbar"],
3595 srcs: ["foo.c"],
3596 }`)
3597
3598 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3599
3600 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3601 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3602 }
3603}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003604
3605func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3606 ctx := testCc(t, `
3607 cc_library {
3608 name: "libfoo",
3609 srcs: ["a/Foo.aidl"],
3610 aidl: { flags: ["-Werror"], },
3611 }
3612 `)
3613
3614 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3615 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3616 aidlCommand := manifest.Commands[0].GetCommand()
3617 expectedAidlFlag := "-Werror"
3618 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3619 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3620 }
3621}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003622
Jiyong Parka008fb02021-03-16 17:15:53 +09003623func TestMinSdkVersionInClangTriple(t *testing.T) {
3624 ctx := testCc(t, `
3625 cc_library_shared {
3626 name: "libfoo",
3627 srcs: ["foo.c"],
3628 min_sdk_version: "29",
3629 }`)
3630
3631 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3632 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3633}
3634
Jiyong Parkfdaa5f72021-03-19 22:18:04 +09003635func TestMinSdkVersionsOfCrtObjects(t *testing.T) {
3636 ctx := testCc(t, `
3637 cc_object {
3638 name: "crt_foo",
3639 srcs: ["foo.c"],
3640 crt: true,
3641 stl: "none",
3642 min_sdk_version: "28",
3643
3644 }`)
3645
3646 arch := "android_arm64_armv8-a"
3647 for _, v := range []string{"", "28", "29", "30", "current"} {
3648 var variant string
3649 if v == "" {
3650 variant = arch
3651 } else {
3652 variant = arch + "_sdk_" + v
3653 }
3654 cflags := ctx.ModuleForTests("crt_foo", variant).Rule("cc").Args["cFlags"]
3655 vNum := v
3656 if v == "current" || v == "" {
3657 vNum = "10000"
3658 }
3659 expected := "-target aarch64-linux-android" + vNum + " "
3660 android.AssertStringDoesContain(t, "cflag", cflags, expected)
3661 }
3662}
3663
3664func TestUseCrtObjectOfCorrectVersion(t *testing.T) {
3665 ctx := testCc(t, `
3666 cc_binary {
3667 name: "bin",
3668 srcs: ["foo.c"],
3669 stl: "none",
3670 min_sdk_version: "29",
3671 sdk_version: "current",
3672 }
3673 `)
3674
3675 // Sdk variant uses the crt object of the matching min_sdk_version
3676 variant := "android_arm64_armv8-a_sdk"
3677 crt := ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3678 android.AssertStringDoesContain(t, "crt dep of sdk variant", crt,
3679 variant+"_29/crtbegin_dynamic.o")
3680
3681 // platform variant uses the crt object built for platform
3682 variant = "android_arm64_armv8-a"
3683 crt = ctx.ModuleForTests("bin", variant).Rule("ld").Args["crtBegin"]
3684 android.AssertStringDoesContain(t, "crt dep of platform variant", crt,
3685 variant+"/crtbegin_dynamic.o")
3686}
3687
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003688type MemtagNoteType int
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003689
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003690const (
3691 None MemtagNoteType = iota + 1
3692 Sync
3693 Async
3694)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003695
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003696func (t MemtagNoteType) str() string {
3697 switch t {
3698 case None:
3699 return "none"
3700 case Sync:
3701 return "sync"
3702 case Async:
3703 return "async"
3704 default:
3705 panic("invalid note type")
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003706 }
3707}
3708
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003709func checkHasMemtagNote(t *testing.T, m android.TestingModule, expected MemtagNoteType) {
3710 note_async := "note_memtag_heap_async"
3711 note_sync := "note_memtag_heap_sync"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003712
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003713 found := None
3714 implicits := m.Rule("ld").Implicits
3715 for _, lib := range implicits {
3716 if strings.Contains(lib.Rel(), note_async) {
3717 found = Async
3718 break
3719 } else if strings.Contains(lib.Rel(), note_sync) {
3720 found = Sync
3721 break
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003722 }
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003723 }
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003724
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003725 if found != expected {
3726 t.Errorf("Wrong Memtag note in target %q: found %q, expected %q", m.Module().(*Module).Name(), found.str(), expected.str())
3727 }
3728}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003729
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003730var prepareForTestWithMemtagHeap = android.GroupFixturePreparers(
3731 android.FixtureModifyMockFS(func(fs android.MockFS) {
3732 templateBp := `
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003733 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003734 name: "%[1]s_test",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003735 gtest: false,
3736 }
3737
3738 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003739 name: "%[1]s_test_false",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003740 gtest: false,
3741 sanitize: { memtag_heap: false },
3742 }
3743
3744 cc_test {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003745 name: "%[1]s_test_true",
3746 gtest: false,
3747 sanitize: { memtag_heap: true },
3748 }
3749
3750 cc_test {
3751 name: "%[1]s_test_true_nodiag",
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003752 gtest: false,
3753 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3754 }
3755
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003756 cc_test {
3757 name: "%[1]s_test_true_diag",
3758 gtest: false,
3759 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
3760 }
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003761
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003762 cc_binary {
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003763 name: "%[1]s_binary",
3764 }
3765
3766 cc_binary {
3767 name: "%[1]s_binary_false",
3768 sanitize: { memtag_heap: false },
3769 }
3770
3771 cc_binary {
3772 name: "%[1]s_binary_true",
3773 sanitize: { memtag_heap: true },
3774 }
3775
3776 cc_binary {
3777 name: "%[1]s_binary_true_nodiag",
3778 sanitize: { memtag_heap: true, diag: { memtag_heap: false } },
3779 }
3780
3781 cc_binary {
3782 name: "%[1]s_binary_true_diag",
3783 sanitize: { memtag_heap: true, diag: { memtag_heap: true } },
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003784 }
3785 `
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003786 subdirDefaultBp := fmt.Sprintf(templateBp, "default")
3787 subdirExcludeBp := fmt.Sprintf(templateBp, "exclude")
3788 subdirSyncBp := fmt.Sprintf(templateBp, "sync")
3789 subdirAsyncBp := fmt.Sprintf(templateBp, "async")
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003790
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003791 fs.Merge(android.MockFS{
3792 "subdir_default/Android.bp": []byte(subdirDefaultBp),
3793 "subdir_exclude/Android.bp": []byte(subdirExcludeBp),
3794 "subdir_sync/Android.bp": []byte(subdirSyncBp),
3795 "subdir_async/Android.bp": []byte(subdirAsyncBp),
3796 })
3797 }),
3798 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3799 variables.MemtagHeapExcludePaths = []string{"subdir_exclude"}
3800 variables.MemtagHeapSyncIncludePaths = []string{"subdir_sync"}
3801 variables.MemtagHeapAsyncIncludePaths = []string{"subdir_async"}
3802 }),
3803)
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003804
3805func TestSanitizeMemtagHeap(t *testing.T) {
3806 variant := "android_arm64_armv8-a"
3807
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003808 result := ccFixtureFactory.Extend(prepareForTestWithMemtagHeap).RunTest(t)
3809 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003810
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003811 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3812 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3813 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3814 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3815 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3816
3817 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), None)
3818 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3819 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3820 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3821 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3822
3823 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3824 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3825 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3826 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3827 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3828
3829 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3830 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3831 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3832 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3833 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3834
3835 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3836 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3837 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3838 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3839 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3840
3841 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3842 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3843 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3844 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3845 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3846
3847 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3848 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3849 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3850 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3851 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3852
3853 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3854 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3855 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3856 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3857 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3858}
3859
3860func TestSanitizeMemtagHeapWithSanitizeDevice(t *testing.T) {
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003861 variant := "android_arm64_armv8-a"
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003862
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003863 result := ccFixtureFactory.Extend(
3864 prepareForTestWithMemtagHeap,
3865 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3866 variables.SanitizeDevice = []string{"memtag_heap"}
3867 }),
3868 ).RunTest(t)
3869 ctx := result.TestContext
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003870
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003871 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3872 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3873 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Async)
3874 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3875 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
Evgenii Stepanov4beaa0c2021-01-05 16:41:26 -08003876
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003877 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Async)
3878 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3879 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Async)
3880 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3881 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3882
3883 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3884 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3885 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Async)
3886 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3887 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3888
3889 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3890 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3891 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Async)
3892 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3893 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3894
3895 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3896 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3897 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Async)
3898 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3899 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3900
3901 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Async)
3902 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3903 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Async)
3904 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3905 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3906
3907 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3908 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3909 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3910 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3911 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3912
3913 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3914 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3915 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3916 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3917 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
3918}
3919
3920func TestSanitizeMemtagHeapWithSanitizeDeviceDiag(t *testing.T) {
3921 variant := "android_arm64_armv8-a"
3922
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003923 result := ccFixtureFactory.Extend(
3924 prepareForTestWithMemtagHeap,
3925 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3926 variables.SanitizeDevice = []string{"memtag_heap"}
3927 variables.SanitizeDeviceDiag = []string{"memtag_heap"}
3928 }),
3929 ).RunTest(t)
3930 ctx := result.TestContext
Evgenii Stepanov04896ca2021-01-12 18:28:33 -08003931
3932 checkHasMemtagNote(t, ctx.ModuleForTests("default_test", variant), Sync)
3933 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_false", variant), None)
3934 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true", variant), Sync)
3935 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_nodiag", variant), Async)
3936 checkHasMemtagNote(t, ctx.ModuleForTests("default_test_true_diag", variant), Sync)
3937
3938 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary", variant), Sync)
3939 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_false", variant), None)
3940 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true", variant), Sync)
3941 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_nodiag", variant), Async)
3942 checkHasMemtagNote(t, ctx.ModuleForTests("default_binary_true_diag", variant), Sync)
3943
3944 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test", variant), Sync)
3945 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_false", variant), None)
3946 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true", variant), Sync)
3947 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_nodiag", variant), Async)
3948 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_test_true_diag", variant), Sync)
3949
3950 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary", variant), None)
3951 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_false", variant), None)
3952 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true", variant), Sync)
3953 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_nodiag", variant), Async)
3954 checkHasMemtagNote(t, ctx.ModuleForTests("exclude_binary_true_diag", variant), Sync)
3955
3956 checkHasMemtagNote(t, ctx.ModuleForTests("async_test", variant), Sync)
3957 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_false", variant), None)
3958 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true", variant), Sync)
3959 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_nodiag", variant), Async)
3960 checkHasMemtagNote(t, ctx.ModuleForTests("async_test_true_diag", variant), Sync)
3961
3962 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary", variant), Sync)
3963 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_false", variant), None)
3964 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true", variant), Sync)
3965 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_nodiag", variant), Async)
3966 checkHasMemtagNote(t, ctx.ModuleForTests("async_binary_true_diag", variant), Sync)
3967
3968 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test", variant), Sync)
3969 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_false", variant), None)
3970 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true", variant), Sync)
3971 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_nodiag", variant), Async)
3972 checkHasMemtagNote(t, ctx.ModuleForTests("sync_test_true_diag", variant), Sync)
3973
3974 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary", variant), Sync)
3975 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_false", variant), None)
3976 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true", variant), Sync)
3977 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_nodiag", variant), Async)
3978 checkHasMemtagNote(t, ctx.ModuleForTests("sync_binary_true_diag", variant), Sync)
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003979}
Paul Duffin3cb603e2021-02-19 13:57:10 +00003980
3981func TestIncludeDirsExporting(t *testing.T) {
3982
3983 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3984 // embedded newline characters alone.
3985 trimIndentingSpaces := func(s string) string {
3986 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3987 }
3988
3989 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
3990 t.Helper()
3991 expected = trimIndentingSpaces(expected)
3992 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
3993 if expected != actual {
3994 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
3995 }
3996 }
3997
3998 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
3999
4000 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4001 t.Helper()
4002 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4003 name := module.Name()
4004
4005 for _, checker := range checkers {
4006 checker(t, name, exported)
4007 }
4008 }
4009
4010 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4011 return func(t *testing.T, name string, exported FlagExporterInfo) {
4012 t.Helper()
4013 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4014 }
4015 }
4016
4017 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4018 return func(t *testing.T, name string, exported FlagExporterInfo) {
4019 t.Helper()
4020 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4021 }
4022 }
4023
4024 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4025 return func(t *testing.T, name string, exported FlagExporterInfo) {
4026 t.Helper()
4027 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4028 }
4029 }
4030
4031 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4032 return func(t *testing.T, name string, exported FlagExporterInfo) {
4033 t.Helper()
4034 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4035 }
4036 }
4037
4038 genRuleModules := `
4039 genrule {
4040 name: "genrule_foo",
4041 cmd: "generate-foo",
4042 out: [
4043 "generated_headers/foo/generated_header.h",
4044 ],
4045 export_include_dirs: [
4046 "generated_headers",
4047 ],
4048 }
4049
4050 genrule {
4051 name: "genrule_bar",
4052 cmd: "generate-bar",
4053 out: [
4054 "generated_headers/bar/generated_header.h",
4055 ],
4056 export_include_dirs: [
4057 "generated_headers",
4058 ],
4059 }
4060 `
4061
4062 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4063 ctx := testCc(t, genRuleModules+`
4064 cc_library {
4065 name: "libfoo",
4066 srcs: ["foo.c"],
4067 export_include_dirs: ["foo/standard"],
4068 export_system_include_dirs: ["foo/system"],
4069 generated_headers: ["genrule_foo"],
4070 export_generated_headers: ["genrule_foo"],
4071 }
4072
4073 cc_library {
4074 name: "libbar",
4075 srcs: ["bar.c"],
4076 shared_libs: ["libfoo"],
4077 export_include_dirs: ["bar/standard"],
4078 export_system_include_dirs: ["bar/system"],
4079 generated_headers: ["genrule_bar"],
4080 export_generated_headers: ["genrule_bar"],
4081 }
4082 `)
4083 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4084 checkIncludeDirs(t, ctx, foo,
4085 expectedIncludeDirs(`
4086 foo/standard
4087 .intermediates/genrule_foo/gen/generated_headers
4088 `),
4089 expectedSystemIncludeDirs(`foo/system`),
4090 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4091 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4092 )
4093
4094 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4095 checkIncludeDirs(t, ctx, bar,
4096 expectedIncludeDirs(`
4097 bar/standard
4098 .intermediates/genrule_bar/gen/generated_headers
4099 `),
4100 expectedSystemIncludeDirs(`bar/system`),
4101 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4102 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4103 )
4104 })
4105
4106 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4107 ctx := testCc(t, genRuleModules+`
4108 cc_library {
4109 name: "libfoo",
4110 srcs: ["foo.c"],
4111 export_include_dirs: ["foo/standard"],
4112 export_system_include_dirs: ["foo/system"],
4113 generated_headers: ["genrule_foo"],
4114 export_generated_headers: ["genrule_foo"],
4115 }
4116
4117 cc_library {
4118 name: "libbar",
4119 srcs: ["bar.c"],
4120 whole_static_libs: ["libfoo"],
4121 export_include_dirs: ["bar/standard"],
4122 export_system_include_dirs: ["bar/system"],
4123 generated_headers: ["genrule_bar"],
4124 export_generated_headers: ["genrule_bar"],
4125 }
4126 `)
4127 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4128 checkIncludeDirs(t, ctx, foo,
4129 expectedIncludeDirs(`
4130 foo/standard
4131 .intermediates/genrule_foo/gen/generated_headers
4132 `),
4133 expectedSystemIncludeDirs(`foo/system`),
4134 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4135 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4136 )
4137
4138 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4139 checkIncludeDirs(t, ctx, bar,
4140 expectedIncludeDirs(`
4141 bar/standard
4142 foo/standard
4143 .intermediates/genrule_foo/gen/generated_headers
4144 .intermediates/genrule_bar/gen/generated_headers
4145 `),
4146 expectedSystemIncludeDirs(`
4147 bar/system
4148 foo/system
4149 `),
4150 expectedGeneratedHeaders(`
4151 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4152 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4153 `),
4154 expectedOrderOnlyDeps(`
4155 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4156 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4157 `),
4158 )
4159 })
4160
Paul Duffin3cb603e2021-02-19 13:57:10 +00004161 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4162 ctx := testCc(t, genRuleModules+`
4163 cc_library_shared {
4164 name: "libfoo",
4165 srcs: [
4166 "foo.c",
4167 "b.aidl",
4168 "a.proto",
4169 ],
4170 aidl: {
4171 export_aidl_headers: true,
4172 }
4173 }
4174 `)
4175 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4176 checkIncludeDirs(t, ctx, foo,
4177 expectedIncludeDirs(`
4178 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4179 `),
4180 expectedSystemIncludeDirs(``),
4181 expectedGeneratedHeaders(`
4182 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4183 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4184 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004185 `),
4186 expectedOrderOnlyDeps(`
4187 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4188 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4189 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004190 `),
4191 )
4192 })
4193
Paul Duffin3cb603e2021-02-19 13:57:10 +00004194 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4195 ctx := testCc(t, genRuleModules+`
4196 cc_library_shared {
4197 name: "libfoo",
4198 srcs: [
4199 "foo.c",
4200 "b.aidl",
4201 "a.proto",
4202 ],
4203 proto: {
4204 export_proto_headers: true,
4205 }
4206 }
4207 `)
4208 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4209 checkIncludeDirs(t, ctx, foo,
4210 expectedIncludeDirs(`
4211 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4212 `),
4213 expectedSystemIncludeDirs(``),
4214 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004215 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4216 `),
4217 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004218 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4219 `),
4220 )
4221 })
4222
Paul Duffin33056e82021-02-19 13:49:08 +00004223 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004224 ctx := testCc(t, genRuleModules+`
4225 cc_library_shared {
4226 name: "libfoo",
4227 srcs: [
4228 "foo.c",
4229 "a.sysprop",
4230 "b.aidl",
4231 "a.proto",
4232 ],
4233 }
4234 `)
4235 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4236 checkIncludeDirs(t, ctx, foo,
4237 expectedIncludeDirs(`
4238 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4239 `),
4240 expectedSystemIncludeDirs(``),
4241 expectedGeneratedHeaders(`
4242 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004243 `),
4244 expectedOrderOnlyDeps(`
4245 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4246 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004247 `),
4248 )
4249 })
4250}