blob: ad2ccae42c88689b5f98ef3e66c3cf115122bda1 [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 Duffin2e6f90e2021-03-22 23:20:25 +000033var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000034 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000035 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
36 variables.DeviceVndkVersion = StringPtr("current")
37 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090038 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000039 }),
40)
41
Paul Duffin8567f222021-03-23 00:02:06 +000042// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000043//
44// See testCc for an explanation as to how to stop using this deprecated method.
45//
46// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080047func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070048 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000049 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000050 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090051}
52
Paul Duffin8567f222021-03-23 00:02:06 +000053// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000054//
Paul Duffin8567f222021-03-23 00:02:06 +000055// Do not add any new usages of this, instead use the prepareForCcTest directly as it makes it much
Paul Duffin02a3d652021-02-24 18:51:54 +000056// easier to customize the test behavior.
57//
58// If it is necessary to customize the behavior of an existing test that uses this then please first
Paul Duffin8567f222021-03-23 00:02:06 +000059// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000060// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
61// that it did not change the test behavior unexpectedly.
62//
63// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080064func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080065 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000066 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000067 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080068}
69
Paul Duffin8567f222021-03-23 00:02:06 +000070// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000071//
72// See testCc for an explanation as to how to stop using this deprecated method.
73//
74// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080075func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080076 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000077 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090078 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080079
Colin Cross98be1bb2019-12-13 20:41:13 -080080 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080081}
82
Paul Duffin8567f222021-03-23 00:02:06 +000083// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000084//
85// See testCc for an explanation as to how to stop using this deprecated method.
86//
87// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090088func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
89 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000090 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090091 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090092 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090093
94 return testCcWithConfig(t, config)
95}
96
Paul Duffin8567f222021-03-23 00:02:06 +000097// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000098//
99// See testCc for an explanation as to how to stop using this deprecated method.
100//
101// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900102func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800103 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800104
Paul Duffin8567f222021-03-23 00:02:06 +0000105 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000106 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
107 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800108}
109
Paul Duffin8567f222021-03-23 00:02:06 +0000110// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000111//
112// See testCc for an explanation as to how to stop using this deprecated method.
113//
114// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900115func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900116 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000117 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900118 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900119 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900120 testCcErrorWithConfig(t, pattern, config)
121 return
122}
123
Paul Duffin8567f222021-03-23 00:02:06 +0000124// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000125//
126// See testCc for an explanation as to how to stop using this deprecated method.
127//
128// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900129func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900130 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000131 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900132 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
133 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900134 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900135 testCcErrorWithConfig(t, pattern, config)
136 return
137}
138
Logan Chienf3511742017-10-31 18:04:35 +0800139const (
Colin Cross7113d202019-11-20 16:39:12 -0800140 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900141 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
142 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800143 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800144)
145
Paul Duffindb462dd2021-03-21 22:01:55 +0000146// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
147// running it in a fixture that requires all source files to exist.
148func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
149 android.GroupFixturePreparers(
150 PrepareForTestWithCcDefaultModules,
151 android.PrepareForTestDisallowNonExistentPaths,
152 ).RunTest(t)
153}
154
Jiyong Park6a43f042017-10-12 23:05:00 +0900155func TestVendorSrc(t *testing.T) {
156 ctx := testCc(t, `
157 cc_library {
158 name: "libTest",
159 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700160 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800161 nocrt: true,
162 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900163 vendor_available: true,
164 target: {
165 vendor: {
166 srcs: ["bar.c"],
167 },
168 },
169 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900170 `)
171
Logan Chienf3511742017-10-31 18:04:35 +0800172 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900173 var objs []string
174 for _, o := range ld.Inputs {
175 objs = append(objs, o.Base())
176 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800177 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900178 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
179 }
180}
181
Justin Yun7f99ec72021-04-12 13:19:28 +0900182func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
183 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
184 partitionDefined := false
185 checkPartition := func(specific bool, partition string) {
186 if specific {
187 if expected != partition && !partitionDefined {
188 // The variant is installed to the 'partition'
189 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
190 }
191 partitionDefined = true
192 } else {
193 // The variant is not installed to the 'partition'
194 if expected == partition {
195 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
196 }
197 }
198 }
199 socSpecific := func(m *Module) bool {
200 return m.SocSpecific() || m.socSpecificModuleContext()
201 }
202 deviceSpecific := func(m *Module) bool {
203 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
204 }
205 productSpecific := func(m *Module) bool {
206 return m.ProductSpecific() || m.productSpecificModuleContext()
207 }
208 systemExtSpecific := func(m *Module) bool {
209 return m.SystemExtSpecific()
210 }
211 checkPartition(socSpecific(mod), "vendor")
212 checkPartition(deviceSpecific(mod), "odm")
213 checkPartition(productSpecific(mod), "product")
214 checkPartition(systemExtSpecific(mod), "system_ext")
215 if !partitionDefined && expected != "system" {
216 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
217 " but installed to system partition", variant, name, expected)
218 }
219}
220
221func TestInstallPartition(t *testing.T) {
222 t.Helper()
223 ctx := prepareForCcTest.RunTestWithBp(t, `
224 cc_library {
225 name: "libsystem",
226 }
227 cc_library {
228 name: "libsystem_ext",
229 system_ext_specific: true,
230 }
231 cc_library {
232 name: "libproduct",
233 product_specific: true,
234 }
235 cc_library {
236 name: "libvendor",
237 vendor: true,
238 }
239 cc_library {
240 name: "libodm",
241 device_specific: true,
242 }
243 cc_library {
244 name: "liball_available",
245 vendor_available: true,
246 product_available: true,
247 }
248 cc_library {
249 name: "libsystem_ext_all_available",
250 system_ext_specific: true,
251 vendor_available: true,
252 product_available: true,
253 }
254 cc_library {
255 name: "liball_available_odm",
256 odm_available: true,
257 product_available: true,
258 }
259 cc_library {
260 name: "libproduct_vendoravailable",
261 product_specific: true,
262 vendor_available: true,
263 }
264 cc_library {
265 name: "libproduct_odmavailable",
266 product_specific: true,
267 odm_available: true,
268 }
269 `).TestContext
270
271 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
272 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
273 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
274 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
275 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
276
277 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
278 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
279 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
280
281 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
282 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
283 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
284
285 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
286 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
287 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
288
289 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
290 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
291
292 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
293 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
294}
295
Logan Chienf3511742017-10-31 18:04:35 +0800296func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900297 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800298
Logan Chiend3c59a22018-03-29 14:08:15 +0800299 t.Helper()
300
Justin Yun0ecf0b22020-02-28 15:07:59 +0900301 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800302
303 // Check library properties.
304 lib, ok := mod.compiler.(*libraryDecorator)
305 if !ok {
306 t.Errorf("%q must have libraryDecorator", name)
307 } else if lib.baseInstaller.subDir != subDir {
308 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
309 lib.baseInstaller.subDir)
310 }
311
312 // Check VNDK properties.
313 if mod.vndkdep == nil {
314 t.Fatalf("%q must have `vndkdep`", name)
315 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700316 if !mod.IsVndk() {
317 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800318 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400319 if mod.IsVndkSp() != isVndkSp {
320 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800321 }
322
323 // Check VNDK extension properties.
324 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500325 if mod.IsVndkExt() != isVndkExt {
326 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800327 }
328
329 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
330 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
331 }
332}
333
Jooyung Han2216fb12019-11-06 16:46:15 +0900334func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
335 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800336 content := android.ContentFromFileRuleForTests(t, params)
337 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900338 assertArrayString(t, actual, expected)
339}
340
Jooyung Han097087b2019-10-22 19:32:18 +0900341func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
342 t.Helper()
343 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900344 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
345}
346
347func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
348 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800349 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800350 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900351}
352
Logan Chienf3511742017-10-31 18:04:35 +0800353func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800354 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800355 cc_library {
356 name: "libvndk",
357 vendor_available: true,
358 vndk: {
359 enabled: true,
360 },
361 nocrt: true,
362 }
363
364 cc_library {
365 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900366 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800367 vndk: {
368 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900369 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800370 },
371 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900372 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800373 }
374
375 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900376 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800377 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900378 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800379 vndk: {
380 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900381 },
382 nocrt: true,
383 target: {
384 vendor: {
385 cflags: ["-DTEST"],
386 },
387 product: {
388 cflags: ["-DTEST"],
389 },
390 },
391 }
392
393 cc_library {
394 name: "libvndk_sp",
395 vendor_available: true,
396 vndk: {
397 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800398 support_system_process: true,
399 },
400 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900401 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800402 }
403
404 cc_library {
405 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900406 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800407 vndk: {
408 enabled: true,
409 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900410 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800411 },
412 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900413 target: {
414 vendor: {
415 suffix: "-x",
416 },
417 },
Logan Chienf3511742017-10-31 18:04:35 +0800418 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900419
420 cc_library {
421 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900422 vendor_available: true,
423 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900424 vndk: {
425 enabled: true,
426 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900427 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900428 },
429 nocrt: true,
430 target: {
431 vendor: {
432 suffix: "-x",
433 },
434 product: {
435 suffix: "-x",
436 },
437 },
438 }
439
Justin Yun450ae722021-04-16 19:58:18 +0900440 cc_library {
441 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700442 llndk: {
443 symbol_file: "libllndk.map.txt",
444 export_llndk_headers: ["libllndk_headers"],
445 }
Justin Yun450ae722021-04-16 19:58:18 +0900446 }
447
Justin Yun611e8862021-05-24 18:17:33 +0900448 cc_library {
449 name: "libclang_rt.hwasan-llndk",
450 llndk: {
451 symbol_file: "libclang_rt.hwasan.map.txt",
452 }
453 }
454
Colin Cross627280f2021-04-26 16:53:58 -0700455 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900456 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700457 llndk: {
458 llndk_headers: true,
459 },
Justin Yun450ae722021-04-16 19:58:18 +0900460 export_include_dirs: ["include"],
461 }
462
Colin Crosse4e44bc2020-12-28 13:50:21 -0800463 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900464 name: "llndk.libraries.txt",
465 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800466 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900467 name: "vndkcore.libraries.txt",
468 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800469 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900470 name: "vndksp.libraries.txt",
471 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800472 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900473 name: "vndkprivate.libraries.txt",
474 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800475 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900476 name: "vndkproduct.libraries.txt",
477 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800478 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900479 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800480 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900481 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800482 `
483
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000484 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800485 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900486 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900487 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800488
489 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800490
Jooyung Han261e1582020-10-20 18:54:21 +0900491 // subdir == "" because VNDK libs are not supposed to be installed separately.
492 // They are installed as part of VNDK APEX instead.
493 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
494 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900495 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900496 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
497 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900498 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900499
Justin Yun6977e8a2020-10-29 18:24:11 +0900500 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
501 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900502
Inseob Kim1f086e22019-05-09 13:29:15 +0900503 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900504 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000505 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900506
507 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
508 "arm64", "armv8-a"))
509 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
510 "arm", "armv7-a-neon"))
511
512 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
513 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900514 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
515
Inseob Kim1f086e22019-05-09 13:29:15 +0900516 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
517 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900518 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900519
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900520 variant := "android_vendor.29_arm64_armv8-a_shared"
521 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900522
Inseob Kim7f283f42020-06-01 21:53:49 +0900523 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
524
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400525 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
526 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
527 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
528 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
529 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
530 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
531 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
532 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900533
Jooyung Han39edb6c2019-11-06 16:53:07 +0900534 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800535 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
536 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
537 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
538 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
539 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900540
Jooyung Han097087b2019-10-22 19:32:18 +0900541 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
542 "LLNDK: libc.so",
543 "LLNDK: libdl.so",
544 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900545 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900546 "LLNDK: libm.so",
547 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900548 "VNDK-SP: libvndk_sp-x.so",
549 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900550 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900551 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900552 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900553 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900554 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900555 "VNDK-private: libvndk-private.so",
556 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900557 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900558 "VNDK-product: libc++.so",
559 "VNDK-product: libvndk_product.so",
560 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900561 })
Justin Yun611e8862021-05-24 18:17:33 +0900562 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt", []string{"libc.so", "libclang_rt.hwasan-llndk.so", "libdl.so", "libft2.so", "libllndk.so", "libm.so"})
Justin Yun6977e8a2020-10-29 18:24:11 +0900563 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
564 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
565 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 +0900566 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 +0900567 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
568}
569
Yo Chiangbba545e2020-06-09 16:15:37 +0800570func TestVndkWithHostSupported(t *testing.T) {
571 ctx := testCc(t, `
572 cc_library {
573 name: "libvndk_host_supported",
574 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900575 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800576 vndk: {
577 enabled: true,
578 },
579 host_supported: true,
580 }
581
582 cc_library {
583 name: "libvndk_host_supported_but_disabled_on_device",
584 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900585 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800586 vndk: {
587 enabled: true,
588 },
589 host_supported: true,
590 enabled: false,
591 target: {
592 host: {
593 enabled: true,
594 }
595 }
596 }
597
Colin Crosse4e44bc2020-12-28 13:50:21 -0800598 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800599 name: "vndkcore.libraries.txt",
600 }
601 `)
602
603 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
604}
605
Jooyung Han2216fb12019-11-06 16:46:15 +0900606func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800607 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800608 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900609 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800610 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800611 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000612 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800613 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900614 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800615 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900616
Colin Cross45bce852021-11-11 22:47:54 -0800617 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700618 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900619 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900620}
621
622func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800623 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900624 cc_library {
625 name: "libvndk",
626 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900627 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900628 vndk: {
629 enabled: true,
630 },
631 nocrt: true,
632 }
633
634 cc_library {
635 name: "libvndk_sp",
636 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900637 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900638 vndk: {
639 enabled: true,
640 support_system_process: true,
641 },
642 nocrt: true,
643 }
644
645 cc_library {
646 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900647 vendor_available: true,
648 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900649 vndk: {
650 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900651 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900652 },
653 nocrt: true,
654 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900655
Colin Crosse4e44bc2020-12-28 13:50:21 -0800656 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900657 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800658 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900659 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800660 `
661
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000662 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800663 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900664 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800665 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
666
667 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
668
669 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900670
Jooyung Han2216fb12019-11-06 16:46:15 +0900671 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900672}
673
Chris Parsons79d66a52020-06-05 17:26:16 -0400674func TestDataLibs(t *testing.T) {
675 bp := `
676 cc_test_library {
677 name: "test_lib",
678 srcs: ["test_lib.cpp"],
679 gtest: false,
680 }
681
682 cc_test {
683 name: "main_test",
684 data_libs: ["test_lib"],
685 gtest: false,
686 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400687 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400688
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000689 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400690 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900691 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400692 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
693
694 ctx := testCcWithConfig(t, config)
695 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
696 testBinary := module.(*Module).linker.(*testBinary)
697 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
698 if err != nil {
699 t.Errorf("Expected cc_test to produce output files, error: %s", err)
700 return
701 }
702 if len(outputFiles) != 1 {
703 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
704 return
705 }
706 if len(testBinary.dataPaths()) != 1 {
707 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
708 return
709 }
710
711 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400712 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400713
714 if !strings.HasSuffix(outputPath, "/main_test") {
715 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
716 return
717 }
718 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
719 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
720 return
721 }
722}
723
Chris Parsons216e10a2020-07-09 17:12:52 -0400724func TestDataLibsRelativeInstallPath(t *testing.T) {
725 bp := `
726 cc_test_library {
727 name: "test_lib",
728 srcs: ["test_lib.cpp"],
729 relative_install_path: "foo/bar/baz",
730 gtest: false,
731 }
732
733 cc_test {
734 name: "main_test",
735 data_libs: ["test_lib"],
736 gtest: false,
737 }
738 `
739
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000740 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400741 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900742 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400743 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
744
745 ctx := testCcWithConfig(t, config)
746 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
747 testBinary := module.(*Module).linker.(*testBinary)
748 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
749 if err != nil {
750 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
751 }
752 if len(outputFiles) != 1 {
753 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
754 }
755 if len(testBinary.dataPaths()) != 1 {
756 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
757 }
758
759 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400760
761 if !strings.HasSuffix(outputPath, "/main_test") {
762 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
763 }
Colin Crossaa255532020-07-03 13:18:24 -0700764 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400765 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
766 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400767 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400768 }
769}
770
Jooyung Han0302a842019-10-30 18:43:49 +0900771func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900772 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900773 cc_library {
774 name: "libvndk",
775 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900776 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900777 vndk: {
778 enabled: true,
779 },
780 nocrt: true,
781 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900782 cc_library {
783 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900784 vendor_available: true,
785 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900786 vndk: {
787 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900788 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900789 },
790 nocrt: true,
791 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800792
793 cc_library {
794 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700795 llndk: {
796 symbol_file: "libllndk.map.txt",
797 export_llndk_headers: ["libllndk_headers"],
798 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800799 }
800
Colin Cross627280f2021-04-26 16:53:58 -0700801 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800802 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700803 llndk: {
804 symbol_file: "libllndk.map.txt",
805 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800806 export_include_dirs: ["include"],
807 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900808 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900809
810 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
811 "LLNDK: libc.so",
812 "LLNDK: libdl.so",
813 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800814 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900815 "LLNDK: libm.so",
816 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900817 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900818 "VNDK-core: libvndk.so",
819 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900820 "VNDK-private: libvndk-private.so",
821 "VNDK-product: libc++.so",
822 "VNDK-product: libvndk-private.so",
823 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900824 })
Logan Chienf3511742017-10-31 18:04:35 +0800825}
826
Justin Yun63e9ec72020-10-29 16:49:43 +0900827func TestVndkModuleError(t *testing.T) {
828 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900829 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900830 cc_library {
831 name: "libvndk",
832 vndk: {
833 enabled: true,
834 },
835 nocrt: true,
836 }
837 `)
838
Justin Yunc0d8c492021-01-07 17:45:31 +0900839 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900840 cc_library {
841 name: "libvndk",
842 product_available: true,
843 vndk: {
844 enabled: true,
845 },
846 nocrt: true,
847 }
848 `)
849
Justin Yun6977e8a2020-10-29 18:24:11 +0900850 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
851 cc_library {
852 name: "libvndkprop",
853 vendor_available: true,
854 product_available: true,
855 vndk: {
856 enabled: true,
857 },
858 nocrt: true,
859 target: {
860 vendor: {
861 cflags: ["-DTEST",],
862 },
863 },
864 }
865 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900866}
867
Logan Chiend3c59a22018-03-29 14:08:15 +0800868func TestVndkDepError(t *testing.T) {
869 // Check whether an error is emitted when a VNDK lib depends on a system lib.
870 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
871 cc_library {
872 name: "libvndk",
873 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900874 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800875 vndk: {
876 enabled: true,
877 },
878 shared_libs: ["libfwk"], // Cause error
879 nocrt: true,
880 }
881
882 cc_library {
883 name: "libfwk",
884 nocrt: true,
885 }
886 `)
887
888 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
889 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
890 cc_library {
891 name: "libvndk",
892 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900893 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800894 vndk: {
895 enabled: true,
896 },
897 shared_libs: ["libvendor"], // Cause error
898 nocrt: true,
899 }
900
901 cc_library {
902 name: "libvendor",
903 vendor: true,
904 nocrt: true,
905 }
906 `)
907
908 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
909 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
910 cc_library {
911 name: "libvndk_sp",
912 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900913 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800914 vndk: {
915 enabled: true,
916 support_system_process: true,
917 },
918 shared_libs: ["libfwk"], // Cause error
919 nocrt: true,
920 }
921
922 cc_library {
923 name: "libfwk",
924 nocrt: true,
925 }
926 `)
927
928 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
929 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
930 cc_library {
931 name: "libvndk_sp",
932 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900933 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800934 vndk: {
935 enabled: true,
936 support_system_process: true,
937 },
938 shared_libs: ["libvendor"], // Cause error
939 nocrt: true,
940 }
941
942 cc_library {
943 name: "libvendor",
944 vendor: true,
945 nocrt: true,
946 }
947 `)
948
949 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
950 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
951 cc_library {
952 name: "libvndk_sp",
953 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900954 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800955 vndk: {
956 enabled: true,
957 support_system_process: true,
958 },
959 shared_libs: ["libvndk"], // Cause error
960 nocrt: true,
961 }
962
963 cc_library {
964 name: "libvndk",
965 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900966 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800967 vndk: {
968 enabled: true,
969 },
970 nocrt: true,
971 }
972 `)
Jooyung Hana70f0672019-01-18 15:20:43 +0900973
974 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
975 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
976 cc_library {
977 name: "libvndk",
978 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900979 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +0900980 vndk: {
981 enabled: true,
982 },
983 shared_libs: ["libnonvndk"],
984 nocrt: true,
985 }
986
987 cc_library {
988 name: "libnonvndk",
989 vendor_available: true,
990 nocrt: true,
991 }
992 `)
993
994 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
995 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
996 cc_library {
997 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +0900998 vendor_available: true,
999 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001000 vndk: {
1001 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001002 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001003 },
1004 shared_libs: ["libnonvndk"],
1005 nocrt: true,
1006 }
1007
1008 cc_library {
1009 name: "libnonvndk",
1010 vendor_available: true,
1011 nocrt: true,
1012 }
1013 `)
1014
1015 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1016 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1017 cc_library {
1018 name: "libvndksp",
1019 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001020 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001021 vndk: {
1022 enabled: true,
1023 support_system_process: true,
1024 },
1025 shared_libs: ["libnonvndk"],
1026 nocrt: true,
1027 }
1028
1029 cc_library {
1030 name: "libnonvndk",
1031 vendor_available: true,
1032 nocrt: true,
1033 }
1034 `)
1035
1036 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1037 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1038 cc_library {
1039 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001040 vendor_available: true,
1041 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001042 vndk: {
1043 enabled: true,
1044 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001045 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001046 },
1047 shared_libs: ["libnonvndk"],
1048 nocrt: true,
1049 }
1050
1051 cc_library {
1052 name: "libnonvndk",
1053 vendor_available: true,
1054 nocrt: true,
1055 }
1056 `)
1057}
1058
1059func TestDoubleLoadbleDep(t *testing.T) {
1060 // okay to link : LLNDK -> double_loadable VNDK
1061 testCc(t, `
1062 cc_library {
1063 name: "libllndk",
1064 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001065 llndk: {
1066 symbol_file: "libllndk.map.txt",
1067 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001068 }
1069
1070 cc_library {
1071 name: "libdoubleloadable",
1072 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001073 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001074 vndk: {
1075 enabled: true,
1076 },
1077 double_loadable: true,
1078 }
1079 `)
1080 // okay to link : LLNDK -> VNDK-SP
1081 testCc(t, `
1082 cc_library {
1083 name: "libllndk",
1084 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001085 llndk: {
1086 symbol_file: "libllndk.map.txt",
1087 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001088 }
1089
1090 cc_library {
1091 name: "libvndksp",
1092 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001093 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001094 vndk: {
1095 enabled: true,
1096 support_system_process: true,
1097 },
1098 }
1099 `)
1100 // okay to link : double_loadable -> double_loadable
1101 testCc(t, `
1102 cc_library {
1103 name: "libdoubleloadable1",
1104 shared_libs: ["libdoubleloadable2"],
1105 vendor_available: true,
1106 double_loadable: true,
1107 }
1108
1109 cc_library {
1110 name: "libdoubleloadable2",
1111 vendor_available: true,
1112 double_loadable: true,
1113 }
1114 `)
1115 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1116 testCc(t, `
1117 cc_library {
1118 name: "libdoubleloadable",
1119 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001120 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001121 vndk: {
1122 enabled: true,
1123 },
1124 double_loadable: true,
1125 shared_libs: ["libnondoubleloadable"],
1126 }
1127
1128 cc_library {
1129 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001130 vendor_available: true,
1131 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001132 vndk: {
1133 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001134 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001135 },
1136 double_loadable: true,
1137 }
1138 `)
1139 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1140 testCc(t, `
1141 cc_library {
1142 name: "libllndk",
1143 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001144 llndk: {
1145 symbol_file: "libllndk.map.txt",
1146 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001147 }
1148
1149 cc_library {
1150 name: "libcoreonly",
1151 shared_libs: ["libvendoravailable"],
1152 }
1153
1154 // indirect dependency of LLNDK
1155 cc_library {
1156 name: "libvendoravailable",
1157 vendor_available: true,
1158 double_loadable: true,
1159 }
1160 `)
1161}
1162
1163func TestDoubleLoadableDepError(t *testing.T) {
1164 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1165 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1166 cc_library {
1167 name: "libllndk",
1168 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001169 llndk: {
1170 symbol_file: "libllndk.map.txt",
1171 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001172 }
1173
1174 cc_library {
1175 name: "libnondoubleloadable",
1176 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001177 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001178 vndk: {
1179 enabled: true,
1180 },
1181 }
1182 `)
1183
1184 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1185 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1186 cc_library {
1187 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001188 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001189 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001190 llndk: {
1191 symbol_file: "libllndk.map.txt",
1192 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001193 }
1194
1195 cc_library {
1196 name: "libnondoubleloadable",
1197 vendor_available: true,
1198 }
1199 `)
1200
Jooyung Hana70f0672019-01-18 15:20:43 +09001201 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1202 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1203 cc_library {
1204 name: "libllndk",
1205 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001206 llndk: {
1207 symbol_file: "libllndk.map.txt",
1208 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 }
1210
1211 cc_library {
1212 name: "libcoreonly",
1213 shared_libs: ["libvendoravailable"],
1214 }
1215
1216 // indirect dependency of LLNDK
1217 cc_library {
1218 name: "libvendoravailable",
1219 vendor_available: true,
1220 }
1221 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001222
1223 // The error is not from 'client' but from 'libllndk'
1224 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1225 cc_library {
1226 name: "client",
1227 vendor_available: true,
1228 double_loadable: true,
1229 shared_libs: ["libllndk"],
1230 }
1231 cc_library {
1232 name: "libllndk",
1233 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001234 llndk: {
1235 symbol_file: "libllndk.map.txt",
1236 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001237 }
1238 cc_library {
1239 name: "libnondoubleloadable",
1240 vendor_available: true,
1241 }
1242 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001243}
1244
Jooyung Han479ca172020-10-19 18:51:07 +09001245func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1246 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1247 cc_library {
1248 name: "libvndksp",
1249 shared_libs: ["libanothervndksp"],
1250 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001251 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001252 vndk: {
1253 enabled: true,
1254 support_system_process: true,
1255 }
1256 }
1257
1258 cc_library {
1259 name: "libllndk",
1260 shared_libs: ["libanothervndksp"],
1261 }
1262
Jooyung Han479ca172020-10-19 18:51:07 +09001263 cc_library {
1264 name: "libanothervndksp",
1265 vendor_available: true,
1266 }
1267 `)
1268}
1269
Logan Chienf3511742017-10-31 18:04:35 +08001270func TestVndkExt(t *testing.T) {
1271 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001272 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001273 cc_library {
1274 name: "libvndk",
1275 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001276 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001277 vndk: {
1278 enabled: true,
1279 },
1280 nocrt: true,
1281 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001282 cc_library {
1283 name: "libvndk2",
1284 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001285 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001286 vndk: {
1287 enabled: true,
1288 },
1289 target: {
1290 vendor: {
1291 suffix: "-suffix",
1292 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001293 product: {
1294 suffix: "-suffix",
1295 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001296 },
1297 nocrt: true,
1298 }
Logan Chienf3511742017-10-31 18:04:35 +08001299
1300 cc_library {
1301 name: "libvndk_ext",
1302 vendor: true,
1303 vndk: {
1304 enabled: true,
1305 extends: "libvndk",
1306 },
1307 nocrt: true,
1308 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001309
1310 cc_library {
1311 name: "libvndk2_ext",
1312 vendor: true,
1313 vndk: {
1314 enabled: true,
1315 extends: "libvndk2",
1316 },
1317 nocrt: true,
1318 }
Logan Chienf3511742017-10-31 18:04:35 +08001319
Justin Yun0ecf0b22020-02-28 15:07:59 +09001320 cc_library {
1321 name: "libvndk_ext_product",
1322 product_specific: true,
1323 vndk: {
1324 enabled: true,
1325 extends: "libvndk",
1326 },
1327 nocrt: true,
1328 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001329
Justin Yun0ecf0b22020-02-28 15:07:59 +09001330 cc_library {
1331 name: "libvndk2_ext_product",
1332 product_specific: true,
1333 vndk: {
1334 enabled: true,
1335 extends: "libvndk2",
1336 },
1337 nocrt: true,
1338 }
1339 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001340 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001341 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1342 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001343 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001344
1345 ctx := testCcWithConfig(t, config)
1346
1347 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1348 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1349
1350 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1351 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1352
1353 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1354 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001355}
1356
Logan Chiend3c59a22018-03-29 14:08:15 +08001357func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001358 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1359 ctx := testCcNoVndk(t, `
1360 cc_library {
1361 name: "libvndk",
1362 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001363 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001364 vndk: {
1365 enabled: true,
1366 },
1367 nocrt: true,
1368 }
1369
1370 cc_library {
1371 name: "libvndk_ext",
1372 vendor: true,
1373 vndk: {
1374 enabled: true,
1375 extends: "libvndk",
1376 },
1377 nocrt: true,
1378 }
1379 `)
1380
1381 // Ensures that the core variant of "libvndk_ext" can be found.
1382 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1383 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1384 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1385 }
1386}
1387
Justin Yun0ecf0b22020-02-28 15:07:59 +09001388func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1389 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001390 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001391 cc_library {
1392 name: "libvndk",
1393 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001394 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001395 vndk: {
1396 enabled: true,
1397 },
1398 nocrt: true,
1399 }
1400
1401 cc_library {
1402 name: "libvndk_ext_product",
1403 product_specific: true,
1404 vndk: {
1405 enabled: true,
1406 extends: "libvndk",
1407 },
1408 nocrt: true,
1409 }
1410 `)
1411
1412 // Ensures that the core variant of "libvndk_ext_product" can be found.
1413 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1414 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1415 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1416 }
1417}
1418
Logan Chienf3511742017-10-31 18:04:35 +08001419func TestVndkExtError(t *testing.T) {
1420 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001421 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001422 cc_library {
1423 name: "libvndk",
1424 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001425 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001426 vndk: {
1427 enabled: true,
1428 },
1429 nocrt: true,
1430 }
1431
1432 cc_library {
1433 name: "libvndk_ext",
1434 vndk: {
1435 enabled: true,
1436 extends: "libvndk",
1437 },
1438 nocrt: true,
1439 }
1440 `)
1441
1442 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1443 cc_library {
1444 name: "libvndk",
1445 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001446 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001447 vndk: {
1448 enabled: true,
1449 },
1450 nocrt: true,
1451 }
1452
1453 cc_library {
1454 name: "libvndk_ext",
1455 vendor: true,
1456 vndk: {
1457 enabled: true,
1458 },
1459 nocrt: true,
1460 }
1461 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001462
1463 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1464 cc_library {
1465 name: "libvndk",
1466 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001467 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001468 vndk: {
1469 enabled: true,
1470 },
1471 nocrt: true,
1472 }
1473
1474 cc_library {
1475 name: "libvndk_ext_product",
1476 product_specific: true,
1477 vndk: {
1478 enabled: true,
1479 },
1480 nocrt: true,
1481 }
1482 `)
1483
1484 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1485 cc_library {
1486 name: "libvndk",
1487 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001488 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001489 vndk: {
1490 enabled: true,
1491 },
1492 nocrt: true,
1493 }
1494
1495 cc_library {
1496 name: "libvndk_ext_product",
1497 product_specific: true,
1498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001499 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001500 vndk: {
1501 enabled: true,
1502 extends: "libvndk",
1503 },
1504 nocrt: true,
1505 }
1506 `)
Logan Chienf3511742017-10-31 18:04:35 +08001507}
1508
1509func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1510 // This test ensures an error is emitted for inconsistent support_system_process.
1511 testCcError(t, "module \".*\" with mismatched support_system_process", `
1512 cc_library {
1513 name: "libvndk",
1514 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001515 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001516 vndk: {
1517 enabled: true,
1518 },
1519 nocrt: true,
1520 }
1521
1522 cc_library {
1523 name: "libvndk_sp_ext",
1524 vendor: true,
1525 vndk: {
1526 enabled: true,
1527 extends: "libvndk",
1528 support_system_process: true,
1529 },
1530 nocrt: true,
1531 }
1532 `)
1533
1534 testCcError(t, "module \".*\" with mismatched support_system_process", `
1535 cc_library {
1536 name: "libvndk_sp",
1537 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001538 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001539 vndk: {
1540 enabled: true,
1541 support_system_process: true,
1542 },
1543 nocrt: true,
1544 }
1545
1546 cc_library {
1547 name: "libvndk_ext",
1548 vendor: true,
1549 vndk: {
1550 enabled: true,
1551 extends: "libvndk_sp",
1552 },
1553 nocrt: true,
1554 }
1555 `)
1556}
1557
1558func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001559 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001560 // with `private: true`.
1561 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001562 cc_library {
1563 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001564 vendor_available: true,
1565 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001566 vndk: {
1567 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001568 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001569 },
1570 nocrt: true,
1571 }
1572
1573 cc_library {
1574 name: "libvndk_ext",
1575 vendor: true,
1576 vndk: {
1577 enabled: true,
1578 extends: "libvndk",
1579 },
1580 nocrt: true,
1581 }
1582 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001583
Justin Yunfd9e8042020-12-23 18:23:14 +09001584 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001585 cc_library {
1586 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001587 vendor_available: true,
1588 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001589 vndk: {
1590 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001591 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001592 },
1593 nocrt: true,
1594 }
1595
1596 cc_library {
1597 name: "libvndk_ext_product",
1598 product_specific: true,
1599 vndk: {
1600 enabled: true,
1601 extends: "libvndk",
1602 },
1603 nocrt: true,
1604 }
1605 `)
Logan Chienf3511742017-10-31 18:04:35 +08001606}
1607
Logan Chiend3c59a22018-03-29 14:08:15 +08001608func TestVendorModuleUseVndkExt(t *testing.T) {
1609 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001610 testCc(t, `
1611 cc_library {
1612 name: "libvndk",
1613 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001614 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001615 vndk: {
1616 enabled: true,
1617 },
1618 nocrt: true,
1619 }
1620
1621 cc_library {
1622 name: "libvndk_ext",
1623 vendor: true,
1624 vndk: {
1625 enabled: true,
1626 extends: "libvndk",
1627 },
1628 nocrt: true,
1629 }
1630
1631 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001632 name: "libvndk_sp",
1633 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001634 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001635 vndk: {
1636 enabled: true,
1637 support_system_process: true,
1638 },
1639 nocrt: true,
1640 }
1641
1642 cc_library {
1643 name: "libvndk_sp_ext",
1644 vendor: true,
1645 vndk: {
1646 enabled: true,
1647 extends: "libvndk_sp",
1648 support_system_process: true,
1649 },
1650 nocrt: true,
1651 }
1652
1653 cc_library {
1654 name: "libvendor",
1655 vendor: true,
1656 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1657 nocrt: true,
1658 }
1659 `)
1660}
1661
Logan Chiend3c59a22018-03-29 14:08:15 +08001662func TestVndkExtUseVendorLib(t *testing.T) {
1663 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001664 testCc(t, `
1665 cc_library {
1666 name: "libvndk",
1667 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001668 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001669 vndk: {
1670 enabled: true,
1671 },
1672 nocrt: true,
1673 }
1674
1675 cc_library {
1676 name: "libvndk_ext",
1677 vendor: true,
1678 vndk: {
1679 enabled: true,
1680 extends: "libvndk",
1681 },
1682 shared_libs: ["libvendor"],
1683 nocrt: true,
1684 }
1685
1686 cc_library {
1687 name: "libvendor",
1688 vendor: true,
1689 nocrt: true,
1690 }
1691 `)
Logan Chienf3511742017-10-31 18:04:35 +08001692
Logan Chiend3c59a22018-03-29 14:08:15 +08001693 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1694 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001695 cc_library {
1696 name: "libvndk_sp",
1697 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001698 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001699 vndk: {
1700 enabled: true,
1701 support_system_process: true,
1702 },
1703 nocrt: true,
1704 }
1705
1706 cc_library {
1707 name: "libvndk_sp_ext",
1708 vendor: true,
1709 vndk: {
1710 enabled: true,
1711 extends: "libvndk_sp",
1712 support_system_process: true,
1713 },
1714 shared_libs: ["libvendor"], // Cause an error
1715 nocrt: true,
1716 }
1717
1718 cc_library {
1719 name: "libvendor",
1720 vendor: true,
1721 nocrt: true,
1722 }
1723 `)
1724}
1725
Justin Yun0ecf0b22020-02-28 15:07:59 +09001726func TestProductVndkExtDependency(t *testing.T) {
1727 bp := `
1728 cc_library {
1729 name: "libvndk",
1730 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001731 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001732 vndk: {
1733 enabled: true,
1734 },
1735 nocrt: true,
1736 }
1737
1738 cc_library {
1739 name: "libvndk_ext_product",
1740 product_specific: true,
1741 vndk: {
1742 enabled: true,
1743 extends: "libvndk",
1744 },
1745 shared_libs: ["libproduct_for_vndklibs"],
1746 nocrt: true,
1747 }
1748
1749 cc_library {
1750 name: "libvndk_sp",
1751 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001752 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001753 vndk: {
1754 enabled: true,
1755 support_system_process: true,
1756 },
1757 nocrt: true,
1758 }
1759
1760 cc_library {
1761 name: "libvndk_sp_ext_product",
1762 product_specific: true,
1763 vndk: {
1764 enabled: true,
1765 extends: "libvndk_sp",
1766 support_system_process: true,
1767 },
1768 shared_libs: ["libproduct_for_vndklibs"],
1769 nocrt: true,
1770 }
1771
1772 cc_library {
1773 name: "libproduct",
1774 product_specific: true,
1775 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1776 nocrt: true,
1777 }
1778
1779 cc_library {
1780 name: "libproduct_for_vndklibs",
1781 product_specific: true,
1782 nocrt: true,
1783 }
1784 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001785 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001786 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1787 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001788 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001789
1790 testCcWithConfig(t, config)
1791}
1792
Logan Chiend3c59a22018-03-29 14:08:15 +08001793func TestVndkSpExtUseVndkError(t *testing.T) {
1794 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1795 // library.
1796 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1797 cc_library {
1798 name: "libvndk",
1799 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001800 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001801 vndk: {
1802 enabled: true,
1803 },
1804 nocrt: true,
1805 }
1806
1807 cc_library {
1808 name: "libvndk_sp",
1809 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001810 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001811 vndk: {
1812 enabled: true,
1813 support_system_process: true,
1814 },
1815 nocrt: true,
1816 }
1817
1818 cc_library {
1819 name: "libvndk_sp_ext",
1820 vendor: true,
1821 vndk: {
1822 enabled: true,
1823 extends: "libvndk_sp",
1824 support_system_process: true,
1825 },
1826 shared_libs: ["libvndk"], // Cause an error
1827 nocrt: true,
1828 }
1829 `)
1830
1831 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1832 // library.
1833 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1834 cc_library {
1835 name: "libvndk",
1836 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001837 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001838 vndk: {
1839 enabled: true,
1840 },
1841 nocrt: true,
1842 }
1843
1844 cc_library {
1845 name: "libvndk_ext",
1846 vendor: true,
1847 vndk: {
1848 enabled: true,
1849 extends: "libvndk",
1850 },
1851 nocrt: true,
1852 }
1853
1854 cc_library {
1855 name: "libvndk_sp",
1856 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001857 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001858 vndk: {
1859 enabled: true,
1860 support_system_process: true,
1861 },
1862 nocrt: true,
1863 }
1864
1865 cc_library {
1866 name: "libvndk_sp_ext",
1867 vendor: true,
1868 vndk: {
1869 enabled: true,
1870 extends: "libvndk_sp",
1871 support_system_process: true,
1872 },
1873 shared_libs: ["libvndk_ext"], // Cause an error
1874 nocrt: true,
1875 }
1876 `)
1877}
1878
1879func TestVndkUseVndkExtError(t *testing.T) {
1880 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1881 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001882 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1883 cc_library {
1884 name: "libvndk",
1885 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001886 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001887 vndk: {
1888 enabled: true,
1889 },
1890 nocrt: true,
1891 }
1892
1893 cc_library {
1894 name: "libvndk_ext",
1895 vendor: true,
1896 vndk: {
1897 enabled: true,
1898 extends: "libvndk",
1899 },
1900 nocrt: true,
1901 }
1902
1903 cc_library {
1904 name: "libvndk2",
1905 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001906 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001907 vndk: {
1908 enabled: true,
1909 },
1910 shared_libs: ["libvndk_ext"],
1911 nocrt: true,
1912 }
1913 `)
1914
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001915 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001916 cc_library {
1917 name: "libvndk",
1918 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001919 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001920 vndk: {
1921 enabled: true,
1922 },
1923 nocrt: true,
1924 }
1925
1926 cc_library {
1927 name: "libvndk_ext",
1928 vendor: true,
1929 vndk: {
1930 enabled: true,
1931 extends: "libvndk",
1932 },
1933 nocrt: true,
1934 }
1935
1936 cc_library {
1937 name: "libvndk2",
1938 vendor_available: true,
1939 vndk: {
1940 enabled: true,
1941 },
1942 target: {
1943 vendor: {
1944 shared_libs: ["libvndk_ext"],
1945 },
1946 },
1947 nocrt: true,
1948 }
1949 `)
1950
1951 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1952 cc_library {
1953 name: "libvndk_sp",
1954 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001955 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001956 vndk: {
1957 enabled: true,
1958 support_system_process: true,
1959 },
1960 nocrt: true,
1961 }
1962
1963 cc_library {
1964 name: "libvndk_sp_ext",
1965 vendor: true,
1966 vndk: {
1967 enabled: true,
1968 extends: "libvndk_sp",
1969 support_system_process: true,
1970 },
1971 nocrt: true,
1972 }
1973
1974 cc_library {
1975 name: "libvndk_sp_2",
1976 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001977 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001978 vndk: {
1979 enabled: true,
1980 support_system_process: true,
1981 },
1982 shared_libs: ["libvndk_sp_ext"],
1983 nocrt: true,
1984 }
1985 `)
1986
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001987 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001988 cc_library {
1989 name: "libvndk_sp",
1990 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001991 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001992 vndk: {
1993 enabled: true,
1994 },
1995 nocrt: true,
1996 }
1997
1998 cc_library {
1999 name: "libvndk_sp_ext",
2000 vendor: true,
2001 vndk: {
2002 enabled: true,
2003 extends: "libvndk_sp",
2004 },
2005 nocrt: true,
2006 }
2007
2008 cc_library {
2009 name: "libvndk_sp2",
2010 vendor_available: true,
2011 vndk: {
2012 enabled: true,
2013 },
2014 target: {
2015 vendor: {
2016 shared_libs: ["libvndk_sp_ext"],
2017 },
2018 },
2019 nocrt: true,
2020 }
2021 `)
2022}
2023
Justin Yun5f7f7e82019-11-18 19:52:14 +09002024func TestEnforceProductVndkVersion(t *testing.T) {
2025 bp := `
2026 cc_library {
2027 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002028 llndk: {
2029 symbol_file: "libllndk.map.txt",
2030 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002031 }
2032 cc_library {
2033 name: "libvndk",
2034 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002035 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002036 vndk: {
2037 enabled: true,
2038 },
2039 nocrt: true,
2040 }
2041 cc_library {
2042 name: "libvndk_sp",
2043 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002044 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002045 vndk: {
2046 enabled: true,
2047 support_system_process: true,
2048 },
2049 nocrt: true,
2050 }
2051 cc_library {
2052 name: "libva",
2053 vendor_available: true,
2054 nocrt: true,
2055 }
2056 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002057 name: "libpa",
2058 product_available: true,
2059 nocrt: true,
2060 }
2061 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002062 name: "libboth_available",
2063 vendor_available: true,
2064 product_available: true,
2065 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002066 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002067 target: {
2068 vendor: {
2069 suffix: "-vendor",
2070 },
2071 product: {
2072 suffix: "-product",
2073 },
2074 }
2075 }
2076 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002077 name: "libproduct_va",
2078 product_specific: true,
2079 vendor_available: true,
2080 nocrt: true,
2081 }
2082 cc_library {
2083 name: "libprod",
2084 product_specific: true,
2085 shared_libs: [
2086 "libllndk",
2087 "libvndk",
2088 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002089 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002090 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002091 "libproduct_va",
2092 ],
2093 nocrt: true,
2094 }
2095 cc_library {
2096 name: "libvendor",
2097 vendor: true,
2098 shared_libs: [
2099 "libllndk",
2100 "libvndk",
2101 "libvndk_sp",
2102 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002103 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002104 "libproduct_va",
2105 ],
2106 nocrt: true,
2107 }
2108 `
2109
Paul Duffin8567f222021-03-23 00:02:06 +00002110 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002111
Jooyung Han261e1582020-10-20 18:54:21 +09002112 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2113 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002114
2115 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2116 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2117
2118 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2119 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002120
2121 ensureStringContains := func(t *testing.T, str string, substr string) {
2122 t.Helper()
2123 if !strings.Contains(str, substr) {
2124 t.Errorf("%q is not found in %v", substr, str)
2125 }
2126 }
2127 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2128 t.Helper()
2129 if strings.Contains(str, substr) {
2130 t.Errorf("%q is found in %v", substr, str)
2131 }
2132 }
2133
2134 // _static variant is used since _shared reuses *.o from the static variant
2135 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2136 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2137
2138 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2139 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2140 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2141 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2142
2143 product_cflags := product_static.Rule("cc").Args["cFlags"]
2144 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2145 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2146 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002147}
2148
2149func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002150 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002151 cc_library {
2152 name: "libprod",
2153 product_specific: true,
2154 shared_libs: [
2155 "libvendor",
2156 ],
2157 nocrt: true,
2158 }
2159 cc_library {
2160 name: "libvendor",
2161 vendor: true,
2162 nocrt: true,
2163 }
2164 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002165 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002166 cc_library {
2167 name: "libprod",
2168 product_specific: true,
2169 shared_libs: [
2170 "libsystem",
2171 ],
2172 nocrt: true,
2173 }
2174 cc_library {
2175 name: "libsystem",
2176 nocrt: true,
2177 }
2178 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002179 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002180 cc_library {
2181 name: "libprod",
2182 product_specific: true,
2183 shared_libs: [
2184 "libva",
2185 ],
2186 nocrt: true,
2187 }
2188 cc_library {
2189 name: "libva",
2190 vendor_available: true,
2191 nocrt: true,
2192 }
2193 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002194 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002195 cc_library {
2196 name: "libprod",
2197 product_specific: true,
2198 shared_libs: [
2199 "libvndk_private",
2200 ],
2201 nocrt: true,
2202 }
2203 cc_library {
2204 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002205 vendor_available: true,
2206 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002207 vndk: {
2208 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002209 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002210 },
2211 nocrt: true,
2212 }
2213 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002214 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002215 cc_library {
2216 name: "libprod",
2217 product_specific: true,
2218 shared_libs: [
2219 "libsystem_ext",
2220 ],
2221 nocrt: true,
2222 }
2223 cc_library {
2224 name: "libsystem_ext",
2225 system_ext_specific: true,
2226 nocrt: true,
2227 }
2228 `)
2229 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2230 cc_library {
2231 name: "libsystem",
2232 shared_libs: [
2233 "libproduct_va",
2234 ],
2235 nocrt: true,
2236 }
2237 cc_library {
2238 name: "libproduct_va",
2239 product_specific: true,
2240 vendor_available: true,
2241 nocrt: true,
2242 }
2243 `)
2244}
2245
Jooyung Han38002912019-05-16 04:01:54 +09002246func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002247 bp := `
2248 cc_library {
2249 name: "libvndk",
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 },
2255 }
2256 cc_library {
2257 name: "libvndksp",
2258 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002259 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002260 vndk: {
2261 enabled: true,
2262 support_system_process: true,
2263 },
2264 }
2265 cc_library {
2266 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002267 vendor_available: true,
2268 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002269 vndk: {
2270 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002271 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002272 },
2273 }
2274 cc_library {
2275 name: "libvendor",
2276 vendor: true,
2277 }
2278 cc_library {
2279 name: "libvndkext",
2280 vendor: true,
2281 vndk: {
2282 enabled: true,
2283 extends: "libvndk",
2284 },
2285 }
2286 vndk_prebuilt_shared {
2287 name: "prevndk",
2288 version: "27",
2289 target_arch: "arm",
2290 binder32bit: true,
2291 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002292 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002293 vndk: {
2294 enabled: true,
2295 },
2296 arch: {
2297 arm: {
2298 srcs: ["liba.so"],
2299 },
2300 },
2301 }
2302 cc_library {
2303 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002304 llndk: {
2305 symbol_file: "libllndk.map.txt",
2306 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002307 }
2308 cc_library {
2309 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002310 llndk: {
2311 symbol_file: "libllndkprivate.map.txt",
2312 private: true,
2313 }
Colin Cross78212242021-01-06 14:51:30 -08002314 }
2315
2316 llndk_libraries_txt {
2317 name: "llndk.libraries.txt",
2318 }
2319 vndkcore_libraries_txt {
2320 name: "vndkcore.libraries.txt",
2321 }
2322 vndksp_libraries_txt {
2323 name: "vndksp.libraries.txt",
2324 }
2325 vndkprivate_libraries_txt {
2326 name: "vndkprivate.libraries.txt",
2327 }
2328 vndkcorevariant_libraries_txt {
2329 name: "vndkcorevariant.libraries.txt",
2330 insert_vndk_version: false,
2331 }
2332 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002333
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002334 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002335 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002336 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002337 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002338 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002339
Colin Cross78212242021-01-06 14:51:30 -08002340 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2341 []string{"libvndk.so", "libvndkprivate.so"})
2342 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2343 []string{"libc++.so", "libvndksp.so"})
2344 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2345 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2346 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2347 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002348
Colin Crossfb0c16e2019-11-20 17:12:35 -08002349 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002350
Jooyung Han38002912019-05-16 04:01:54 +09002351 tests := []struct {
2352 variant string
2353 name string
2354 expected string
2355 }{
2356 {vendorVariant, "libvndk", "native:vndk"},
2357 {vendorVariant, "libvndksp", "native:vndk"},
2358 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2359 {vendorVariant, "libvendor", "native:vendor"},
2360 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002361 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002362 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002363 {coreVariant, "libvndk", "native:platform"},
2364 {coreVariant, "libvndkprivate", "native:platform"},
2365 {coreVariant, "libllndk", "native:platform"},
2366 }
2367 for _, test := range tests {
2368 t.Run(test.name, func(t *testing.T) {
2369 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2370 assertString(t, module.makeLinkType, test.expected)
2371 })
2372 }
2373}
2374
Jeff Gaston294356f2017-09-27 17:05:30 -07002375var staticLinkDepOrderTestCases = []struct {
2376 // This is a string representation of a map[moduleName][]moduleDependency .
2377 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002378 inStatic string
2379
2380 // This is a string representation of a map[moduleName][]moduleDependency .
2381 // It models the dependencies declared in an Android.bp file.
2382 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002383
2384 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2385 // The keys of allOrdered specify which modules we would like to check.
2386 // The values of allOrdered specify the expected result (of the transitive closure of all
2387 // dependencies) for each module to test
2388 allOrdered string
2389
2390 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2391 // The keys of outOrdered specify which modules we would like to check.
2392 // The values of outOrdered specify the expected result (of the ordered linker command line)
2393 // for each module to test.
2394 outOrdered string
2395}{
2396 // Simple tests
2397 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002398 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002399 outOrdered: "",
2400 },
2401 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002402 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002403 outOrdered: "a:",
2404 },
2405 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002406 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002407 outOrdered: "a:b; b:",
2408 },
2409 // Tests of reordering
2410 {
2411 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002412 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002413 outOrdered: "a:b,c,d; b:d; c:d; d:",
2414 },
2415 {
2416 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002417 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002418 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2419 },
2420 {
2421 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002422 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002423 outOrdered: "a:d,b,e,c; d:b; e:c",
2424 },
2425 {
2426 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002427 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002428 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2429 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2430 },
2431 {
2432 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002433 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 -07002434 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2435 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2436 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002437 // shared dependencies
2438 {
2439 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2440 // So, we don't actually have to check that a shared dependency of c will change the order
2441 // of a library that depends statically on b and on c. We only need to check that if c has
2442 // a shared dependency on b, that that shows up in allOrdered.
2443 inShared: "c:b",
2444 allOrdered: "c:b",
2445 outOrdered: "c:",
2446 },
2447 {
2448 // This test doesn't actually include any shared dependencies but it's a reminder of what
2449 // the second phase of the above test would look like
2450 inStatic: "a:b,c; c:b",
2451 allOrdered: "a:c,b; c:b",
2452 outOrdered: "a:c,b; c:b",
2453 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002454 // tiebreakers for when two modules specifying different orderings and there is no dependency
2455 // to dictate an order
2456 {
2457 // 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 -08002458 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002459 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2460 },
2461 {
2462 // 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 -08002463 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 -07002464 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2465 },
2466 // Tests involving duplicate dependencies
2467 {
2468 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002469 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002470 outOrdered: "a:c,b",
2471 },
2472 {
2473 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002474 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002475 outOrdered: "a:d,c,b",
2476 },
2477 // Tests to confirm the nonexistence of infinite loops.
2478 // These cases should never happen, so as long as the test terminates and the
2479 // result is deterministic then that should be fine.
2480 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002481 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002482 outOrdered: "a:a",
2483 },
2484 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002485 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002486 allOrdered: "a:b,c; b:c,a; c:a,b",
2487 outOrdered: "a:b; b:c; c:a",
2488 },
2489 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002490 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002491 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2492 outOrdered: "a:c,b; b:a,c; c:b,a",
2493 },
2494}
2495
2496// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2497func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2498 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2499 strippedText := strings.Replace(text, " ", "", -1)
2500 if len(strippedText) < 1 {
2501 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2502 }
2503 allDeps = make(map[android.Path][]android.Path, 0)
2504
2505 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2506 moduleTexts := strings.Split(strippedText, ";")
2507
2508 outputForModuleName := func(moduleName string) android.Path {
2509 return android.PathForTesting(moduleName)
2510 }
2511
2512 for _, moduleText := range moduleTexts {
2513 // convert from "a:b,c" to ["a", "b,c"]
2514 components := strings.Split(moduleText, ":")
2515 if len(components) != 2 {
2516 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2517 }
2518 moduleName := components[0]
2519 moduleOutput := outputForModuleName(moduleName)
2520 modulesInOrder = append(modulesInOrder, moduleOutput)
2521
2522 depString := components[1]
2523 // convert from "b,c" to ["b", "c"]
2524 depNames := strings.Split(depString, ",")
2525 if len(depString) < 1 {
2526 depNames = []string{}
2527 }
2528 var deps []android.Path
2529 for _, depName := range depNames {
2530 deps = append(deps, outputForModuleName(depName))
2531 }
2532 allDeps[moduleOutput] = deps
2533 }
2534 return modulesInOrder, allDeps
2535}
2536
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002537func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002538 ctx := testCc(t, `
2539 cc_library {
2540 name: "a",
2541 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002542 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002543 }
2544 cc_library {
2545 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002546 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002547 }
2548 cc_library {
2549 name: "c",
2550 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002551 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002552 }
2553 cc_library {
2554 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002555 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 }
2557
2558 `)
2559
Colin Cross7113d202019-11-20 16:39:12 -08002560 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002561 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002562 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2563 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002564 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002565
2566 if !reflect.DeepEqual(actual, expected) {
2567 t.Errorf("staticDeps orderings were not propagated correctly"+
2568 "\nactual: %v"+
2569 "\nexpected: %v",
2570 actual,
2571 expected,
2572 )
2573 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002574}
Jeff Gaston294356f2017-09-27 17:05:30 -07002575
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002576func TestStaticLibDepReorderingWithShared(t *testing.T) {
2577 ctx := testCc(t, `
2578 cc_library {
2579 name: "a",
2580 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002581 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002582 }
2583 cc_library {
2584 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002585 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002586 }
2587 cc_library {
2588 name: "c",
2589 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002590 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002591 }
2592
2593 `)
2594
Colin Cross7113d202019-11-20 16:39:12 -08002595 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002596 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002597 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2598 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002599 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002600
2601 if !reflect.DeepEqual(actual, expected) {
2602 t.Errorf("staticDeps orderings did not account for shared libs"+
2603 "\nactual: %v"+
2604 "\nexpected: %v",
2605 actual,
2606 expected,
2607 )
2608 }
2609}
2610
Jooyung Hanb04a4992020-03-13 18:57:35 +09002611func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002612 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002613 if !reflect.DeepEqual(actual, expected) {
2614 t.Errorf(message+
2615 "\nactual: %v"+
2616 "\nexpected: %v",
2617 actual,
2618 expected,
2619 )
2620 }
2621}
2622
Jooyung Han61b66e92020-03-21 14:21:46 +00002623func TestLlndkLibrary(t *testing.T) {
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002624 result := prepareForCcTest.RunTestWithBp(t, `
2625 cc_library {
2626 name: "libllndk",
2627 stubs: { versions: ["1", "2"] },
2628 llndk: {
2629 symbol_file: "libllndk.map.txt",
2630 },
2631 export_include_dirs: ["include"],
2632 }
2633
2634 cc_prebuilt_library_shared {
2635 name: "libllndkprebuilt",
2636 stubs: { versions: ["1", "2"] },
2637 llndk: {
2638 symbol_file: "libllndkprebuilt.map.txt",
2639 },
2640 }
2641
2642 cc_library {
2643 name: "libllndk_with_external_headers",
2644 stubs: { versions: ["1", "2"] },
2645 llndk: {
2646 symbol_file: "libllndk.map.txt",
2647 export_llndk_headers: ["libexternal_llndk_headers"],
2648 },
2649 header_libs: ["libexternal_headers"],
2650 export_header_lib_headers: ["libexternal_headers"],
2651 }
2652 cc_library_headers {
2653 name: "libexternal_headers",
2654 export_include_dirs: ["include"],
2655 vendor_available: true,
2656 }
2657 cc_library_headers {
2658 name: "libexternal_llndk_headers",
2659 export_include_dirs: ["include_llndk"],
2660 llndk: {
2661 symbol_file: "libllndk.map.txt",
2662 },
2663 vendor_available: true,
2664 }
2665
2666 cc_library {
2667 name: "libllndk_with_override_headers",
2668 stubs: { versions: ["1", "2"] },
2669 llndk: {
2670 symbol_file: "libllndk.map.txt",
2671 override_export_include_dirs: ["include_llndk"],
2672 },
2673 export_include_dirs: ["include"],
2674 }
2675 `)
2676 actual := result.ModuleVariantsForTests("libllndk")
2677 for i := 0; i < len(actual); i++ {
2678 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2679 actual = append(actual[:i], actual[i+1:]...)
2680 i--
2681 }
2682 }
2683 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002684 "android_vendor.29_arm64_armv8-a_shared_current",
2685 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002686 "android_vendor.29_arm_armv7-a-neon_shared_current",
2687 "android_vendor.29_arm_armv7-a-neon_shared",
2688 }
2689 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2690
2691 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2692 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2693
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002694 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2695 t.Helper()
2696 m := result.ModuleForTests(module, variant).Module()
2697 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2698 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2699 expectedDirs, f.IncludeDirs)
2700 }
2701
2702 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2703 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2704 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2705 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2706 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2707 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2708}
2709
Jiyong Parka46a4d52017-12-14 19:54:34 +09002710func TestLlndkHeaders(t *testing.T) {
2711 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002712 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002713 name: "libllndk_headers",
2714 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002715 llndk: {
2716 llndk_headers: true,
2717 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002718 }
2719 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002720 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002721 llndk: {
2722 symbol_file: "libllndk.map.txt",
2723 export_llndk_headers: ["libllndk_headers"],
2724 }
Colin Cross0477b422020-10-13 18:43:54 -07002725 }
2726
2727 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002728 name: "libvendor",
2729 shared_libs: ["libllndk"],
2730 vendor: true,
2731 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002732 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002733 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002734 }
2735 `)
2736
2737 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002738 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002739 cflags := cc.Args["cFlags"]
2740 if !strings.Contains(cflags, "-Imy_include") {
2741 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2742 }
2743}
2744
Logan Chien43d34c32017-12-20 01:17:32 +08002745func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2746 actual := module.Properties.AndroidMkRuntimeLibs
2747 if !reflect.DeepEqual(actual, expected) {
2748 t.Errorf("incorrect runtime_libs for shared libs"+
2749 "\nactual: %v"+
2750 "\nexpected: %v",
2751 actual,
2752 expected,
2753 )
2754 }
2755}
2756
2757const runtimeLibAndroidBp = `
2758 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002759 name: "liball_available",
2760 vendor_available: true,
2761 product_available: true,
2762 no_libcrt : true,
2763 nocrt : true,
2764 system_shared_libs : [],
2765 }
2766 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002767 name: "libvendor_available1",
2768 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002769 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002770 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002771 nocrt : true,
2772 system_shared_libs : [],
2773 }
2774 cc_library {
2775 name: "libvendor_available2",
2776 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002777 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002778 target: {
2779 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002780 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002781 }
2782 },
Yi Konge7fe9912019-06-02 00:53:50 -07002783 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002784 nocrt : true,
2785 system_shared_libs : [],
2786 }
2787 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002788 name: "libproduct_vendor",
2789 product_specific: true,
2790 vendor_available: true,
2791 no_libcrt : true,
2792 nocrt : true,
2793 system_shared_libs : [],
2794 }
2795 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002796 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002797 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002798 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002799 nocrt : true,
2800 system_shared_libs : [],
2801 }
2802 cc_library {
2803 name: "libvendor1",
2804 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002805 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002806 nocrt : true,
2807 system_shared_libs : [],
2808 }
2809 cc_library {
2810 name: "libvendor2",
2811 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002812 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002813 no_libcrt : true,
2814 nocrt : true,
2815 system_shared_libs : [],
2816 }
2817 cc_library {
2818 name: "libproduct_available1",
2819 product_available: true,
2820 runtime_libs: ["liball_available"],
2821 no_libcrt : true,
2822 nocrt : true,
2823 system_shared_libs : [],
2824 }
2825 cc_library {
2826 name: "libproduct1",
2827 product_specific: true,
2828 no_libcrt : true,
2829 nocrt : true,
2830 system_shared_libs : [],
2831 }
2832 cc_library {
2833 name: "libproduct2",
2834 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002835 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002836 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002837 nocrt : true,
2838 system_shared_libs : [],
2839 }
2840`
2841
2842func TestRuntimeLibs(t *testing.T) {
2843 ctx := testCc(t, runtimeLibAndroidBp)
2844
2845 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002846 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002847
Justin Yun8a2600c2020-12-07 12:44:03 +09002848 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2849 checkRuntimeLibs(t, []string{"liball_available"}, module)
2850
2851 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2852 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002853
2854 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002855 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002856
2857 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2858 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002859 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002860
Justin Yun8a2600c2020-12-07 12:44:03 +09002861 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2862 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002863
2864 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002865 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002866
2867 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2868 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002869 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002870
2871 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2872 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2873
2874 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002875 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002876}
2877
2878func TestExcludeRuntimeLibs(t *testing.T) {
2879 ctx := testCc(t, runtimeLibAndroidBp)
2880
Colin Cross7113d202019-11-20 16:39:12 -08002881 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002882 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2883 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002884
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002885 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002886 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002887 checkRuntimeLibs(t, nil, module)
2888}
2889
2890func TestRuntimeLibsNoVndk(t *testing.T) {
2891 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2892
2893 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2894
Colin Cross7113d202019-11-20 16:39:12 -08002895 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002896
Justin Yun8a2600c2020-12-07 12:44:03 +09002897 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2898 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002899
2900 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002901 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002902
2903 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002904 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002905}
2906
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002907func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002908 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002909 actual := module.Properties.AndroidMkStaticLibs
2910 if !reflect.DeepEqual(actual, expected) {
2911 t.Errorf("incorrect static_libs"+
2912 "\nactual: %v"+
2913 "\nexpected: %v",
2914 actual,
2915 expected,
2916 )
2917 }
2918}
2919
2920const staticLibAndroidBp = `
2921 cc_library {
2922 name: "lib1",
2923 }
2924 cc_library {
2925 name: "lib2",
2926 static_libs: ["lib1"],
2927 }
2928`
2929
2930func TestStaticLibDepExport(t *testing.T) {
2931 ctx := testCc(t, staticLibAndroidBp)
2932
2933 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002934 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002935 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Ryan Prichardc2018e22021-04-02 20:23:22 -07002936 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002937
2938 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08002939 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002940 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
2941 // libc++_static is linked additionally.
Ryan Prichardc2018e22021-04-02 20:23:22 -07002942 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins-aarch64-android"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002943}
2944
Jiyong Parkd08b6972017-09-26 10:50:54 +09002945var compilerFlagsTestCases = []struct {
2946 in string
2947 out bool
2948}{
2949 {
2950 in: "a",
2951 out: false,
2952 },
2953 {
2954 in: "-a",
2955 out: true,
2956 },
2957 {
2958 in: "-Ipath/to/something",
2959 out: false,
2960 },
2961 {
2962 in: "-isystempath/to/something",
2963 out: false,
2964 },
2965 {
2966 in: "--coverage",
2967 out: false,
2968 },
2969 {
2970 in: "-include a/b",
2971 out: true,
2972 },
2973 {
2974 in: "-include a/b c/d",
2975 out: false,
2976 },
2977 {
2978 in: "-DMACRO",
2979 out: true,
2980 },
2981 {
2982 in: "-DMAC RO",
2983 out: false,
2984 },
2985 {
2986 in: "-a -b",
2987 out: false,
2988 },
2989 {
2990 in: "-DMACRO=definition",
2991 out: true,
2992 },
2993 {
2994 in: "-DMACRO=defi nition",
2995 out: true, // TODO(jiyong): this should be false
2996 },
2997 {
2998 in: "-DMACRO(x)=x + 1",
2999 out: true,
3000 },
3001 {
3002 in: "-DMACRO=\"defi nition\"",
3003 out: true,
3004 },
3005}
3006
3007type mockContext struct {
3008 BaseModuleContext
3009 result bool
3010}
3011
3012func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3013 // CheckBadCompilerFlags calls this function when the flag should be rejected
3014 ctx.result = false
3015}
3016
3017func TestCompilerFlags(t *testing.T) {
3018 for _, testCase := range compilerFlagsTestCases {
3019 ctx := &mockContext{result: true}
3020 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3021 if ctx.result != testCase.out {
3022 t.Errorf("incorrect output:")
3023 t.Errorf(" input: %#v", testCase.in)
3024 t.Errorf(" expected: %#v", testCase.out)
3025 t.Errorf(" got: %#v", ctx.result)
3026 }
3027 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003028}
Jiyong Park374510b2018-03-19 18:23:01 +09003029
Jiyong Park37b25202018-07-11 10:49:27 +09003030func TestRecovery(t *testing.T) {
3031 ctx := testCc(t, `
3032 cc_library_shared {
3033 name: "librecovery",
3034 recovery: true,
3035 }
3036 cc_library_shared {
3037 name: "librecovery32",
3038 recovery: true,
3039 compile_multilib:"32",
3040 }
Jiyong Park5baac542018-08-28 09:55:37 +09003041 cc_library_shared {
3042 name: "libHalInRecovery",
3043 recovery_available: true,
3044 vendor: true,
3045 }
Jiyong Park37b25202018-07-11 10:49:27 +09003046 `)
3047
3048 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003049 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003050 if len(variants) != 1 || !android.InList(arm64, variants) {
3051 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3052 }
3053
3054 variants = ctx.ModuleVariantsForTests("librecovery32")
3055 if android.InList(arm64, variants) {
3056 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3057 }
Jiyong Park5baac542018-08-28 09:55:37 +09003058
3059 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3060 if !recoveryModule.Platform() {
3061 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3062 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003063}
Jiyong Park5baac542018-08-28 09:55:37 +09003064
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003065func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3066 bp := `
3067 cc_prebuilt_test_library_shared {
3068 name: "test_lib",
3069 relative_install_path: "foo/bar/baz",
3070 srcs: ["srcpath/dontusethispath/baz.so"],
3071 }
3072
3073 cc_test {
3074 name: "main_test",
3075 data_libs: ["test_lib"],
3076 gtest: false,
3077 }
3078 `
3079
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003080 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003081 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003082 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003083 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3084
3085 ctx := testCcWithConfig(t, config)
3086 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3087 testBinary := module.(*Module).linker.(*testBinary)
3088 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3089 if err != nil {
3090 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3091 }
3092 if len(outputFiles) != 1 {
3093 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3094 }
3095 if len(testBinary.dataPaths()) != 1 {
3096 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3097 }
3098
3099 outputPath := outputFiles[0].String()
3100
3101 if !strings.HasSuffix(outputPath, "/main_test") {
3102 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3103 }
Colin Crossaa255532020-07-03 13:18:24 -07003104 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003105 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3106 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3107 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3108 }
3109}
3110
Jiyong Park7ed9de32018-10-15 22:25:07 +09003111func TestVersionedStubs(t *testing.T) {
3112 ctx := testCc(t, `
3113 cc_library_shared {
3114 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003115 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003116 stubs: {
3117 symbol_file: "foo.map.txt",
3118 versions: ["1", "2", "3"],
3119 },
3120 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003121
Jiyong Park7ed9de32018-10-15 22:25:07 +09003122 cc_library_shared {
3123 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003124 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003125 shared_libs: ["libFoo#1"],
3126 }`)
3127
3128 variants := ctx.ModuleVariantsForTests("libFoo")
3129 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003130 "android_arm64_armv8-a_shared",
3131 "android_arm64_armv8-a_shared_1",
3132 "android_arm64_armv8-a_shared_2",
3133 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003134 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003135 "android_arm_armv7-a-neon_shared",
3136 "android_arm_armv7-a-neon_shared_1",
3137 "android_arm_armv7-a-neon_shared_2",
3138 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003139 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003140 }
3141 variantsMismatch := false
3142 if len(variants) != len(expectedVariants) {
3143 variantsMismatch = true
3144 } else {
3145 for _, v := range expectedVariants {
3146 if !inList(v, variants) {
3147 variantsMismatch = false
3148 }
3149 }
3150 }
3151 if variantsMismatch {
3152 t.Errorf("variants of libFoo expected:\n")
3153 for _, v := range expectedVariants {
3154 t.Errorf("%q\n", v)
3155 }
3156 t.Errorf(", but got:\n")
3157 for _, v := range variants {
3158 t.Errorf("%q\n", v)
3159 }
3160 }
3161
Colin Cross7113d202019-11-20 16:39:12 -08003162 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003163 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003164 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003165 if !strings.Contains(libFlags, libFoo1StubPath) {
3166 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3167 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003168
Colin Cross7113d202019-11-20 16:39:12 -08003169 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003170 cFlags := libBarCompileRule.Args["cFlags"]
3171 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3172 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3173 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3174 }
Jiyong Park37b25202018-07-11 10:49:27 +09003175}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003176
Jooyung Hanb04a4992020-03-13 18:57:35 +09003177func TestVersioningMacro(t *testing.T) {
3178 for _, tc := range []struct{ moduleName, expected string }{
3179 {"libc", "__LIBC_API__"},
3180 {"libfoo", "__LIBFOO_API__"},
3181 {"libfoo@1", "__LIBFOO_1_API__"},
3182 {"libfoo-v1", "__LIBFOO_V1_API__"},
3183 {"libfoo.v1", "__LIBFOO_V1_API__"},
3184 } {
3185 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3186 }
3187}
3188
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003189func TestStaticExecutable(t *testing.T) {
3190 ctx := testCc(t, `
3191 cc_binary {
3192 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003193 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003194 static_executable: true,
3195 }`)
3196
Colin Cross7113d202019-11-20 16:39:12 -08003197 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003198 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3199 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003200 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003201 for _, lib := range systemStaticLibs {
3202 if !strings.Contains(libFlags, lib) {
3203 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3204 }
3205 }
3206 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3207 for _, lib := range systemSharedLibs {
3208 if strings.Contains(libFlags, lib) {
3209 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3210 }
3211 }
3212}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003213
3214func TestStaticDepsOrderWithStubs(t *testing.T) {
3215 ctx := testCc(t, `
3216 cc_binary {
3217 name: "mybin",
3218 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003219 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003220 static_executable: true,
3221 stl: "none",
3222 }
3223
3224 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003225 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003226 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003227 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003228 stl: "none",
3229 }
3230
3231 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003232 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003233 srcs: ["foo.c"],
3234 stl: "none",
3235 stubs: {
3236 versions: ["1"],
3237 },
3238 }`)
3239
Colin Cross0de8a1e2020-09-18 14:15:30 -07003240 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3241 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003242 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003243
3244 if !reflect.DeepEqual(actual, expected) {
3245 t.Errorf("staticDeps orderings were not propagated correctly"+
3246 "\nactual: %v"+
3247 "\nexpected: %v",
3248 actual,
3249 expected,
3250 )
3251 }
3252}
Jooyung Han38002912019-05-16 04:01:54 +09003253
Jooyung Hand48f3c32019-08-23 11:18:57 +09003254func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3255 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3256 cc_library {
3257 name: "libA",
3258 srcs: ["foo.c"],
3259 shared_libs: ["libB"],
3260 stl: "none",
3261 }
3262
3263 cc_library {
3264 name: "libB",
3265 srcs: ["foo.c"],
3266 enabled: false,
3267 stl: "none",
3268 }
3269 `)
3270}
3271
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003272// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3273// correctly.
3274func TestFuzzTarget(t *testing.T) {
3275 ctx := testCc(t, `
3276 cc_fuzz {
3277 name: "fuzz_smoke_test",
3278 srcs: ["foo.c"],
3279 }`)
3280
Paul Duffin075c4172019-12-19 19:06:13 +00003281 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003282 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3283}
3284
Jiyong Park29074592019-07-07 16:27:47 +09003285func TestAidl(t *testing.T) {
3286}
3287
Jooyung Han38002912019-05-16 04:01:54 +09003288func assertString(t *testing.T, got, expected string) {
3289 t.Helper()
3290 if got != expected {
3291 t.Errorf("expected %q got %q", expected, got)
3292 }
3293}
3294
3295func assertArrayString(t *testing.T, got, expected []string) {
3296 t.Helper()
3297 if len(got) != len(expected) {
3298 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3299 return
3300 }
3301 for i := range got {
3302 if got[i] != expected[i] {
3303 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3304 i, expected[i], expected, got[i], got)
3305 return
3306 }
3307 }
3308}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003309
Jooyung Han0302a842019-10-30 18:43:49 +09003310func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3311 t.Helper()
3312 assertArrayString(t, android.SortedStringKeys(m), expected)
3313}
3314
Colin Crosse1bb5d02019-09-24 14:55:04 -07003315func TestDefaults(t *testing.T) {
3316 ctx := testCc(t, `
3317 cc_defaults {
3318 name: "defaults",
3319 srcs: ["foo.c"],
3320 static: {
3321 srcs: ["bar.c"],
3322 },
3323 shared: {
3324 srcs: ["baz.c"],
3325 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003326 bazel_module: {
3327 bp2build_available: true,
3328 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003329 }
3330
3331 cc_library_static {
3332 name: "libstatic",
3333 defaults: ["defaults"],
3334 }
3335
3336 cc_library_shared {
3337 name: "libshared",
3338 defaults: ["defaults"],
3339 }
3340
3341 cc_library {
3342 name: "libboth",
3343 defaults: ["defaults"],
3344 }
3345
3346 cc_binary {
3347 name: "binary",
3348 defaults: ["defaults"],
3349 }`)
3350
3351 pathsToBase := func(paths android.Paths) []string {
3352 var ret []string
3353 for _, p := range paths {
3354 ret = append(ret, p.Base())
3355 }
3356 return ret
3357 }
3358
Colin Cross7113d202019-11-20 16:39:12 -08003359 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003360 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3361 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3362 }
Colin Cross7113d202019-11-20 16:39:12 -08003363 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003364 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3365 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3366 }
Colin Cross7113d202019-11-20 16:39:12 -08003367 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003368 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3369 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3370 }
3371
Colin Cross7113d202019-11-20 16:39:12 -08003372 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003373 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3374 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3375 }
Colin Cross7113d202019-11-20 16:39:12 -08003376 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003377 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3378 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3379 }
3380}
Colin Crosseabaedd2020-02-06 17:01:55 -08003381
3382func TestProductVariableDefaults(t *testing.T) {
3383 bp := `
3384 cc_defaults {
3385 name: "libfoo_defaults",
3386 srcs: ["foo.c"],
3387 cppflags: ["-DFOO"],
3388 product_variables: {
3389 debuggable: {
3390 cppflags: ["-DBAR"],
3391 },
3392 },
3393 }
3394
3395 cc_library {
3396 name: "libfoo",
3397 defaults: ["libfoo_defaults"],
3398 }
3399 `
3400
Paul Duffin8567f222021-03-23 00:02:06 +00003401 result := android.GroupFixturePreparers(
3402 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003403 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003404
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003405 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3406 variables.Debuggable = BoolPtr(true)
3407 }),
3408 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003409
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003410 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003411 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003412}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003413
3414func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3415 t.Parallel()
3416 bp := `
3417 cc_library_static {
3418 name: "libfoo",
3419 srcs: ["foo.c"],
3420 whole_static_libs: ["libbar"],
3421 }
3422
3423 cc_library_static {
3424 name: "libbar",
3425 whole_static_libs: ["libmissing"],
3426 }
3427 `
3428
Paul Duffin8567f222021-03-23 00:02:06 +00003429 result := android.GroupFixturePreparers(
3430 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003431 android.PrepareForTestWithAllowMissingDependencies,
3432 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003433
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003434 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003435 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003436
Paul Duffine84b1332021-03-12 11:59:43 +00003437 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003438
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003439 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003440 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003441}
Colin Crosse9fe2942020-11-10 18:12:15 -08003442
3443func TestInstallSharedLibs(t *testing.T) {
3444 bp := `
3445 cc_binary {
3446 name: "bin",
3447 host_supported: true,
3448 shared_libs: ["libshared"],
3449 runtime_libs: ["libruntime"],
3450 srcs: [":gen"],
3451 }
3452
3453 cc_library_shared {
3454 name: "libshared",
3455 host_supported: true,
3456 shared_libs: ["libtransitive"],
3457 }
3458
3459 cc_library_shared {
3460 name: "libtransitive",
3461 host_supported: true,
3462 }
3463
3464 cc_library_shared {
3465 name: "libruntime",
3466 host_supported: true,
3467 }
3468
3469 cc_binary_host {
3470 name: "tool",
3471 srcs: ["foo.cpp"],
3472 }
3473
3474 genrule {
3475 name: "gen",
3476 tools: ["tool"],
3477 out: ["gen.cpp"],
3478 cmd: "$(location tool) $(out)",
3479 }
3480 `
3481
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003482 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003483 ctx := testCcWithConfig(t, config)
3484
3485 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3486 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3487 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3488 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3489 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3490
3491 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3492 t.Errorf("expected host bin dependency %q, got %q", w, g)
3493 }
3494
3495 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3496 t.Errorf("expected host bin dependency %q, got %q", w, g)
3497 }
3498
3499 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3500 t.Errorf("expected host bin dependency %q, got %q", w, g)
3501 }
3502
3503 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3504 t.Errorf("expected host bin dependency %q, got %q", w, g)
3505 }
3506
3507 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3508 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3509 }
3510
3511 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3512 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3513 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3514 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3515
3516 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3517 t.Errorf("expected device bin dependency %q, got %q", w, g)
3518 }
3519
3520 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3521 t.Errorf("expected device bin dependency %q, got %q", w, g)
3522 }
3523
3524 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3525 t.Errorf("expected device bin dependency %q, got %q", w, g)
3526 }
3527
3528 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3529 t.Errorf("expected device bin dependency %q, got %q", w, g)
3530 }
3531
3532 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3533 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3534 }
3535
3536}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003537
3538func TestStubsLibReexportsHeaders(t *testing.T) {
3539 ctx := testCc(t, `
3540 cc_library_shared {
3541 name: "libclient",
3542 srcs: ["foo.c"],
3543 shared_libs: ["libfoo#1"],
3544 }
3545
3546 cc_library_shared {
3547 name: "libfoo",
3548 srcs: ["foo.c"],
3549 shared_libs: ["libbar"],
3550 export_shared_lib_headers: ["libbar"],
3551 stubs: {
3552 symbol_file: "foo.map.txt",
3553 versions: ["1", "2", "3"],
3554 },
3555 }
3556
3557 cc_library_shared {
3558 name: "libbar",
3559 export_include_dirs: ["include/libbar"],
3560 srcs: ["foo.c"],
3561 }`)
3562
3563 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3564
3565 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3566 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3567 }
3568}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003569
3570func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3571 ctx := testCc(t, `
3572 cc_library {
3573 name: "libfoo",
3574 srcs: ["a/Foo.aidl"],
3575 aidl: { flags: ["-Werror"], },
3576 }
3577 `)
3578
3579 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3580 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3581 aidlCommand := manifest.Commands[0].GetCommand()
3582 expectedAidlFlag := "-Werror"
3583 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3584 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3585 }
3586}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003587
Jooyung Han07f70c02021-11-06 07:08:45 +09003588func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
3589 for _, tc := range []struct {
3590 name string
3591 sdkVersion string
3592 variant string
3593 expected string
3594 }{
3595 {
3596 name: "default is current",
3597 sdkVersion: "",
3598 variant: "android_arm64_armv8-a_static",
3599 expected: "platform_apis",
3600 },
3601 {
3602 name: "use sdk_version",
3603 sdkVersion: `sdk_version: "29"`,
3604 variant: "android_arm64_armv8-a_static",
3605 expected: "platform_apis",
3606 },
3607 {
3608 name: "use sdk_version(sdk variant)",
3609 sdkVersion: `sdk_version: "29"`,
3610 variant: "android_arm64_armv8-a_sdk_static",
3611 expected: "29",
3612 },
3613 {
3614 name: "use min_sdk_version",
3615 sdkVersion: `min_sdk_version: "29"`,
3616 variant: "android_arm64_armv8-a_static",
3617 expected: "29",
3618 },
3619 } {
3620 t.Run(tc.name, func(t *testing.T) {
3621 ctx := testCc(t, `
3622 cc_library {
3623 name: "libfoo",
3624 stl: "none",
3625 srcs: ["a/Foo.aidl"],
3626 `+tc.sdkVersion+`
3627 }
3628 `)
3629 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
3630 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3631 aidlCommand := manifest.Commands[0].GetCommand()
3632 expectedAidlFlag := "--min_sdk_version=" + tc.expected
3633 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3634 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3635 }
3636 })
3637 }
3638}
3639
Jiyong Parka008fb02021-03-16 17:15:53 +09003640func TestMinSdkVersionInClangTriple(t *testing.T) {
3641 ctx := testCc(t, `
3642 cc_library_shared {
3643 name: "libfoo",
3644 srcs: ["foo.c"],
3645 min_sdk_version: "29",
3646 }`)
3647
3648 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3649 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3650}
3651
Paul Duffin3cb603e2021-02-19 13:57:10 +00003652func TestIncludeDirsExporting(t *testing.T) {
3653
3654 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3655 // embedded newline characters alone.
3656 trimIndentingSpaces := func(s string) string {
3657 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3658 }
3659
3660 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
3661 t.Helper()
3662 expected = trimIndentingSpaces(expected)
3663 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
3664 if expected != actual {
3665 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
3666 }
3667 }
3668
3669 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
3670
3671 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
3672 t.Helper()
3673 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
3674 name := module.Name()
3675
3676 for _, checker := range checkers {
3677 checker(t, name, exported)
3678 }
3679 }
3680
3681 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
3682 return func(t *testing.T, name string, exported FlagExporterInfo) {
3683 t.Helper()
3684 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
3685 }
3686 }
3687
3688 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
3689 return func(t *testing.T, name string, exported FlagExporterInfo) {
3690 t.Helper()
3691 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
3692 }
3693 }
3694
3695 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
3696 return func(t *testing.T, name string, exported FlagExporterInfo) {
3697 t.Helper()
3698 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
3699 }
3700 }
3701
3702 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
3703 return func(t *testing.T, name string, exported FlagExporterInfo) {
3704 t.Helper()
3705 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
3706 }
3707 }
3708
3709 genRuleModules := `
3710 genrule {
3711 name: "genrule_foo",
3712 cmd: "generate-foo",
3713 out: [
3714 "generated_headers/foo/generated_header.h",
3715 ],
3716 export_include_dirs: [
3717 "generated_headers",
3718 ],
3719 }
3720
3721 genrule {
3722 name: "genrule_bar",
3723 cmd: "generate-bar",
3724 out: [
3725 "generated_headers/bar/generated_header.h",
3726 ],
3727 export_include_dirs: [
3728 "generated_headers",
3729 ],
3730 }
3731 `
3732
3733 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
3734 ctx := testCc(t, genRuleModules+`
3735 cc_library {
3736 name: "libfoo",
3737 srcs: ["foo.c"],
3738 export_include_dirs: ["foo/standard"],
3739 export_system_include_dirs: ["foo/system"],
3740 generated_headers: ["genrule_foo"],
3741 export_generated_headers: ["genrule_foo"],
3742 }
3743
3744 cc_library {
3745 name: "libbar",
3746 srcs: ["bar.c"],
3747 shared_libs: ["libfoo"],
3748 export_include_dirs: ["bar/standard"],
3749 export_system_include_dirs: ["bar/system"],
3750 generated_headers: ["genrule_bar"],
3751 export_generated_headers: ["genrule_bar"],
3752 }
3753 `)
3754 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3755 checkIncludeDirs(t, ctx, foo,
3756 expectedIncludeDirs(`
3757 foo/standard
3758 .intermediates/genrule_foo/gen/generated_headers
3759 `),
3760 expectedSystemIncludeDirs(`foo/system`),
3761 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3762 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3763 )
3764
3765 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
3766 checkIncludeDirs(t, ctx, bar,
3767 expectedIncludeDirs(`
3768 bar/standard
3769 .intermediates/genrule_bar/gen/generated_headers
3770 `),
3771 expectedSystemIncludeDirs(`bar/system`),
3772 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
3773 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
3774 )
3775 })
3776
3777 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
3778 ctx := testCc(t, genRuleModules+`
3779 cc_library {
3780 name: "libfoo",
3781 srcs: ["foo.c"],
3782 export_include_dirs: ["foo/standard"],
3783 export_system_include_dirs: ["foo/system"],
3784 generated_headers: ["genrule_foo"],
3785 export_generated_headers: ["genrule_foo"],
3786 }
3787
3788 cc_library {
3789 name: "libbar",
3790 srcs: ["bar.c"],
3791 whole_static_libs: ["libfoo"],
3792 export_include_dirs: ["bar/standard"],
3793 export_system_include_dirs: ["bar/system"],
3794 generated_headers: ["genrule_bar"],
3795 export_generated_headers: ["genrule_bar"],
3796 }
3797 `)
3798 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3799 checkIncludeDirs(t, ctx, foo,
3800 expectedIncludeDirs(`
3801 foo/standard
3802 .intermediates/genrule_foo/gen/generated_headers
3803 `),
3804 expectedSystemIncludeDirs(`foo/system`),
3805 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3806 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3807 )
3808
3809 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
3810 checkIncludeDirs(t, ctx, bar,
3811 expectedIncludeDirs(`
3812 bar/standard
3813 foo/standard
3814 .intermediates/genrule_foo/gen/generated_headers
3815 .intermediates/genrule_bar/gen/generated_headers
3816 `),
3817 expectedSystemIncludeDirs(`
3818 bar/system
3819 foo/system
3820 `),
3821 expectedGeneratedHeaders(`
3822 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
3823 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
3824 `),
3825 expectedOrderOnlyDeps(`
3826 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
3827 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
3828 `),
3829 )
3830 })
3831
Paul Duffin3cb603e2021-02-19 13:57:10 +00003832 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
3833 ctx := testCc(t, genRuleModules+`
3834 cc_library_shared {
3835 name: "libfoo",
3836 srcs: [
3837 "foo.c",
3838 "b.aidl",
3839 "a.proto",
3840 ],
3841 aidl: {
3842 export_aidl_headers: true,
3843 }
3844 }
3845 `)
3846 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3847 checkIncludeDirs(t, ctx, foo,
3848 expectedIncludeDirs(`
3849 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
3850 `),
3851 expectedSystemIncludeDirs(``),
3852 expectedGeneratedHeaders(`
3853 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
3854 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
3855 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00003856 `),
3857 expectedOrderOnlyDeps(`
3858 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
3859 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
3860 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00003861 `),
3862 )
3863 })
3864
Paul Duffin3cb603e2021-02-19 13:57:10 +00003865 t.Run("ensure only proto headers are exported", func(t *testing.T) {
3866 ctx := testCc(t, genRuleModules+`
3867 cc_library_shared {
3868 name: "libfoo",
3869 srcs: [
3870 "foo.c",
3871 "b.aidl",
3872 "a.proto",
3873 ],
3874 proto: {
3875 export_proto_headers: true,
3876 }
3877 }
3878 `)
3879 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3880 checkIncludeDirs(t, ctx, foo,
3881 expectedIncludeDirs(`
3882 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
3883 `),
3884 expectedSystemIncludeDirs(``),
3885 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00003886 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
3887 `),
3888 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00003889 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
3890 `),
3891 )
3892 })
3893
Paul Duffin33056e82021-02-19 13:49:08 +00003894 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00003895 ctx := testCc(t, genRuleModules+`
3896 cc_library_shared {
3897 name: "libfoo",
3898 srcs: [
3899 "foo.c",
3900 "a.sysprop",
3901 "b.aidl",
3902 "a.proto",
3903 ],
3904 }
3905 `)
3906 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3907 checkIncludeDirs(t, ctx, foo,
3908 expectedIncludeDirs(`
3909 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
3910 `),
3911 expectedSystemIncludeDirs(``),
3912 expectedGeneratedHeaders(`
3913 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00003914 `),
3915 expectedOrderOnlyDeps(`
3916 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
3917 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00003918 `),
3919 )
3920 })
3921}
Colin Crossae628182021-06-14 16:52:28 -07003922
3923func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer08572c62021-09-30 10:11:04 -04003924 baseExpectedFlags := []string{
3925 "${config.ArmThumbCflags}",
3926 "${config.ArmCflags}",
3927 "${config.CommonGlobalCflags}",
3928 "${config.DeviceGlobalCflags}",
3929 "${config.ExternalCflags}",
3930 "${config.ArmToolchainCflags}",
3931 "${config.ArmArmv7ANeonCflags}",
3932 "${config.ArmGenericCflags}",
3933 "-target",
3934 "armv7a-linux-androideabi20",
3935 "-B${config.ArmGccRoot}/arm-linux-androideabi/bin",
3936 }
3937
3938 expectedIncludes := []string{
3939 "external/foo/android_arm_export_include_dirs",
3940 "external/foo/lib32_export_include_dirs",
3941 "external/foo/arm_export_include_dirs",
3942 "external/foo/android_export_include_dirs",
3943 "external/foo/linux_export_include_dirs",
3944 "external/foo/export_include_dirs",
3945 "external/foo/android_arm_local_include_dirs",
3946 "external/foo/lib32_local_include_dirs",
3947 "external/foo/arm_local_include_dirs",
3948 "external/foo/android_local_include_dirs",
3949 "external/foo/linux_local_include_dirs",
3950 "external/foo/local_include_dirs",
3951 "external/foo",
3952 "external/foo/libheader1",
3953 "external/foo/libheader2",
3954 "external/foo/libwhole1",
3955 "external/foo/libwhole2",
3956 "external/foo/libstatic1",
3957 "external/foo/libstatic2",
3958 "external/foo/libshared1",
3959 "external/foo/libshared2",
3960 "external/foo/liblinux",
3961 "external/foo/libandroid",
3962 "external/foo/libarm",
3963 "external/foo/lib32",
3964 "external/foo/libandroid_arm",
3965 "defaults/cc/common/ndk_libc++_shared",
3966 "defaults/cc/common/ndk_libandroid_support",
3967 }
3968
3969 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
3970 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
3971
3972 cflags := []string{"-Wall", "-Werror"}
3973 cstd := []string{"-std=gnu99"}
3974 cppstd := []string{"-std=gnu++17", "-fno-rtti"}
3975
3976 lastIncludes := []string{
3977 "out/soong/ndk/sysroot/usr/include",
3978 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
3979 }
3980
3981 combineSlices := func(slices ...[]string) []string {
3982 var ret []string
3983 for _, s := range slices {
3984 ret = append(ret, s...)
3985 }
3986 return ret
3987 }
3988
3989 testCases := []struct {
3990 name string
3991 src string
3992 expected []string
3993 }{
3994 {
3995 name: "c",
3996 src: "foo.c",
3997 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}"}),
3998 },
3999 {
4000 name: "cc",
4001 src: "foo.cc",
4002 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}"}),
4003 },
4004 {
4005 name: "assemble",
4006 src: "foo.s",
4007 expected: combineSlices(baseExpectedFlags, []string{"-D__ASSEMBLY__"}, expectedIncludes, lastIncludes),
4008 },
4009 }
4010
4011 for _, tc := range testCases {
4012 t.Run(tc.name, func(t *testing.T) {
4013 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004014 cc_library {
4015 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004016 srcs: ["%s"],
Colin Crossae628182021-06-14 16:52:28 -07004017 local_include_dirs: ["local_include_dirs"],
4018 export_include_dirs: ["export_include_dirs"],
4019 export_system_include_dirs: ["export_system_include_dirs"],
4020 static_libs: ["libstatic1", "libstatic2"],
4021 whole_static_libs: ["libwhole1", "libwhole2"],
4022 shared_libs: ["libshared1", "libshared2"],
4023 header_libs: ["libheader1", "libheader2"],
4024 target: {
4025 android: {
4026 shared_libs: ["libandroid"],
4027 local_include_dirs: ["android_local_include_dirs"],
4028 export_include_dirs: ["android_export_include_dirs"],
4029 },
4030 android_arm: {
4031 shared_libs: ["libandroid_arm"],
4032 local_include_dirs: ["android_arm_local_include_dirs"],
4033 export_include_dirs: ["android_arm_export_include_dirs"],
4034 },
4035 linux: {
4036 shared_libs: ["liblinux"],
4037 local_include_dirs: ["linux_local_include_dirs"],
4038 export_include_dirs: ["linux_export_include_dirs"],
4039 },
4040 },
4041 multilib: {
4042 lib32: {
4043 shared_libs: ["lib32"],
4044 local_include_dirs: ["lib32_local_include_dirs"],
4045 export_include_dirs: ["lib32_export_include_dirs"],
4046 },
4047 },
4048 arch: {
4049 arm: {
4050 shared_libs: ["libarm"],
4051 local_include_dirs: ["arm_local_include_dirs"],
4052 export_include_dirs: ["arm_export_include_dirs"],
4053 },
4054 },
4055 stl: "libc++",
4056 sdk_version: "20",
4057 }
4058
4059 cc_library_headers {
4060 name: "libheader1",
4061 export_include_dirs: ["libheader1"],
4062 sdk_version: "20",
4063 stl: "none",
4064 }
4065
4066 cc_library_headers {
4067 name: "libheader2",
4068 export_include_dirs: ["libheader2"],
4069 sdk_version: "20",
4070 stl: "none",
4071 }
Liz Kammer08572c62021-09-30 10:11:04 -04004072 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004073
Liz Kammer08572c62021-09-30 10:11:04 -04004074 libs := []string{
4075 "libstatic1",
4076 "libstatic2",
4077 "libwhole1",
4078 "libwhole2",
4079 "libshared1",
4080 "libshared2",
4081 "libandroid",
4082 "libandroid_arm",
4083 "liblinux",
4084 "lib32",
4085 "libarm",
4086 }
Colin Crossae628182021-06-14 16:52:28 -07004087
Liz Kammer08572c62021-09-30 10:11:04 -04004088 for _, lib := range libs {
4089 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004090 cc_library {
4091 name: "%s",
4092 export_include_dirs: ["%s"],
4093 sdk_version: "20",
4094 stl: "none",
4095 }
4096 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004097 }
4098
4099 ctx := android.GroupFixturePreparers(
4100 PrepareForIntegrationTestWithCc,
4101 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4102 ).RunTest(t)
4103 // Use the arm variant instead of the arm64 variant so that it gets headers from
4104 // ndk_libandroid_support to test LateStaticLibs.
4105 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4106
4107 var includes []string
4108 flags := strings.Split(cflags, " ")
4109 for _, flag := range flags {
4110 if strings.HasPrefix(flag, "-I") {
4111 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4112 } else if flag == "-isystem" {
4113 // skip isystem, include next
4114 } else if len(flag) > 0 {
4115 includes = append(includes, flag)
4116 }
4117 }
4118
4119 android.AssertArrayString(t, "includes", tc.expected, includes)
4120 })
Colin Crossae628182021-06-14 16:52:28 -07004121 }
4122
Colin Crossae628182021-06-14 16:52:28 -07004123}