blob: 6a22bd0a60c9347db076793c76c7943d5a9c4ddf [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"
Cory Barker9cfcf6d2022-07-22 17:22:02 +000023 "runtime"
Jeff Gaston294356f2017-09-27 17:05:30 -070024 "strings"
Colin Cross74d1ec02015-04-28 13:30:13 -070025 "testing"
Colin Crosse1bb5d02019-09-24 14:55:04 -070026
27 "android/soong/android"
Colin Cross74d1ec02015-04-28 13:30:13 -070028)
29
Jiyong Park6a43f042017-10-12 23:05:00 +090030func TestMain(m *testing.M) {
Paul Duffinc3e6ce02021-03-22 23:21:32 +000031 os.Exit(m.Run())
Jiyong Park6a43f042017-10-12 23:05:00 +090032}
33
Paul Duffin2e6f90e2021-03-22 23:20:25 +000034var prepareForCcTest = android.GroupFixturePreparers(
Paul Duffin02a3d652021-02-24 18:51:54 +000035 PrepareForTestWithCcIncludeVndk,
Paul Duffin02a3d652021-02-24 18:51:54 +000036 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
37 variables.DeviceVndkVersion = StringPtr("current")
38 variables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090039 variables.Platform_vndk_version = StringPtr("29")
Paul Duffin02a3d652021-02-24 18:51:54 +000040 }),
41)
42
Paul Duffin8567f222021-03-23 00:02:06 +000043// testCcWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000044//
45// See testCc for an explanation as to how to stop using this deprecated method.
46//
47// deprecated
Colin Cross98be1bb2019-12-13 20:41:13 -080048func testCcWithConfig(t *testing.T, config android.Config) *android.TestContext {
Colin Crosse1bb5d02019-09-24 14:55:04 -070049 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000050 result := prepareForCcTest.RunTestWithConfig(t, config)
Paul Duffin02a3d652021-02-24 18:51:54 +000051 return result.TestContext
Jiyong Park6a43f042017-10-12 23:05:00 +090052}
53
Paul Duffin8567f222021-03-23 00:02:06 +000054// testCc runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000055//
Paul Duffin8567f222021-03-23 00:02:06 +000056// 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 +000057// easier to customize the test behavior.
58//
59// 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 +000060// convert the test to using prepareForCcTest first and then in a following change add the
Paul Duffin02a3d652021-02-24 18:51:54 +000061// appropriate fixture preparers. Keeping the conversion change separate makes it easy to verify
62// that it did not change the test behavior unexpectedly.
63//
64// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080065func testCc(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080066 t.Helper()
Paul Duffin8567f222021-03-23 00:02:06 +000067 result := prepareForCcTest.RunTestWithBp(t, bp)
Paul Duffin02a3d652021-02-24 18:51:54 +000068 return result.TestContext
Logan Chienf3511742017-10-31 18:04:35 +080069}
70
Paul Duffin8567f222021-03-23 00:02:06 +000071// testCcNoVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000072//
73// See testCc for an explanation as to how to stop using this deprecated method.
74//
75// deprecated
Logan Chienf3511742017-10-31 18:04:35 +080076func testCcNoVndk(t *testing.T, bp string) *android.TestContext {
Logan Chiend3c59a22018-03-29 14:08:15 +080077 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000078 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jiyong Parkf58c46e2021-04-01 21:35:20 +090079 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Logan Chienf3511742017-10-31 18:04:35 +080080
Colin Cross98be1bb2019-12-13 20:41:13 -080081 return testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +080082}
83
Paul Duffin8567f222021-03-23 00:02:06 +000084// testCcNoProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000085//
86// See testCc for an explanation as to how to stop using this deprecated method.
87//
88// deprecated
Justin Yun8a2600c2020-12-07 12:44:03 +090089func testCcNoProductVndk(t *testing.T, bp string) *android.TestContext {
90 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +000091 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun8a2600c2020-12-07 12:44:03 +090092 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +090093 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun8a2600c2020-12-07 12:44:03 +090094
95 return testCcWithConfig(t, config)
96}
97
Paul Duffin8567f222021-03-23 00:02:06 +000098// testCcErrorWithConfig runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +000099//
100// See testCc for an explanation as to how to stop using this deprecated method.
101//
102// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900103func testCcErrorWithConfig(t *testing.T, pattern string, config android.Config) {
Logan Chiend3c59a22018-03-29 14:08:15 +0800104 t.Helper()
Logan Chienf3511742017-10-31 18:04:35 +0800105
Paul Duffin8567f222021-03-23 00:02:06 +0000106 prepareForCcTest.
Paul Duffin02a3d652021-02-24 18:51:54 +0000107 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(pattern)).
108 RunTestWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800109}
110
Paul Duffin8567f222021-03-23 00:02:06 +0000111// testCcError runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000112//
113// See testCc for an explanation as to how to stop using this deprecated method.
114//
115// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900116func testCcError(t *testing.T, pattern string, bp string) {
Jooyung Han479ca172020-10-19 18:51:07 +0900117 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000118 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900119 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900120 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900121 testCcErrorWithConfig(t, pattern, config)
122 return
123}
124
Paul Duffin8567f222021-03-23 00:02:06 +0000125// testCcErrorProductVndk runs tests using the prepareForCcTest
Paul Duffin02a3d652021-02-24 18:51:54 +0000126//
127// See testCc for an explanation as to how to stop using this deprecated method.
128//
129// deprecated
Justin Yun5f7f7e82019-11-18 19:52:14 +0900130func testCcErrorProductVndk(t *testing.T, pattern string, bp string) {
Jooyung Han261e1582020-10-20 18:54:21 +0900131 t.Helper()
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000132 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun5f7f7e82019-11-18 19:52:14 +0900133 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
134 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900135 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun5f7f7e82019-11-18 19:52:14 +0900136 testCcErrorWithConfig(t, pattern, config)
137 return
138}
139
Logan Chienf3511742017-10-31 18:04:35 +0800140const (
Colin Cross7113d202019-11-20 16:39:12 -0800141 coreVariant = "android_arm64_armv8-a_shared"
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900142 vendorVariant = "android_vendor.29_arm64_armv8-a_shared"
143 productVariant = "android_product.29_arm64_armv8-a_shared"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800144 recoveryVariant = "android_recovery_arm64_armv8-a_shared"
Logan Chienf3511742017-10-31 18:04:35 +0800145)
146
Paul Duffindb462dd2021-03-21 22:01:55 +0000147// Test that the PrepareForTestWithCcDefaultModules provides all the files that it uses by
148// running it in a fixture that requires all source files to exist.
149func TestPrepareForTestWithCcDefaultModules(t *testing.T) {
150 android.GroupFixturePreparers(
151 PrepareForTestWithCcDefaultModules,
152 android.PrepareForTestDisallowNonExistentPaths,
153 ).RunTest(t)
154}
155
Jiyong Park6a43f042017-10-12 23:05:00 +0900156func TestVendorSrc(t *testing.T) {
157 ctx := testCc(t, `
158 cc_library {
159 name: "libTest",
160 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700161 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800162 nocrt: true,
163 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900164 vendor_available: true,
165 target: {
166 vendor: {
167 srcs: ["bar.c"],
168 },
169 },
170 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900171 `)
172
Logan Chienf3511742017-10-31 18:04:35 +0800173 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900174 var objs []string
175 for _, o := range ld.Inputs {
176 objs = append(objs, o.Base())
177 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800178 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900179 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
180 }
181}
182
Justin Yun7f99ec72021-04-12 13:19:28 +0900183func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
184 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
185 partitionDefined := false
186 checkPartition := func(specific bool, partition string) {
187 if specific {
188 if expected != partition && !partitionDefined {
189 // The variant is installed to the 'partition'
190 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
191 }
192 partitionDefined = true
193 } else {
194 // The variant is not installed to the 'partition'
195 if expected == partition {
196 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
197 }
198 }
199 }
200 socSpecific := func(m *Module) bool {
201 return m.SocSpecific() || m.socSpecificModuleContext()
202 }
203 deviceSpecific := func(m *Module) bool {
204 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
205 }
206 productSpecific := func(m *Module) bool {
207 return m.ProductSpecific() || m.productSpecificModuleContext()
208 }
209 systemExtSpecific := func(m *Module) bool {
210 return m.SystemExtSpecific()
211 }
212 checkPartition(socSpecific(mod), "vendor")
213 checkPartition(deviceSpecific(mod), "odm")
214 checkPartition(productSpecific(mod), "product")
215 checkPartition(systemExtSpecific(mod), "system_ext")
216 if !partitionDefined && expected != "system" {
217 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
218 " but installed to system partition", variant, name, expected)
219 }
220}
221
222func TestInstallPartition(t *testing.T) {
223 t.Helper()
224 ctx := prepareForCcTest.RunTestWithBp(t, `
225 cc_library {
226 name: "libsystem",
227 }
228 cc_library {
229 name: "libsystem_ext",
230 system_ext_specific: true,
231 }
232 cc_library {
233 name: "libproduct",
234 product_specific: true,
235 }
236 cc_library {
237 name: "libvendor",
238 vendor: true,
239 }
240 cc_library {
241 name: "libodm",
242 device_specific: true,
243 }
244 cc_library {
245 name: "liball_available",
246 vendor_available: true,
247 product_available: true,
248 }
249 cc_library {
250 name: "libsystem_ext_all_available",
251 system_ext_specific: true,
252 vendor_available: true,
253 product_available: true,
254 }
255 cc_library {
256 name: "liball_available_odm",
257 odm_available: true,
258 product_available: true,
259 }
260 cc_library {
261 name: "libproduct_vendoravailable",
262 product_specific: true,
263 vendor_available: true,
264 }
265 cc_library {
266 name: "libproduct_odmavailable",
267 product_specific: true,
268 odm_available: true,
269 }
270 `).TestContext
271
272 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
273 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
274 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
275 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
276 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
277
278 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
279 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
280 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
281
282 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
283 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
284 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
285
286 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
287 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
288 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
289
290 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
291 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
292
293 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
294 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
295}
296
Logan Chienf3511742017-10-31 18:04:35 +0800297func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900298 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800299
Logan Chiend3c59a22018-03-29 14:08:15 +0800300 t.Helper()
301
Justin Yun0ecf0b22020-02-28 15:07:59 +0900302 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800303
304 // Check library properties.
305 lib, ok := mod.compiler.(*libraryDecorator)
306 if !ok {
307 t.Errorf("%q must have libraryDecorator", name)
308 } else if lib.baseInstaller.subDir != subDir {
309 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
310 lib.baseInstaller.subDir)
311 }
312
313 // Check VNDK properties.
314 if mod.vndkdep == nil {
315 t.Fatalf("%q must have `vndkdep`", name)
316 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700317 if !mod.IsVndk() {
318 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800319 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400320 if mod.IsVndkSp() != isVndkSp {
321 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800322 }
323
324 // Check VNDK extension properties.
325 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500326 if mod.IsVndkExt() != isVndkExt {
327 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800328 }
329
330 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
331 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
332 }
333}
334
Jooyung Han2216fb12019-11-06 16:46:15 +0900335func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
336 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800337 content := android.ContentFromFileRuleForTests(t, params)
338 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900339 assertArrayString(t, actual, expected)
340}
341
Jooyung Han097087b2019-10-22 19:32:18 +0900342func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
343 t.Helper()
344 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900345 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
346}
347
348func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
349 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800350 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800351 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900352}
353
Logan Chienf3511742017-10-31 18:04:35 +0800354func TestVndk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800355 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800356 cc_library {
357 name: "libvndk",
358 vendor_available: true,
359 vndk: {
360 enabled: true,
361 },
362 nocrt: true,
363 }
364
365 cc_library {
366 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900367 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800368 vndk: {
369 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900370 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800371 },
372 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900373 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800374 }
375
376 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900377 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800378 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900379 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800380 vndk: {
381 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900382 },
383 nocrt: true,
384 target: {
385 vendor: {
386 cflags: ["-DTEST"],
387 },
388 product: {
389 cflags: ["-DTEST"],
390 },
391 },
392 }
393
394 cc_library {
395 name: "libvndk_sp",
396 vendor_available: true,
397 vndk: {
398 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800399 support_system_process: true,
400 },
401 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900402 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800403 }
404
405 cc_library {
406 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900407 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800408 vndk: {
409 enabled: true,
410 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900411 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800412 },
413 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900414 target: {
415 vendor: {
416 suffix: "-x",
417 },
418 },
Logan Chienf3511742017-10-31 18:04:35 +0800419 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900420
421 cc_library {
422 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900423 vendor_available: true,
424 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900425 vndk: {
426 enabled: true,
427 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900428 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900429 },
430 nocrt: true,
431 target: {
432 vendor: {
433 suffix: "-x",
434 },
435 product: {
436 suffix: "-x",
437 },
438 },
439 }
440
Justin Yun450ae722021-04-16 19:58:18 +0900441 cc_library {
442 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700443 llndk: {
444 symbol_file: "libllndk.map.txt",
445 export_llndk_headers: ["libllndk_headers"],
446 }
Justin Yun450ae722021-04-16 19:58:18 +0900447 }
448
Justin Yun611e8862021-05-24 18:17:33 +0900449 cc_library {
450 name: "libclang_rt.hwasan-llndk",
451 llndk: {
452 symbol_file: "libclang_rt.hwasan.map.txt",
453 }
454 }
455
Colin Cross627280f2021-04-26 16:53:58 -0700456 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900457 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700458 llndk: {
459 llndk_headers: true,
460 },
Justin Yun450ae722021-04-16 19:58:18 +0900461 export_include_dirs: ["include"],
462 }
463
Colin Crosse4e44bc2020-12-28 13:50:21 -0800464 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900465 name: "llndk.libraries.txt",
466 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800467 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900468 name: "vndkcore.libraries.txt",
469 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800470 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900471 name: "vndksp.libraries.txt",
472 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800473 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900474 name: "vndkprivate.libraries.txt",
475 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800476 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900477 name: "vndkproduct.libraries.txt",
478 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800479 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900480 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800481 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900482 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800483 `
484
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000485 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800486 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900487 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900488 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800489
490 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800491
Jooyung Han261e1582020-10-20 18:54:21 +0900492 // subdir == "" because VNDK libs are not supposed to be installed separately.
493 // They are installed as part of VNDK APEX instead.
494 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
495 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900496 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900497 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
498 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900499 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900500
Justin Yun6977e8a2020-10-29 18:24:11 +0900501 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
502 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900503
Inseob Kim1f086e22019-05-09 13:29:15 +0900504 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900505 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000506 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900507
508 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
509 "arm64", "armv8-a"))
510 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
511 "arm", "armv7-a-neon"))
512
513 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
514 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900515 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
516
Inseob Kim1f086e22019-05-09 13:29:15 +0900517 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
518 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900519 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900520
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900521 variant := "android_vendor.29_arm64_armv8-a_shared"
522 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900523
Inseob Kim7f283f42020-06-01 21:53:49 +0900524 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
525
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400526 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
527 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
528 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
529 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
530 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
531 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
532 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
533 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900534
Jooyung Han39edb6c2019-11-06 16:53:07 +0900535 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800536 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
537 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
538 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
539 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
540 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900541
Jooyung Han097087b2019-10-22 19:32:18 +0900542 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
543 "LLNDK: libc.so",
544 "LLNDK: libdl.so",
545 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900546 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900547 "LLNDK: libm.so",
548 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900549 "VNDK-SP: libvndk_sp-x.so",
550 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900551 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900552 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900553 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900554 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900555 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900556 "VNDK-private: libvndk-private.so",
557 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900558 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900559 "VNDK-product: libc++.so",
560 "VNDK-product: libvndk_product.so",
561 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900562 })
Justin Yun611e8862021-05-24 18:17:33 +0900563 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 +0900564 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
565 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
566 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 +0900567 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 +0900568 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
569}
570
Yo Chiangbba545e2020-06-09 16:15:37 +0800571func TestVndkWithHostSupported(t *testing.T) {
572 ctx := testCc(t, `
573 cc_library {
574 name: "libvndk_host_supported",
575 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900576 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800577 vndk: {
578 enabled: true,
579 },
580 host_supported: true,
581 }
582
583 cc_library {
584 name: "libvndk_host_supported_but_disabled_on_device",
585 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900586 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800587 vndk: {
588 enabled: true,
589 },
590 host_supported: true,
591 enabled: false,
592 target: {
593 host: {
594 enabled: true,
595 }
596 }
597 }
598
Colin Crosse4e44bc2020-12-28 13:50:21 -0800599 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800600 name: "vndkcore.libraries.txt",
601 }
602 `)
603
604 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
605}
606
Jooyung Han2216fb12019-11-06 16:46:15 +0900607func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800608 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800609 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900610 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800611 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800612 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000613 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800614 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900615 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800616 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900617
Colin Cross45bce852021-11-11 22:47:54 -0800618 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700619 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900620 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900621}
622
623func TestVndkUsingCoreVariant(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800624 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900625 cc_library {
626 name: "libvndk",
627 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900628 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900629 vndk: {
630 enabled: true,
631 },
632 nocrt: true,
633 }
634
635 cc_library {
636 name: "libvndk_sp",
637 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900638 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900639 vndk: {
640 enabled: true,
641 support_system_process: true,
642 },
643 nocrt: true,
644 }
645
646 cc_library {
647 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900648 vendor_available: true,
649 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900650 vndk: {
651 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900652 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900653 },
654 nocrt: true,
655 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900656
Colin Crosse4e44bc2020-12-28 13:50:21 -0800657 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900658 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800659 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900660 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800661 `
662
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000663 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800664 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900665 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800666 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
667
668 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
669
670 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900671
Jooyung Han2216fb12019-11-06 16:46:15 +0900672 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900673}
674
Chris Parsons79d66a52020-06-05 17:26:16 -0400675func TestDataLibs(t *testing.T) {
676 bp := `
677 cc_test_library {
678 name: "test_lib",
679 srcs: ["test_lib.cpp"],
680 gtest: false,
681 }
682
683 cc_test {
684 name: "main_test",
685 data_libs: ["test_lib"],
686 gtest: false,
687 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400688 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400689
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000690 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400691 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900692 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400693 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
694
695 ctx := testCcWithConfig(t, config)
696 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
697 testBinary := module.(*Module).linker.(*testBinary)
698 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
699 if err != nil {
700 t.Errorf("Expected cc_test to produce output files, error: %s", err)
701 return
702 }
703 if len(outputFiles) != 1 {
704 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
705 return
706 }
707 if len(testBinary.dataPaths()) != 1 {
708 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
709 return
710 }
711
712 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400713 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400714
715 if !strings.HasSuffix(outputPath, "/main_test") {
716 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
717 return
718 }
719 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
720 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
721 return
722 }
723}
724
Chris Parsons216e10a2020-07-09 17:12:52 -0400725func TestDataLibsRelativeInstallPath(t *testing.T) {
726 bp := `
727 cc_test_library {
728 name: "test_lib",
729 srcs: ["test_lib.cpp"],
730 relative_install_path: "foo/bar/baz",
731 gtest: false,
732 }
733
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400734 cc_binary {
735 name: "test_bin",
736 relative_install_path: "foo/bar/baz",
737 compile_multilib: "both",
738 }
739
Chris Parsons216e10a2020-07-09 17:12:52 -0400740 cc_test {
741 name: "main_test",
742 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400743 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400744 gtest: false,
745 }
746 `
747
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000748 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400749 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900750 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400751 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
752
753 ctx := testCcWithConfig(t, config)
754 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
755 testBinary := module.(*Module).linker.(*testBinary)
756 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
757 if err != nil {
758 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
759 }
760 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400761 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400762 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400763 if len(testBinary.dataPaths()) != 2 {
764 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400765 }
766
767 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400768
769 if !strings.HasSuffix(outputPath, "/main_test") {
770 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
771 }
Colin Crossaa255532020-07-03 13:18:24 -0700772 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400773 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
774 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400775 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400776 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400777 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
778 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
779 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
780 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400781}
782
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000783func TestTestBinaryTestSuites(t *testing.T) {
784 bp := `
785 cc_test {
786 name: "main_test",
787 srcs: ["main_test.cpp"],
788 test_suites: [
789 "suite_1",
790 "suite_2",
791 ],
792 gtest: false,
793 }
794 `
795
796 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
797 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
798
799 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
800 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
801 if len(compatEntries) != 2 {
802 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
803 }
804 if compatEntries[0] != "suite_1" {
805 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
806 " but was '%s'", compatEntries[0])
807 }
808 if compatEntries[1] != "suite_2" {
809 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
810 " but was '%s'", compatEntries[1])
811 }
812}
813
814func TestTestLibraryTestSuites(t *testing.T) {
815 bp := `
816 cc_test_library {
817 name: "main_test_lib",
818 srcs: ["main_test_lib.cpp"],
819 test_suites: [
820 "suite_1",
821 "suite_2",
822 ],
823 gtest: false,
824 }
825 `
826
827 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
828 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
829
830 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
831 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
832 if len(compatEntries) != 2 {
833 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
834 }
835 if compatEntries[0] != "suite_1" {
836 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
837 " but was '%s'", compatEntries[0])
838 }
839 if compatEntries[1] != "suite_2" {
840 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
841 " but was '%s'", compatEntries[1])
842 }
843}
844
Jooyung Han0302a842019-10-30 18:43:49 +0900845func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Jooyung Han2216fb12019-11-06 16:46:15 +0900846 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900847 cc_library {
848 name: "libvndk",
849 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900850 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900851 vndk: {
852 enabled: true,
853 },
854 nocrt: true,
855 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900856 cc_library {
857 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900858 vendor_available: true,
859 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900860 vndk: {
861 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900862 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900863 },
864 nocrt: true,
865 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800866
867 cc_library {
868 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700869 llndk: {
870 symbol_file: "libllndk.map.txt",
871 export_llndk_headers: ["libllndk_headers"],
872 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800873 }
874
Colin Cross627280f2021-04-26 16:53:58 -0700875 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800876 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700877 llndk: {
878 symbol_file: "libllndk.map.txt",
879 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800880 export_include_dirs: ["include"],
881 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900882 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900883
884 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
885 "LLNDK: libc.so",
886 "LLNDK: libdl.so",
887 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800888 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900889 "LLNDK: libm.so",
890 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900891 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900892 "VNDK-core: libvndk.so",
893 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900894 "VNDK-private: libvndk-private.so",
895 "VNDK-product: libc++.so",
896 "VNDK-product: libvndk-private.so",
897 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900898 })
Logan Chienf3511742017-10-31 18:04:35 +0800899}
900
Justin Yun63e9ec72020-10-29 16:49:43 +0900901func TestVndkModuleError(t *testing.T) {
902 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900903 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900904 cc_library {
905 name: "libvndk",
906 vndk: {
907 enabled: true,
908 },
909 nocrt: true,
910 }
911 `)
912
Justin Yunc0d8c492021-01-07 17:45:31 +0900913 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900914 cc_library {
915 name: "libvndk",
916 product_available: true,
917 vndk: {
918 enabled: true,
919 },
920 nocrt: true,
921 }
922 `)
923
Justin Yun6977e8a2020-10-29 18:24:11 +0900924 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
925 cc_library {
926 name: "libvndkprop",
927 vendor_available: true,
928 product_available: true,
929 vndk: {
930 enabled: true,
931 },
932 nocrt: true,
933 target: {
934 vendor: {
935 cflags: ["-DTEST",],
936 },
937 },
938 }
939 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900940}
941
Logan Chiend3c59a22018-03-29 14:08:15 +0800942func TestVndkDepError(t *testing.T) {
943 // Check whether an error is emitted when a VNDK lib depends on a system lib.
944 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
945 cc_library {
946 name: "libvndk",
947 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900948 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800949 vndk: {
950 enabled: true,
951 },
952 shared_libs: ["libfwk"], // Cause error
953 nocrt: true,
954 }
955
956 cc_library {
957 name: "libfwk",
958 nocrt: true,
959 }
960 `)
961
962 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
963 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
964 cc_library {
965 name: "libvndk",
966 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900967 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800968 vndk: {
969 enabled: true,
970 },
971 shared_libs: ["libvendor"], // Cause error
972 nocrt: true,
973 }
974
975 cc_library {
976 name: "libvendor",
977 vendor: true,
978 nocrt: true,
979 }
980 `)
981
982 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
983 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
984 cc_library {
985 name: "libvndk_sp",
986 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900987 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800988 vndk: {
989 enabled: true,
990 support_system_process: true,
991 },
992 shared_libs: ["libfwk"], // Cause error
993 nocrt: true,
994 }
995
996 cc_library {
997 name: "libfwk",
998 nocrt: true,
999 }
1000 `)
1001
1002 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1003 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1004 cc_library {
1005 name: "libvndk_sp",
1006 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001007 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001008 vndk: {
1009 enabled: true,
1010 support_system_process: true,
1011 },
1012 shared_libs: ["libvendor"], // Cause error
1013 nocrt: true,
1014 }
1015
1016 cc_library {
1017 name: "libvendor",
1018 vendor: true,
1019 nocrt: true,
1020 }
1021 `)
1022
1023 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1024 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1025 cc_library {
1026 name: "libvndk_sp",
1027 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001028 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001029 vndk: {
1030 enabled: true,
1031 support_system_process: true,
1032 },
1033 shared_libs: ["libvndk"], // Cause error
1034 nocrt: true,
1035 }
1036
1037 cc_library {
1038 name: "libvndk",
1039 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001040 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001041 vndk: {
1042 enabled: true,
1043 },
1044 nocrt: true,
1045 }
1046 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001047
1048 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1049 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1050 cc_library {
1051 name: "libvndk",
1052 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001053 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001054 vndk: {
1055 enabled: true,
1056 },
1057 shared_libs: ["libnonvndk"],
1058 nocrt: true,
1059 }
1060
1061 cc_library {
1062 name: "libnonvndk",
1063 vendor_available: true,
1064 nocrt: true,
1065 }
1066 `)
1067
1068 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1069 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1070 cc_library {
1071 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001072 vendor_available: true,
1073 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001074 vndk: {
1075 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001076 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001077 },
1078 shared_libs: ["libnonvndk"],
1079 nocrt: true,
1080 }
1081
1082 cc_library {
1083 name: "libnonvndk",
1084 vendor_available: true,
1085 nocrt: true,
1086 }
1087 `)
1088
1089 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1090 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1091 cc_library {
1092 name: "libvndksp",
1093 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001094 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001095 vndk: {
1096 enabled: true,
1097 support_system_process: true,
1098 },
1099 shared_libs: ["libnonvndk"],
1100 nocrt: true,
1101 }
1102
1103 cc_library {
1104 name: "libnonvndk",
1105 vendor_available: true,
1106 nocrt: true,
1107 }
1108 `)
1109
1110 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1111 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1112 cc_library {
1113 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001114 vendor_available: true,
1115 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001116 vndk: {
1117 enabled: true,
1118 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001119 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001120 },
1121 shared_libs: ["libnonvndk"],
1122 nocrt: true,
1123 }
1124
1125 cc_library {
1126 name: "libnonvndk",
1127 vendor_available: true,
1128 nocrt: true,
1129 }
1130 `)
1131}
1132
1133func TestDoubleLoadbleDep(t *testing.T) {
1134 // okay to link : LLNDK -> double_loadable VNDK
1135 testCc(t, `
1136 cc_library {
1137 name: "libllndk",
1138 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001139 llndk: {
1140 symbol_file: "libllndk.map.txt",
1141 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001142 }
1143
1144 cc_library {
1145 name: "libdoubleloadable",
1146 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001147 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 vndk: {
1149 enabled: true,
1150 },
1151 double_loadable: true,
1152 }
1153 `)
1154 // okay to link : LLNDK -> VNDK-SP
1155 testCc(t, `
1156 cc_library {
1157 name: "libllndk",
1158 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001159 llndk: {
1160 symbol_file: "libllndk.map.txt",
1161 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001162 }
1163
1164 cc_library {
1165 name: "libvndksp",
1166 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001167 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001168 vndk: {
1169 enabled: true,
1170 support_system_process: true,
1171 },
1172 }
1173 `)
1174 // okay to link : double_loadable -> double_loadable
1175 testCc(t, `
1176 cc_library {
1177 name: "libdoubleloadable1",
1178 shared_libs: ["libdoubleloadable2"],
1179 vendor_available: true,
1180 double_loadable: true,
1181 }
1182
1183 cc_library {
1184 name: "libdoubleloadable2",
1185 vendor_available: true,
1186 double_loadable: true,
1187 }
1188 `)
1189 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1190 testCc(t, `
1191 cc_library {
1192 name: "libdoubleloadable",
1193 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001194 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001195 vndk: {
1196 enabled: true,
1197 },
1198 double_loadable: true,
1199 shared_libs: ["libnondoubleloadable"],
1200 }
1201
1202 cc_library {
1203 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001204 vendor_available: true,
1205 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001206 vndk: {
1207 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001208 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 },
1210 double_loadable: true,
1211 }
1212 `)
1213 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1214 testCc(t, `
1215 cc_library {
1216 name: "libllndk",
1217 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001218 llndk: {
1219 symbol_file: "libllndk.map.txt",
1220 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001221 }
1222
1223 cc_library {
1224 name: "libcoreonly",
1225 shared_libs: ["libvendoravailable"],
1226 }
1227
1228 // indirect dependency of LLNDK
1229 cc_library {
1230 name: "libvendoravailable",
1231 vendor_available: true,
1232 double_loadable: true,
1233 }
1234 `)
1235}
1236
1237func TestDoubleLoadableDepError(t *testing.T) {
1238 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1239 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1240 cc_library {
1241 name: "libllndk",
1242 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001243 llndk: {
1244 symbol_file: "libllndk.map.txt",
1245 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001246 }
1247
1248 cc_library {
1249 name: "libnondoubleloadable",
1250 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001251 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001252 vndk: {
1253 enabled: true,
1254 },
1255 }
1256 `)
1257
1258 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1259 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1260 cc_library {
1261 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001262 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001263 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001264 llndk: {
1265 symbol_file: "libllndk.map.txt",
1266 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001267 }
1268
1269 cc_library {
1270 name: "libnondoubleloadable",
1271 vendor_available: true,
1272 }
1273 `)
1274
Jooyung Hana70f0672019-01-18 15:20:43 +09001275 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1276 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1277 cc_library {
1278 name: "libllndk",
1279 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001280 llndk: {
1281 symbol_file: "libllndk.map.txt",
1282 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001283 }
1284
1285 cc_library {
1286 name: "libcoreonly",
1287 shared_libs: ["libvendoravailable"],
1288 }
1289
1290 // indirect dependency of LLNDK
1291 cc_library {
1292 name: "libvendoravailable",
1293 vendor_available: true,
1294 }
1295 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001296
1297 // The error is not from 'client' but from 'libllndk'
1298 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1299 cc_library {
1300 name: "client",
1301 vendor_available: true,
1302 double_loadable: true,
1303 shared_libs: ["libllndk"],
1304 }
1305 cc_library {
1306 name: "libllndk",
1307 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001308 llndk: {
1309 symbol_file: "libllndk.map.txt",
1310 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001311 }
1312 cc_library {
1313 name: "libnondoubleloadable",
1314 vendor_available: true,
1315 }
1316 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001317}
1318
Jooyung Han479ca172020-10-19 18:51:07 +09001319func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
1320 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1321 cc_library {
1322 name: "libvndksp",
1323 shared_libs: ["libanothervndksp"],
1324 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001325 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001326 vndk: {
1327 enabled: true,
1328 support_system_process: true,
1329 }
1330 }
1331
1332 cc_library {
1333 name: "libllndk",
1334 shared_libs: ["libanothervndksp"],
1335 }
1336
Jooyung Han479ca172020-10-19 18:51:07 +09001337 cc_library {
1338 name: "libanothervndksp",
1339 vendor_available: true,
1340 }
1341 `)
1342}
1343
Logan Chienf3511742017-10-31 18:04:35 +08001344func TestVndkExt(t *testing.T) {
1345 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001346 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001347 cc_library {
1348 name: "libvndk",
1349 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001350 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001351 vndk: {
1352 enabled: true,
1353 },
1354 nocrt: true,
1355 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001356 cc_library {
1357 name: "libvndk2",
1358 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001359 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001360 vndk: {
1361 enabled: true,
1362 },
1363 target: {
1364 vendor: {
1365 suffix: "-suffix",
1366 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001367 product: {
1368 suffix: "-suffix",
1369 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001370 },
1371 nocrt: true,
1372 }
Logan Chienf3511742017-10-31 18:04:35 +08001373
1374 cc_library {
1375 name: "libvndk_ext",
1376 vendor: true,
1377 vndk: {
1378 enabled: true,
1379 extends: "libvndk",
1380 },
1381 nocrt: true,
1382 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001383
1384 cc_library {
1385 name: "libvndk2_ext",
1386 vendor: true,
1387 vndk: {
1388 enabled: true,
1389 extends: "libvndk2",
1390 },
1391 nocrt: true,
1392 }
Logan Chienf3511742017-10-31 18:04:35 +08001393
Justin Yun0ecf0b22020-02-28 15:07:59 +09001394 cc_library {
1395 name: "libvndk_ext_product",
1396 product_specific: true,
1397 vndk: {
1398 enabled: true,
1399 extends: "libvndk",
1400 },
1401 nocrt: true,
1402 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001403
Justin Yun0ecf0b22020-02-28 15:07:59 +09001404 cc_library {
1405 name: "libvndk2_ext_product",
1406 product_specific: true,
1407 vndk: {
1408 enabled: true,
1409 extends: "libvndk2",
1410 },
1411 nocrt: true,
1412 }
1413 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001414 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001415 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1416 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001417 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001418
1419 ctx := testCcWithConfig(t, config)
1420
1421 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1422 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1423
1424 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1425 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1426
1427 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1428 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001429}
1430
Logan Chiend3c59a22018-03-29 14:08:15 +08001431func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Logan Chienf3511742017-10-31 18:04:35 +08001432 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1433 ctx := testCcNoVndk(t, `
1434 cc_library {
1435 name: "libvndk",
1436 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001437 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001438 vndk: {
1439 enabled: true,
1440 },
1441 nocrt: true,
1442 }
1443
1444 cc_library {
1445 name: "libvndk_ext",
1446 vendor: true,
1447 vndk: {
1448 enabled: true,
1449 extends: "libvndk",
1450 },
1451 nocrt: true,
1452 }
1453 `)
1454
1455 // Ensures that the core variant of "libvndk_ext" can be found.
1456 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1457 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1458 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1459 }
1460}
1461
Justin Yun0ecf0b22020-02-28 15:07:59 +09001462func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
1463 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001464 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001465 cc_library {
1466 name: "libvndk",
1467 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001468 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001469 vndk: {
1470 enabled: true,
1471 },
1472 nocrt: true,
1473 }
1474
1475 cc_library {
1476 name: "libvndk_ext_product",
1477 product_specific: true,
1478 vndk: {
1479 enabled: true,
1480 extends: "libvndk",
1481 },
1482 nocrt: true,
1483 }
1484 `)
1485
1486 // Ensures that the core variant of "libvndk_ext_product" can be found.
1487 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1488 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1489 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1490 }
1491}
1492
Logan Chienf3511742017-10-31 18:04:35 +08001493func TestVndkExtError(t *testing.T) {
1494 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001495 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001496 cc_library {
1497 name: "libvndk",
1498 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001499 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001500 vndk: {
1501 enabled: true,
1502 },
1503 nocrt: true,
1504 }
1505
1506 cc_library {
1507 name: "libvndk_ext",
1508 vndk: {
1509 enabled: true,
1510 extends: "libvndk",
1511 },
1512 nocrt: true,
1513 }
1514 `)
1515
1516 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1517 cc_library {
1518 name: "libvndk",
1519 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001520 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001521 vndk: {
1522 enabled: true,
1523 },
1524 nocrt: true,
1525 }
1526
1527 cc_library {
1528 name: "libvndk_ext",
1529 vendor: true,
1530 vndk: {
1531 enabled: true,
1532 },
1533 nocrt: true,
1534 }
1535 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001536
1537 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1538 cc_library {
1539 name: "libvndk",
1540 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001541 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001542 vndk: {
1543 enabled: true,
1544 },
1545 nocrt: true,
1546 }
1547
1548 cc_library {
1549 name: "libvndk_ext_product",
1550 product_specific: true,
1551 vndk: {
1552 enabled: true,
1553 },
1554 nocrt: true,
1555 }
1556 `)
1557
1558 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1559 cc_library {
1560 name: "libvndk",
1561 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001562 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001563 vndk: {
1564 enabled: true,
1565 },
1566 nocrt: true,
1567 }
1568
1569 cc_library {
1570 name: "libvndk_ext_product",
1571 product_specific: true,
1572 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001573 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001574 vndk: {
1575 enabled: true,
1576 extends: "libvndk",
1577 },
1578 nocrt: true,
1579 }
1580 `)
Logan Chienf3511742017-10-31 18:04:35 +08001581}
1582
1583func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
1584 // This test ensures an error is emitted for inconsistent support_system_process.
1585 testCcError(t, "module \".*\" with mismatched support_system_process", `
1586 cc_library {
1587 name: "libvndk",
1588 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001589 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001590 vndk: {
1591 enabled: true,
1592 },
1593 nocrt: true,
1594 }
1595
1596 cc_library {
1597 name: "libvndk_sp_ext",
1598 vendor: true,
1599 vndk: {
1600 enabled: true,
1601 extends: "libvndk",
1602 support_system_process: true,
1603 },
1604 nocrt: true,
1605 }
1606 `)
1607
1608 testCcError(t, "module \".*\" with mismatched support_system_process", `
1609 cc_library {
1610 name: "libvndk_sp",
1611 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001612 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001613 vndk: {
1614 enabled: true,
1615 support_system_process: true,
1616 },
1617 nocrt: true,
1618 }
1619
1620 cc_library {
1621 name: "libvndk_ext",
1622 vendor: true,
1623 vndk: {
1624 enabled: true,
1625 extends: "libvndk_sp",
1626 },
1627 nocrt: true,
1628 }
1629 `)
1630}
1631
1632func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Logan Chiend3c59a22018-03-29 14:08:15 +08001633 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001634 // with `private: true`.
1635 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001636 cc_library {
1637 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001638 vendor_available: true,
1639 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001640 vndk: {
1641 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001642 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001643 },
1644 nocrt: true,
1645 }
1646
1647 cc_library {
1648 name: "libvndk_ext",
1649 vendor: true,
1650 vndk: {
1651 enabled: true,
1652 extends: "libvndk",
1653 },
1654 nocrt: true,
1655 }
1656 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001657
Justin Yunfd9e8042020-12-23 18:23:14 +09001658 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001659 cc_library {
1660 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001661 vendor_available: true,
1662 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001663 vndk: {
1664 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001665 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001666 },
1667 nocrt: true,
1668 }
1669
1670 cc_library {
1671 name: "libvndk_ext_product",
1672 product_specific: true,
1673 vndk: {
1674 enabled: true,
1675 extends: "libvndk",
1676 },
1677 nocrt: true,
1678 }
1679 `)
Logan Chienf3511742017-10-31 18:04:35 +08001680}
1681
Logan Chiend3c59a22018-03-29 14:08:15 +08001682func TestVendorModuleUseVndkExt(t *testing.T) {
1683 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001684 testCc(t, `
1685 cc_library {
1686 name: "libvndk",
1687 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001688 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001689 vndk: {
1690 enabled: true,
1691 },
1692 nocrt: true,
1693 }
1694
1695 cc_library {
1696 name: "libvndk_ext",
1697 vendor: true,
1698 vndk: {
1699 enabled: true,
1700 extends: "libvndk",
1701 },
1702 nocrt: true,
1703 }
1704
1705 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001706 name: "libvndk_sp",
1707 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001708 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001709 vndk: {
1710 enabled: true,
1711 support_system_process: true,
1712 },
1713 nocrt: true,
1714 }
1715
1716 cc_library {
1717 name: "libvndk_sp_ext",
1718 vendor: true,
1719 vndk: {
1720 enabled: true,
1721 extends: "libvndk_sp",
1722 support_system_process: true,
1723 },
1724 nocrt: true,
1725 }
1726
1727 cc_library {
1728 name: "libvendor",
1729 vendor: true,
1730 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1731 nocrt: true,
1732 }
1733 `)
1734}
1735
Logan Chiend3c59a22018-03-29 14:08:15 +08001736func TestVndkExtUseVendorLib(t *testing.T) {
1737 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001738 testCc(t, `
1739 cc_library {
1740 name: "libvndk",
1741 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001742 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001743 vndk: {
1744 enabled: true,
1745 },
1746 nocrt: true,
1747 }
1748
1749 cc_library {
1750 name: "libvndk_ext",
1751 vendor: true,
1752 vndk: {
1753 enabled: true,
1754 extends: "libvndk",
1755 },
1756 shared_libs: ["libvendor"],
1757 nocrt: true,
1758 }
1759
1760 cc_library {
1761 name: "libvendor",
1762 vendor: true,
1763 nocrt: true,
1764 }
1765 `)
Logan Chienf3511742017-10-31 18:04:35 +08001766
Logan Chiend3c59a22018-03-29 14:08:15 +08001767 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1768 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001769 cc_library {
1770 name: "libvndk_sp",
1771 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001772 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001773 vndk: {
1774 enabled: true,
1775 support_system_process: true,
1776 },
1777 nocrt: true,
1778 }
1779
1780 cc_library {
1781 name: "libvndk_sp_ext",
1782 vendor: true,
1783 vndk: {
1784 enabled: true,
1785 extends: "libvndk_sp",
1786 support_system_process: true,
1787 },
1788 shared_libs: ["libvendor"], // Cause an error
1789 nocrt: true,
1790 }
1791
1792 cc_library {
1793 name: "libvendor",
1794 vendor: true,
1795 nocrt: true,
1796 }
1797 `)
1798}
1799
Justin Yun0ecf0b22020-02-28 15:07:59 +09001800func TestProductVndkExtDependency(t *testing.T) {
1801 bp := `
1802 cc_library {
1803 name: "libvndk",
1804 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001805 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001806 vndk: {
1807 enabled: true,
1808 },
1809 nocrt: true,
1810 }
1811
1812 cc_library {
1813 name: "libvndk_ext_product",
1814 product_specific: true,
1815 vndk: {
1816 enabled: true,
1817 extends: "libvndk",
1818 },
1819 shared_libs: ["libproduct_for_vndklibs"],
1820 nocrt: true,
1821 }
1822
1823 cc_library {
1824 name: "libvndk_sp",
1825 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001826 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001827 vndk: {
1828 enabled: true,
1829 support_system_process: true,
1830 },
1831 nocrt: true,
1832 }
1833
1834 cc_library {
1835 name: "libvndk_sp_ext_product",
1836 product_specific: true,
1837 vndk: {
1838 enabled: true,
1839 extends: "libvndk_sp",
1840 support_system_process: true,
1841 },
1842 shared_libs: ["libproduct_for_vndklibs"],
1843 nocrt: true,
1844 }
1845
1846 cc_library {
1847 name: "libproduct",
1848 product_specific: true,
1849 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1850 nocrt: true,
1851 }
1852
1853 cc_library {
1854 name: "libproduct_for_vndklibs",
1855 product_specific: true,
1856 nocrt: true,
1857 }
1858 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001859 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001860 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1861 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001862 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001863
1864 testCcWithConfig(t, config)
1865}
1866
Logan Chiend3c59a22018-03-29 14:08:15 +08001867func TestVndkSpExtUseVndkError(t *testing.T) {
1868 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1869 // library.
1870 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1871 cc_library {
1872 name: "libvndk",
1873 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001874 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001875 vndk: {
1876 enabled: true,
1877 },
1878 nocrt: true,
1879 }
1880
1881 cc_library {
1882 name: "libvndk_sp",
1883 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001884 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001885 vndk: {
1886 enabled: true,
1887 support_system_process: true,
1888 },
1889 nocrt: true,
1890 }
1891
1892 cc_library {
1893 name: "libvndk_sp_ext",
1894 vendor: true,
1895 vndk: {
1896 enabled: true,
1897 extends: "libvndk_sp",
1898 support_system_process: true,
1899 },
1900 shared_libs: ["libvndk"], // Cause an error
1901 nocrt: true,
1902 }
1903 `)
1904
1905 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1906 // library.
1907 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1908 cc_library {
1909 name: "libvndk",
1910 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001911 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001912 vndk: {
1913 enabled: true,
1914 },
1915 nocrt: true,
1916 }
1917
1918 cc_library {
1919 name: "libvndk_ext",
1920 vendor: true,
1921 vndk: {
1922 enabled: true,
1923 extends: "libvndk",
1924 },
1925 nocrt: true,
1926 }
1927
1928 cc_library {
1929 name: "libvndk_sp",
1930 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001931 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001932 vndk: {
1933 enabled: true,
1934 support_system_process: true,
1935 },
1936 nocrt: true,
1937 }
1938
1939 cc_library {
1940 name: "libvndk_sp_ext",
1941 vendor: true,
1942 vndk: {
1943 enabled: true,
1944 extends: "libvndk_sp",
1945 support_system_process: true,
1946 },
1947 shared_libs: ["libvndk_ext"], // Cause an error
1948 nocrt: true,
1949 }
1950 `)
1951}
1952
1953func TestVndkUseVndkExtError(t *testing.T) {
1954 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1955 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001956 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1957 cc_library {
1958 name: "libvndk",
1959 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001960 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001961 vndk: {
1962 enabled: true,
1963 },
1964 nocrt: true,
1965 }
1966
1967 cc_library {
1968 name: "libvndk_ext",
1969 vendor: true,
1970 vndk: {
1971 enabled: true,
1972 extends: "libvndk",
1973 },
1974 nocrt: true,
1975 }
1976
1977 cc_library {
1978 name: "libvndk2",
1979 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001980 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001981 vndk: {
1982 enabled: true,
1983 },
1984 shared_libs: ["libvndk_ext"],
1985 nocrt: true,
1986 }
1987 `)
1988
Martin Stjernholmef449fe2018-11-06 16:12:13 +00001989 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08001990 cc_library {
1991 name: "libvndk",
1992 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001993 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001994 vndk: {
1995 enabled: true,
1996 },
1997 nocrt: true,
1998 }
1999
2000 cc_library {
2001 name: "libvndk_ext",
2002 vendor: true,
2003 vndk: {
2004 enabled: true,
2005 extends: "libvndk",
2006 },
2007 nocrt: true,
2008 }
2009
2010 cc_library {
2011 name: "libvndk2",
2012 vendor_available: true,
2013 vndk: {
2014 enabled: true,
2015 },
2016 target: {
2017 vendor: {
2018 shared_libs: ["libvndk_ext"],
2019 },
2020 },
2021 nocrt: true,
2022 }
2023 `)
2024
2025 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2026 cc_library {
2027 name: "libvndk_sp",
2028 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002029 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002030 vndk: {
2031 enabled: true,
2032 support_system_process: true,
2033 },
2034 nocrt: true,
2035 }
2036
2037 cc_library {
2038 name: "libvndk_sp_ext",
2039 vendor: true,
2040 vndk: {
2041 enabled: true,
2042 extends: "libvndk_sp",
2043 support_system_process: true,
2044 },
2045 nocrt: true,
2046 }
2047
2048 cc_library {
2049 name: "libvndk_sp_2",
2050 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002051 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002052 vndk: {
2053 enabled: true,
2054 support_system_process: true,
2055 },
2056 shared_libs: ["libvndk_sp_ext"],
2057 nocrt: true,
2058 }
2059 `)
2060
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002061 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002062 cc_library {
2063 name: "libvndk_sp",
2064 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002065 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002066 vndk: {
2067 enabled: true,
2068 },
2069 nocrt: true,
2070 }
2071
2072 cc_library {
2073 name: "libvndk_sp_ext",
2074 vendor: true,
2075 vndk: {
2076 enabled: true,
2077 extends: "libvndk_sp",
2078 },
2079 nocrt: true,
2080 }
2081
2082 cc_library {
2083 name: "libvndk_sp2",
2084 vendor_available: true,
2085 vndk: {
2086 enabled: true,
2087 },
2088 target: {
2089 vendor: {
2090 shared_libs: ["libvndk_sp_ext"],
2091 },
2092 },
2093 nocrt: true,
2094 }
2095 `)
2096}
2097
Justin Yun5f7f7e82019-11-18 19:52:14 +09002098func TestEnforceProductVndkVersion(t *testing.T) {
2099 bp := `
2100 cc_library {
2101 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002102 llndk: {
2103 symbol_file: "libllndk.map.txt",
2104 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002105 }
2106 cc_library {
2107 name: "libvndk",
2108 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002109 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002110 vndk: {
2111 enabled: true,
2112 },
2113 nocrt: true,
2114 }
2115 cc_library {
2116 name: "libvndk_sp",
2117 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002118 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002119 vndk: {
2120 enabled: true,
2121 support_system_process: true,
2122 },
2123 nocrt: true,
2124 }
2125 cc_library {
2126 name: "libva",
2127 vendor_available: true,
2128 nocrt: true,
2129 }
2130 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002131 name: "libpa",
2132 product_available: true,
2133 nocrt: true,
2134 }
2135 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002136 name: "libboth_available",
2137 vendor_available: true,
2138 product_available: true,
2139 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002140 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002141 target: {
2142 vendor: {
2143 suffix: "-vendor",
2144 },
2145 product: {
2146 suffix: "-product",
2147 },
2148 }
2149 }
2150 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002151 name: "libproduct_va",
2152 product_specific: true,
2153 vendor_available: true,
2154 nocrt: true,
2155 }
2156 cc_library {
2157 name: "libprod",
2158 product_specific: true,
2159 shared_libs: [
2160 "libllndk",
2161 "libvndk",
2162 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002163 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002164 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002165 "libproduct_va",
2166 ],
2167 nocrt: true,
2168 }
2169 cc_library {
2170 name: "libvendor",
2171 vendor: true,
2172 shared_libs: [
2173 "libllndk",
2174 "libvndk",
2175 "libvndk_sp",
2176 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002177 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002178 "libproduct_va",
2179 ],
2180 nocrt: true,
2181 }
2182 `
2183
Paul Duffin8567f222021-03-23 00:02:06 +00002184 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002185
Jooyung Han261e1582020-10-20 18:54:21 +09002186 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2187 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002188
2189 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2190 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2191
2192 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2193 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002194
2195 ensureStringContains := func(t *testing.T, str string, substr string) {
2196 t.Helper()
2197 if !strings.Contains(str, substr) {
2198 t.Errorf("%q is not found in %v", substr, str)
2199 }
2200 }
2201 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2202 t.Helper()
2203 if strings.Contains(str, substr) {
2204 t.Errorf("%q is found in %v", substr, str)
2205 }
2206 }
2207
2208 // _static variant is used since _shared reuses *.o from the static variant
2209 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2210 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2211
2212 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2213 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2214 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2215 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2216
2217 product_cflags := product_static.Rule("cc").Args["cFlags"]
2218 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2219 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2220 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002221}
2222
2223func TestEnforceProductVndkVersionErrors(t *testing.T) {
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002224 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002225 cc_library {
2226 name: "libprod",
2227 product_specific: true,
2228 shared_libs: [
2229 "libvendor",
2230 ],
2231 nocrt: true,
2232 }
2233 cc_library {
2234 name: "libvendor",
2235 vendor: true,
2236 nocrt: true,
2237 }
2238 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002239 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002240 cc_library {
2241 name: "libprod",
2242 product_specific: true,
2243 shared_libs: [
2244 "libsystem",
2245 ],
2246 nocrt: true,
2247 }
2248 cc_library {
2249 name: "libsystem",
2250 nocrt: true,
2251 }
2252 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002253 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002254 cc_library {
2255 name: "libprod",
2256 product_specific: true,
2257 shared_libs: [
2258 "libva",
2259 ],
2260 nocrt: true,
2261 }
2262 cc_library {
2263 name: "libva",
2264 vendor_available: true,
2265 nocrt: true,
2266 }
2267 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002268 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002269 cc_library {
2270 name: "libprod",
2271 product_specific: true,
2272 shared_libs: [
2273 "libvndk_private",
2274 ],
2275 nocrt: true,
2276 }
2277 cc_library {
2278 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002279 vendor_available: true,
2280 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002281 vndk: {
2282 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002283 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002284 },
2285 nocrt: true,
2286 }
2287 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002288 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002289 cc_library {
2290 name: "libprod",
2291 product_specific: true,
2292 shared_libs: [
2293 "libsystem_ext",
2294 ],
2295 nocrt: true,
2296 }
2297 cc_library {
2298 name: "libsystem_ext",
2299 system_ext_specific: true,
2300 nocrt: true,
2301 }
2302 `)
2303 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2304 cc_library {
2305 name: "libsystem",
2306 shared_libs: [
2307 "libproduct_va",
2308 ],
2309 nocrt: true,
2310 }
2311 cc_library {
2312 name: "libproduct_va",
2313 product_specific: true,
2314 vendor_available: true,
2315 nocrt: true,
2316 }
2317 `)
2318}
2319
Jooyung Han38002912019-05-16 04:01:54 +09002320func TestMakeLinkType(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -08002321 bp := `
2322 cc_library {
2323 name: "libvndk",
2324 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002325 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002326 vndk: {
2327 enabled: true,
2328 },
2329 }
2330 cc_library {
2331 name: "libvndksp",
2332 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002333 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002334 vndk: {
2335 enabled: true,
2336 support_system_process: true,
2337 },
2338 }
2339 cc_library {
2340 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002341 vendor_available: true,
2342 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002343 vndk: {
2344 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002345 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002346 },
2347 }
2348 cc_library {
2349 name: "libvendor",
2350 vendor: true,
2351 }
2352 cc_library {
2353 name: "libvndkext",
2354 vendor: true,
2355 vndk: {
2356 enabled: true,
2357 extends: "libvndk",
2358 },
2359 }
2360 vndk_prebuilt_shared {
2361 name: "prevndk",
2362 version: "27",
2363 target_arch: "arm",
2364 binder32bit: true,
2365 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002366 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002367 vndk: {
2368 enabled: true,
2369 },
2370 arch: {
2371 arm: {
2372 srcs: ["liba.so"],
2373 },
2374 },
2375 }
2376 cc_library {
2377 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002378 llndk: {
2379 symbol_file: "libllndk.map.txt",
2380 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002381 }
2382 cc_library {
2383 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002384 llndk: {
2385 symbol_file: "libllndkprivate.map.txt",
2386 private: true,
2387 }
Colin Cross78212242021-01-06 14:51:30 -08002388 }
2389
2390 llndk_libraries_txt {
2391 name: "llndk.libraries.txt",
2392 }
2393 vndkcore_libraries_txt {
2394 name: "vndkcore.libraries.txt",
2395 }
2396 vndksp_libraries_txt {
2397 name: "vndksp.libraries.txt",
2398 }
2399 vndkprivate_libraries_txt {
2400 name: "vndkprivate.libraries.txt",
2401 }
2402 vndkcorevariant_libraries_txt {
2403 name: "vndkcorevariant.libraries.txt",
2404 insert_vndk_version: false,
2405 }
2406 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002407
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002408 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002409 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002410 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002411 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002412 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002413
Colin Cross78212242021-01-06 14:51:30 -08002414 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2415 []string{"libvndk.so", "libvndkprivate.so"})
2416 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2417 []string{"libc++.so", "libvndksp.so"})
2418 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2419 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2420 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2421 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002422
Colin Crossfb0c16e2019-11-20 17:12:35 -08002423 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002424
Jooyung Han38002912019-05-16 04:01:54 +09002425 tests := []struct {
2426 variant string
2427 name string
2428 expected string
2429 }{
2430 {vendorVariant, "libvndk", "native:vndk"},
2431 {vendorVariant, "libvndksp", "native:vndk"},
2432 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2433 {vendorVariant, "libvendor", "native:vendor"},
2434 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002435 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002436 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002437 {coreVariant, "libvndk", "native:platform"},
2438 {coreVariant, "libvndkprivate", "native:platform"},
2439 {coreVariant, "libllndk", "native:platform"},
2440 }
2441 for _, test := range tests {
2442 t.Run(test.name, func(t *testing.T) {
2443 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2444 assertString(t, module.makeLinkType, test.expected)
2445 })
2446 }
2447}
2448
Jeff Gaston294356f2017-09-27 17:05:30 -07002449var staticLinkDepOrderTestCases = []struct {
2450 // This is a string representation of a map[moduleName][]moduleDependency .
2451 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002452 inStatic string
2453
2454 // This is a string representation of a map[moduleName][]moduleDependency .
2455 // It models the dependencies declared in an Android.bp file.
2456 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002457
2458 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2459 // The keys of allOrdered specify which modules we would like to check.
2460 // The values of allOrdered specify the expected result (of the transitive closure of all
2461 // dependencies) for each module to test
2462 allOrdered string
2463
2464 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2465 // The keys of outOrdered specify which modules we would like to check.
2466 // The values of outOrdered specify the expected result (of the ordered linker command line)
2467 // for each module to test.
2468 outOrdered string
2469}{
2470 // Simple tests
2471 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002472 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002473 outOrdered: "",
2474 },
2475 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002476 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002477 outOrdered: "a:",
2478 },
2479 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002480 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002481 outOrdered: "a:b; b:",
2482 },
2483 // Tests of reordering
2484 {
2485 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002486 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002487 outOrdered: "a:b,c,d; b:d; c:d; d:",
2488 },
2489 {
2490 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002491 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002492 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2493 },
2494 {
2495 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002496 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002497 outOrdered: "a:d,b,e,c; d:b; e:c",
2498 },
2499 {
2500 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002501 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002502 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2503 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2504 },
2505 {
2506 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002507 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 -07002508 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2509 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2510 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002511 // shared dependencies
2512 {
2513 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2514 // So, we don't actually have to check that a shared dependency of c will change the order
2515 // of a library that depends statically on b and on c. We only need to check that if c has
2516 // a shared dependency on b, that that shows up in allOrdered.
2517 inShared: "c:b",
2518 allOrdered: "c:b",
2519 outOrdered: "c:",
2520 },
2521 {
2522 // This test doesn't actually include any shared dependencies but it's a reminder of what
2523 // the second phase of the above test would look like
2524 inStatic: "a:b,c; c:b",
2525 allOrdered: "a:c,b; c:b",
2526 outOrdered: "a:c,b; c:b",
2527 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002528 // tiebreakers for when two modules specifying different orderings and there is no dependency
2529 // to dictate an order
2530 {
2531 // 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 -08002532 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002533 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2534 },
2535 {
2536 // 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 -08002537 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 -07002538 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2539 },
2540 // Tests involving duplicate dependencies
2541 {
2542 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002543 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002544 outOrdered: "a:c,b",
2545 },
2546 {
2547 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002548 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002549 outOrdered: "a:d,c,b",
2550 },
2551 // Tests to confirm the nonexistence of infinite loops.
2552 // These cases should never happen, so as long as the test terminates and the
2553 // result is deterministic then that should be fine.
2554 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002555 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002556 outOrdered: "a:a",
2557 },
2558 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002559 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002560 allOrdered: "a:b,c; b:c,a; c:a,b",
2561 outOrdered: "a:b; b:c; c:a",
2562 },
2563 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002564 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002565 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2566 outOrdered: "a:c,b; b:a,c; c:b,a",
2567 },
2568}
2569
2570// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2571func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2572 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2573 strippedText := strings.Replace(text, " ", "", -1)
2574 if len(strippedText) < 1 {
2575 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2576 }
2577 allDeps = make(map[android.Path][]android.Path, 0)
2578
2579 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2580 moduleTexts := strings.Split(strippedText, ";")
2581
2582 outputForModuleName := func(moduleName string) android.Path {
2583 return android.PathForTesting(moduleName)
2584 }
2585
2586 for _, moduleText := range moduleTexts {
2587 // convert from "a:b,c" to ["a", "b,c"]
2588 components := strings.Split(moduleText, ":")
2589 if len(components) != 2 {
2590 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2591 }
2592 moduleName := components[0]
2593 moduleOutput := outputForModuleName(moduleName)
2594 modulesInOrder = append(modulesInOrder, moduleOutput)
2595
2596 depString := components[1]
2597 // convert from "b,c" to ["b", "c"]
2598 depNames := strings.Split(depString, ",")
2599 if len(depString) < 1 {
2600 depNames = []string{}
2601 }
2602 var deps []android.Path
2603 for _, depName := range depNames {
2604 deps = append(deps, outputForModuleName(depName))
2605 }
2606 allDeps[moduleOutput] = deps
2607 }
2608 return modulesInOrder, allDeps
2609}
2610
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002611func TestStaticLibDepReordering(t *testing.T) {
Jeff Gaston294356f2017-09-27 17:05:30 -07002612 ctx := testCc(t, `
2613 cc_library {
2614 name: "a",
2615 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002616 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002617 }
2618 cc_library {
2619 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002620 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002621 }
2622 cc_library {
2623 name: "c",
2624 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002625 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002626 }
2627 cc_library {
2628 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002629 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002630 }
2631
2632 `)
2633
Colin Cross7113d202019-11-20 16:39:12 -08002634 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002635 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002636 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2637 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002638 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002639
2640 if !reflect.DeepEqual(actual, expected) {
2641 t.Errorf("staticDeps orderings were not propagated correctly"+
2642 "\nactual: %v"+
2643 "\nexpected: %v",
2644 actual,
2645 expected,
2646 )
2647 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002648}
Jeff Gaston294356f2017-09-27 17:05:30 -07002649
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002650func TestStaticLibDepReorderingWithShared(t *testing.T) {
2651 ctx := testCc(t, `
2652 cc_library {
2653 name: "a",
2654 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002655 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002656 }
2657 cc_library {
2658 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002659 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002660 }
2661 cc_library {
2662 name: "c",
2663 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002664 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002665 }
2666
2667 `)
2668
Colin Cross7113d202019-11-20 16:39:12 -08002669 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002670 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002671 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2672 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002673 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002674
2675 if !reflect.DeepEqual(actual, expected) {
2676 t.Errorf("staticDeps orderings did not account for shared libs"+
2677 "\nactual: %v"+
2678 "\nexpected: %v",
2679 actual,
2680 expected,
2681 )
2682 }
2683}
2684
Jooyung Hanb04a4992020-03-13 18:57:35 +09002685func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002686 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002687 if !reflect.DeepEqual(actual, expected) {
2688 t.Errorf(message+
2689 "\nactual: %v"+
2690 "\nexpected: %v",
2691 actual,
2692 expected,
2693 )
2694 }
2695}
2696
Jooyung Han61b66e92020-03-21 14:21:46 +00002697func TestLlndkLibrary(t *testing.T) {
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002698 result := prepareForCcTest.RunTestWithBp(t, `
2699 cc_library {
2700 name: "libllndk",
2701 stubs: { versions: ["1", "2"] },
2702 llndk: {
2703 symbol_file: "libllndk.map.txt",
2704 },
2705 export_include_dirs: ["include"],
2706 }
2707
2708 cc_prebuilt_library_shared {
2709 name: "libllndkprebuilt",
2710 stubs: { versions: ["1", "2"] },
2711 llndk: {
2712 symbol_file: "libllndkprebuilt.map.txt",
2713 },
2714 }
2715
2716 cc_library {
2717 name: "libllndk_with_external_headers",
2718 stubs: { versions: ["1", "2"] },
2719 llndk: {
2720 symbol_file: "libllndk.map.txt",
2721 export_llndk_headers: ["libexternal_llndk_headers"],
2722 },
2723 header_libs: ["libexternal_headers"],
2724 export_header_lib_headers: ["libexternal_headers"],
2725 }
2726 cc_library_headers {
2727 name: "libexternal_headers",
2728 export_include_dirs: ["include"],
2729 vendor_available: true,
2730 }
2731 cc_library_headers {
2732 name: "libexternal_llndk_headers",
2733 export_include_dirs: ["include_llndk"],
2734 llndk: {
2735 symbol_file: "libllndk.map.txt",
2736 },
2737 vendor_available: true,
2738 }
2739
2740 cc_library {
2741 name: "libllndk_with_override_headers",
2742 stubs: { versions: ["1", "2"] },
2743 llndk: {
2744 symbol_file: "libllndk.map.txt",
2745 override_export_include_dirs: ["include_llndk"],
2746 },
2747 export_include_dirs: ["include"],
2748 }
2749 `)
2750 actual := result.ModuleVariantsForTests("libllndk")
2751 for i := 0; i < len(actual); i++ {
2752 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2753 actual = append(actual[:i], actual[i+1:]...)
2754 i--
2755 }
2756 }
2757 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002758 "android_vendor.29_arm64_armv8-a_shared_current",
2759 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002760 "android_vendor.29_arm_armv7-a-neon_shared_current",
2761 "android_vendor.29_arm_armv7-a-neon_shared",
2762 }
2763 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2764
2765 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2766 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2767
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002768 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2769 t.Helper()
2770 m := result.ModuleForTests(module, variant).Module()
2771 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2772 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2773 expectedDirs, f.IncludeDirs)
2774 }
2775
2776 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2777 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2778 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2779 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2780 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2781 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2782}
2783
Jiyong Parka46a4d52017-12-14 19:54:34 +09002784func TestLlndkHeaders(t *testing.T) {
2785 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002786 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002787 name: "libllndk_headers",
2788 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002789 llndk: {
2790 llndk_headers: true,
2791 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002792 }
2793 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002794 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002795 llndk: {
2796 symbol_file: "libllndk.map.txt",
2797 export_llndk_headers: ["libllndk_headers"],
2798 }
Colin Cross0477b422020-10-13 18:43:54 -07002799 }
2800
2801 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002802 name: "libvendor",
2803 shared_libs: ["libllndk"],
2804 vendor: true,
2805 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002806 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002807 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002808 }
2809 `)
2810
2811 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002812 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002813 cflags := cc.Args["cFlags"]
2814 if !strings.Contains(cflags, "-Imy_include") {
2815 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2816 }
2817}
2818
Logan Chien43d34c32017-12-20 01:17:32 +08002819func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2820 actual := module.Properties.AndroidMkRuntimeLibs
2821 if !reflect.DeepEqual(actual, expected) {
2822 t.Errorf("incorrect runtime_libs for shared libs"+
2823 "\nactual: %v"+
2824 "\nexpected: %v",
2825 actual,
2826 expected,
2827 )
2828 }
2829}
2830
2831const runtimeLibAndroidBp = `
2832 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002833 name: "liball_available",
2834 vendor_available: true,
2835 product_available: true,
2836 no_libcrt : true,
2837 nocrt : true,
2838 system_shared_libs : [],
2839 }
2840 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002841 name: "libvendor_available1",
2842 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002843 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002844 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002845 nocrt : true,
2846 system_shared_libs : [],
2847 }
2848 cc_library {
2849 name: "libvendor_available2",
2850 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002851 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002852 target: {
2853 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002854 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002855 }
2856 },
Yi Konge7fe9912019-06-02 00:53:50 -07002857 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002858 nocrt : true,
2859 system_shared_libs : [],
2860 }
2861 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002862 name: "libproduct_vendor",
2863 product_specific: true,
2864 vendor_available: true,
2865 no_libcrt : true,
2866 nocrt : true,
2867 system_shared_libs : [],
2868 }
2869 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002870 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002871 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002872 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002873 nocrt : true,
2874 system_shared_libs : [],
2875 }
2876 cc_library {
2877 name: "libvendor1",
2878 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002879 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002880 nocrt : true,
2881 system_shared_libs : [],
2882 }
2883 cc_library {
2884 name: "libvendor2",
2885 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002886 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002887 no_libcrt : true,
2888 nocrt : true,
2889 system_shared_libs : [],
2890 }
2891 cc_library {
2892 name: "libproduct_available1",
2893 product_available: true,
2894 runtime_libs: ["liball_available"],
2895 no_libcrt : true,
2896 nocrt : true,
2897 system_shared_libs : [],
2898 }
2899 cc_library {
2900 name: "libproduct1",
2901 product_specific: true,
2902 no_libcrt : true,
2903 nocrt : true,
2904 system_shared_libs : [],
2905 }
2906 cc_library {
2907 name: "libproduct2",
2908 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002909 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002910 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002911 nocrt : true,
2912 system_shared_libs : [],
2913 }
2914`
2915
2916func TestRuntimeLibs(t *testing.T) {
2917 ctx := testCc(t, runtimeLibAndroidBp)
2918
2919 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002920 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002921
Justin Yun8a2600c2020-12-07 12:44:03 +09002922 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2923 checkRuntimeLibs(t, []string{"liball_available"}, module)
2924
2925 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2926 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002927
2928 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002929 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002930
2931 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2932 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002933 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002934
Justin Yun8a2600c2020-12-07 12:44:03 +09002935 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2936 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002937
2938 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002939 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002940
2941 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2942 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002943 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002944
2945 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2946 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2947
2948 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002949 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002950}
2951
2952func TestExcludeRuntimeLibs(t *testing.T) {
2953 ctx := testCc(t, runtimeLibAndroidBp)
2954
Colin Cross7113d202019-11-20 16:39:12 -08002955 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002956 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2957 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002958
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002959 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002960 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002961 checkRuntimeLibs(t, nil, module)
2962}
2963
2964func TestRuntimeLibsNoVndk(t *testing.T) {
2965 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
2966
2967 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
2968
Colin Cross7113d202019-11-20 16:39:12 -08002969 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002970
Justin Yun8a2600c2020-12-07 12:44:03 +09002971 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2972 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002973
2974 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002975 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002976
2977 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002978 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002979}
2980
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002981func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09002982 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00002983 actual := module.Properties.AndroidMkStaticLibs
2984 if !reflect.DeepEqual(actual, expected) {
2985 t.Errorf("incorrect static_libs"+
2986 "\nactual: %v"+
2987 "\nexpected: %v",
2988 actual,
2989 expected,
2990 )
2991 }
2992}
2993
2994const staticLibAndroidBp = `
2995 cc_library {
2996 name: "lib1",
2997 }
2998 cc_library {
2999 name: "lib2",
3000 static_libs: ["lib1"],
3001 }
3002`
3003
3004func TestStaticLibDepExport(t *testing.T) {
3005 ctx := testCc(t, staticLibAndroidBp)
3006
3007 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003008 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003009 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003010 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003011
3012 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003013 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003014 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3015 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003016 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003017}
3018
Jiyong Parkd08b6972017-09-26 10:50:54 +09003019var compilerFlagsTestCases = []struct {
3020 in string
3021 out bool
3022}{
3023 {
3024 in: "a",
3025 out: false,
3026 },
3027 {
3028 in: "-a",
3029 out: true,
3030 },
3031 {
3032 in: "-Ipath/to/something",
3033 out: false,
3034 },
3035 {
3036 in: "-isystempath/to/something",
3037 out: false,
3038 },
3039 {
3040 in: "--coverage",
3041 out: false,
3042 },
3043 {
3044 in: "-include a/b",
3045 out: true,
3046 },
3047 {
3048 in: "-include a/b c/d",
3049 out: false,
3050 },
3051 {
3052 in: "-DMACRO",
3053 out: true,
3054 },
3055 {
3056 in: "-DMAC RO",
3057 out: false,
3058 },
3059 {
3060 in: "-a -b",
3061 out: false,
3062 },
3063 {
3064 in: "-DMACRO=definition",
3065 out: true,
3066 },
3067 {
3068 in: "-DMACRO=defi nition",
3069 out: true, // TODO(jiyong): this should be false
3070 },
3071 {
3072 in: "-DMACRO(x)=x + 1",
3073 out: true,
3074 },
3075 {
3076 in: "-DMACRO=\"defi nition\"",
3077 out: true,
3078 },
3079}
3080
3081type mockContext struct {
3082 BaseModuleContext
3083 result bool
3084}
3085
3086func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3087 // CheckBadCompilerFlags calls this function when the flag should be rejected
3088 ctx.result = false
3089}
3090
3091func TestCompilerFlags(t *testing.T) {
3092 for _, testCase := range compilerFlagsTestCases {
3093 ctx := &mockContext{result: true}
3094 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3095 if ctx.result != testCase.out {
3096 t.Errorf("incorrect output:")
3097 t.Errorf(" input: %#v", testCase.in)
3098 t.Errorf(" expected: %#v", testCase.out)
3099 t.Errorf(" got: %#v", ctx.result)
3100 }
3101 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003102}
Jiyong Park374510b2018-03-19 18:23:01 +09003103
Jiyong Park37b25202018-07-11 10:49:27 +09003104func TestRecovery(t *testing.T) {
3105 ctx := testCc(t, `
3106 cc_library_shared {
3107 name: "librecovery",
3108 recovery: true,
3109 }
3110 cc_library_shared {
3111 name: "librecovery32",
3112 recovery: true,
3113 compile_multilib:"32",
3114 }
Jiyong Park5baac542018-08-28 09:55:37 +09003115 cc_library_shared {
3116 name: "libHalInRecovery",
3117 recovery_available: true,
3118 vendor: true,
3119 }
Jiyong Park37b25202018-07-11 10:49:27 +09003120 `)
3121
3122 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003123 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003124 if len(variants) != 1 || !android.InList(arm64, variants) {
3125 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3126 }
3127
3128 variants = ctx.ModuleVariantsForTests("librecovery32")
3129 if android.InList(arm64, variants) {
3130 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3131 }
Jiyong Park5baac542018-08-28 09:55:37 +09003132
3133 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3134 if !recoveryModule.Platform() {
3135 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3136 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003137}
Jiyong Park5baac542018-08-28 09:55:37 +09003138
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003139func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
3140 bp := `
3141 cc_prebuilt_test_library_shared {
3142 name: "test_lib",
3143 relative_install_path: "foo/bar/baz",
3144 srcs: ["srcpath/dontusethispath/baz.so"],
3145 }
3146
3147 cc_test {
3148 name: "main_test",
3149 data_libs: ["test_lib"],
3150 gtest: false,
3151 }
3152 `
3153
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003154 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003155 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003156 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003157 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3158
3159 ctx := testCcWithConfig(t, config)
3160 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3161 testBinary := module.(*Module).linker.(*testBinary)
3162 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3163 if err != nil {
3164 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3165 }
3166 if len(outputFiles) != 1 {
3167 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3168 }
3169 if len(testBinary.dataPaths()) != 1 {
3170 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3171 }
3172
3173 outputPath := outputFiles[0].String()
3174
3175 if !strings.HasSuffix(outputPath, "/main_test") {
3176 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3177 }
Colin Crossaa255532020-07-03 13:18:24 -07003178 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003179 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3180 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3181 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3182 }
3183}
3184
Jiyong Park7ed9de32018-10-15 22:25:07 +09003185func TestVersionedStubs(t *testing.T) {
3186 ctx := testCc(t, `
3187 cc_library_shared {
3188 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003189 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003190 stubs: {
3191 symbol_file: "foo.map.txt",
3192 versions: ["1", "2", "3"],
3193 },
3194 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003195
Jiyong Park7ed9de32018-10-15 22:25:07 +09003196 cc_library_shared {
3197 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003198 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003199 shared_libs: ["libFoo#1"],
3200 }`)
3201
3202 variants := ctx.ModuleVariantsForTests("libFoo")
3203 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003204 "android_arm64_armv8-a_shared",
3205 "android_arm64_armv8-a_shared_1",
3206 "android_arm64_armv8-a_shared_2",
3207 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003208 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003209 "android_arm_armv7-a-neon_shared",
3210 "android_arm_armv7-a-neon_shared_1",
3211 "android_arm_armv7-a-neon_shared_2",
3212 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003213 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003214 }
3215 variantsMismatch := false
3216 if len(variants) != len(expectedVariants) {
3217 variantsMismatch = true
3218 } else {
3219 for _, v := range expectedVariants {
3220 if !inList(v, variants) {
3221 variantsMismatch = false
3222 }
3223 }
3224 }
3225 if variantsMismatch {
3226 t.Errorf("variants of libFoo expected:\n")
3227 for _, v := range expectedVariants {
3228 t.Errorf("%q\n", v)
3229 }
3230 t.Errorf(", but got:\n")
3231 for _, v := range variants {
3232 t.Errorf("%q\n", v)
3233 }
3234 }
3235
Colin Cross7113d202019-11-20 16:39:12 -08003236 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003237 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003238 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003239 if !strings.Contains(libFlags, libFoo1StubPath) {
3240 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3241 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003242
Colin Cross7113d202019-11-20 16:39:12 -08003243 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003244 cFlags := libBarCompileRule.Args["cFlags"]
3245 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3246 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3247 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3248 }
Jiyong Park37b25202018-07-11 10:49:27 +09003249}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003250
Jooyung Hanb04a4992020-03-13 18:57:35 +09003251func TestVersioningMacro(t *testing.T) {
3252 for _, tc := range []struct{ moduleName, expected string }{
3253 {"libc", "__LIBC_API__"},
3254 {"libfoo", "__LIBFOO_API__"},
3255 {"libfoo@1", "__LIBFOO_1_API__"},
3256 {"libfoo-v1", "__LIBFOO_V1_API__"},
3257 {"libfoo.v1", "__LIBFOO_V1_API__"},
3258 } {
3259 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3260 }
3261}
3262
Liz Kammer83cf81b2022-09-22 08:24:20 -04003263func pathsToBase(paths android.Paths) []string {
3264 var ret []string
3265 for _, p := range paths {
3266 ret = append(ret, p.Base())
3267 }
3268 return ret
3269}
3270
3271func TestStaticLibArchiveArgs(t *testing.T) {
3272 ctx := testCc(t, `
3273 cc_library_static {
3274 name: "foo",
3275 srcs: ["foo.c"],
3276 }
3277
3278 cc_library_static {
3279 name: "bar",
3280 srcs: ["bar.c"],
3281 }
3282
3283 cc_library_shared {
3284 name: "qux",
3285 srcs: ["qux.c"],
3286 }
3287
3288 cc_library_static {
3289 name: "baz",
3290 srcs: ["baz.c"],
3291 static_libs: ["foo"],
3292 shared_libs: ["qux"],
3293 whole_static_libs: ["bar"],
3294 }`)
3295
3296 variant := "android_arm64_armv8-a_static"
3297 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3298
3299 // For static libraries, the object files of a whole static dep are included in the archive
3300 // directly
3301 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3302 t.Errorf("Expected input objects %q, got %q", w, g)
3303 }
3304
3305 // non whole static dependencies are not linked into the archive
3306 if len(arRule.Implicits) > 0 {
3307 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3308 }
3309}
3310
3311func TestSharedLibLinkingArgs(t *testing.T) {
3312 ctx := testCc(t, `
3313 cc_library_static {
3314 name: "foo",
3315 srcs: ["foo.c"],
3316 }
3317
3318 cc_library_static {
3319 name: "bar",
3320 srcs: ["bar.c"],
3321 }
3322
3323 cc_library_shared {
3324 name: "qux",
3325 srcs: ["qux.c"],
3326 }
3327
3328 cc_library_shared {
3329 name: "baz",
3330 srcs: ["baz.c"],
3331 static_libs: ["foo"],
3332 shared_libs: ["qux"],
3333 whole_static_libs: ["bar"],
3334 }`)
3335
3336 variant := "android_arm64_armv8-a_shared"
3337 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3338 libFlags := linkRule.Args["libFlags"]
3339 // When dynamically linking, we expect static dependencies to be found on the command line
3340 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3341 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3342 }
3343 // When dynamically linking, we expect whole static dependencies to be found on the command line
3344 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3345 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3346 }
3347
3348 // When dynamically linking, we expect shared dependencies to be found on the command line
3349 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3350 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3351 }
3352
3353 // We should only have the objects from the shared library srcs, not the whole static dependencies
3354 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3355 t.Errorf("Expected input objects %q, got %q", w, g)
3356 }
3357}
3358
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003359func TestStaticExecutable(t *testing.T) {
3360 ctx := testCc(t, `
3361 cc_binary {
3362 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003363 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003364 static_executable: true,
3365 }`)
3366
Colin Cross7113d202019-11-20 16:39:12 -08003367 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003368 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3369 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003370 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003371 for _, lib := range systemStaticLibs {
3372 if !strings.Contains(libFlags, lib) {
3373 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3374 }
3375 }
3376 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3377 for _, lib := range systemSharedLibs {
3378 if strings.Contains(libFlags, lib) {
3379 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3380 }
3381 }
3382}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003383
3384func TestStaticDepsOrderWithStubs(t *testing.T) {
3385 ctx := testCc(t, `
3386 cc_binary {
3387 name: "mybin",
3388 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003389 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003390 static_executable: true,
3391 stl: "none",
3392 }
3393
3394 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003395 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003396 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003397 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003398 stl: "none",
3399 }
3400
3401 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003402 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003403 srcs: ["foo.c"],
3404 stl: "none",
3405 stubs: {
3406 versions: ["1"],
3407 },
3408 }`)
3409
Colin Cross0de8a1e2020-09-18 14:15:30 -07003410 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3411 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003412 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003413
3414 if !reflect.DeepEqual(actual, expected) {
3415 t.Errorf("staticDeps orderings were not propagated correctly"+
3416 "\nactual: %v"+
3417 "\nexpected: %v",
3418 actual,
3419 expected,
3420 )
3421 }
3422}
Jooyung Han38002912019-05-16 04:01:54 +09003423
Jooyung Hand48f3c32019-08-23 11:18:57 +09003424func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3425 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3426 cc_library {
3427 name: "libA",
3428 srcs: ["foo.c"],
3429 shared_libs: ["libB"],
3430 stl: "none",
3431 }
3432
3433 cc_library {
3434 name: "libB",
3435 srcs: ["foo.c"],
3436 enabled: false,
3437 stl: "none",
3438 }
3439 `)
3440}
3441
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003442func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3443 bp := `
3444 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003445 name: "test_afl_fuzz_target",
3446 srcs: ["foo.c"],
3447 host_supported: true,
3448 static_libs: [
3449 "afl_fuzz_static_lib",
3450 ],
3451 shared_libs: [
3452 "afl_fuzz_shared_lib",
3453 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003454 fuzzing_frameworks: {
3455 afl: true,
3456 libfuzzer: false,
3457 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003458 }
3459 cc_library {
3460 name: "afl_fuzz_static_lib",
3461 host_supported: true,
3462 srcs: ["static_file.c"],
3463 }
3464 cc_library {
3465 name: "libfuzzer_only_static_lib",
3466 host_supported: true,
3467 srcs: ["static_file.c"],
3468 }
3469 cc_library {
3470 name: "afl_fuzz_shared_lib",
3471 host_supported: true,
3472 srcs: ["shared_file.c"],
3473 static_libs: [
3474 "second_static_lib",
3475 ],
3476 }
3477 cc_library_headers {
3478 name: "libafl_headers",
3479 vendor_available: true,
3480 host_supported: true,
3481 export_include_dirs: [
3482 "include",
3483 "instrumentation",
3484 ],
3485 }
3486 cc_object {
3487 name: "afl-compiler-rt",
3488 vendor_available: true,
3489 host_supported: true,
3490 cflags: [
3491 "-fPIC",
3492 ],
3493 srcs: [
3494 "instrumentation/afl-compiler-rt.o.c",
3495 ],
3496 }
3497 cc_library {
3498 name: "second_static_lib",
3499 host_supported: true,
3500 srcs: ["second_file.c"],
3501 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003502 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003503 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003504 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003505 srcs: [
3506 "aflpp_driver.c",
3507 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003508 }`
3509
3510 testEnv := map[string]string{
3511 "FUZZ_FRAMEWORK": "AFL",
3512 }
3513
3514 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003515
3516 checkPcGuardFlag := func(
3517 modName string, variantName string, shouldHave bool) {
3518 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3519
3520 cFlags, ok := cc.Args["cFlags"]
3521 if !ok {
3522 t.Errorf("Could not find cFlags for module %s and variant %s",
3523 modName, variantName)
3524 }
3525
3526 if strings.Contains(
3527 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3528 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3529 "Test failed for module %s and variant %s",
3530 !shouldHave, shouldHave, modName, variantName)
3531 }
3532 }
3533
Cory Barkera1da26f2022-06-07 20:12:06 +00003534 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003535 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003536
3537 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003538 checkPcGuardFlag(moduleName, variant+"_static", false)
3539 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003540
3541 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003542 checkPcGuardFlag(moduleName, variant+"_static", false)
3543 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003544
3545 ctx.ModuleForTests("afl_fuzz_shared_lib",
3546 "android_arm64_armv8-a_shared").Rule("cc")
3547 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003548 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3549}
3550
3551func TestAFLFuzzTargetForDevice(t *testing.T) {
3552 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3553}
3554
3555func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
3556 if runtime.GOOS != "linux" {
3557 t.Skip("requires linux")
3558 }
3559
3560 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003561}
3562
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003563// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3564// correctly.
3565func TestFuzzTarget(t *testing.T) {
3566 ctx := testCc(t, `
3567 cc_fuzz {
3568 name: "fuzz_smoke_test",
3569 srcs: ["foo.c"],
3570 }`)
3571
Paul Duffin075c4172019-12-19 19:06:13 +00003572 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003573 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3574}
3575
Jiyong Park29074592019-07-07 16:27:47 +09003576func TestAidl(t *testing.T) {
3577}
3578
Jooyung Han38002912019-05-16 04:01:54 +09003579func assertString(t *testing.T, got, expected string) {
3580 t.Helper()
3581 if got != expected {
3582 t.Errorf("expected %q got %q", expected, got)
3583 }
3584}
3585
3586func assertArrayString(t *testing.T, got, expected []string) {
3587 t.Helper()
3588 if len(got) != len(expected) {
3589 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3590 return
3591 }
3592 for i := range got {
3593 if got[i] != expected[i] {
3594 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3595 i, expected[i], expected, got[i], got)
3596 return
3597 }
3598 }
3599}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003600
Jooyung Han0302a842019-10-30 18:43:49 +09003601func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3602 t.Helper()
3603 assertArrayString(t, android.SortedStringKeys(m), expected)
3604}
3605
Colin Crosse1bb5d02019-09-24 14:55:04 -07003606func TestDefaults(t *testing.T) {
3607 ctx := testCc(t, `
3608 cc_defaults {
3609 name: "defaults",
3610 srcs: ["foo.c"],
3611 static: {
3612 srcs: ["bar.c"],
3613 },
3614 shared: {
3615 srcs: ["baz.c"],
3616 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003617 bazel_module: {
3618 bp2build_available: true,
3619 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003620 }
3621
3622 cc_library_static {
3623 name: "libstatic",
3624 defaults: ["defaults"],
3625 }
3626
3627 cc_library_shared {
3628 name: "libshared",
3629 defaults: ["defaults"],
3630 }
3631
3632 cc_library {
3633 name: "libboth",
3634 defaults: ["defaults"],
3635 }
3636
3637 cc_binary {
3638 name: "binary",
3639 defaults: ["defaults"],
3640 }`)
3641
Colin Cross7113d202019-11-20 16:39:12 -08003642 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003643 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3644 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3645 }
Colin Cross7113d202019-11-20 16:39:12 -08003646 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003647 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3648 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3649 }
Colin Cross7113d202019-11-20 16:39:12 -08003650 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003651 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3652 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3653 }
3654
Colin Cross7113d202019-11-20 16:39:12 -08003655 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003656 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3657 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3658 }
Colin Cross7113d202019-11-20 16:39:12 -08003659 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003660 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3661 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3662 }
3663}
Colin Crosseabaedd2020-02-06 17:01:55 -08003664
3665func TestProductVariableDefaults(t *testing.T) {
3666 bp := `
3667 cc_defaults {
3668 name: "libfoo_defaults",
3669 srcs: ["foo.c"],
3670 cppflags: ["-DFOO"],
3671 product_variables: {
3672 debuggable: {
3673 cppflags: ["-DBAR"],
3674 },
3675 },
3676 }
3677
3678 cc_library {
3679 name: "libfoo",
3680 defaults: ["libfoo_defaults"],
3681 }
3682 `
3683
Paul Duffin8567f222021-03-23 00:02:06 +00003684 result := android.GroupFixturePreparers(
3685 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003686 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003687
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003688 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3689 variables.Debuggable = BoolPtr(true)
3690 }),
3691 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003692
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003693 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003694 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003695}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003696
3697func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3698 t.Parallel()
3699 bp := `
3700 cc_library_static {
3701 name: "libfoo",
3702 srcs: ["foo.c"],
3703 whole_static_libs: ["libbar"],
3704 }
3705
3706 cc_library_static {
3707 name: "libbar",
3708 whole_static_libs: ["libmissing"],
3709 }
3710 `
3711
Paul Duffin8567f222021-03-23 00:02:06 +00003712 result := android.GroupFixturePreparers(
3713 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003714 android.PrepareForTestWithAllowMissingDependencies,
3715 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003716
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003717 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003718 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003719
Paul Duffine84b1332021-03-12 11:59:43 +00003720 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003721
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003722 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003723 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003724}
Colin Crosse9fe2942020-11-10 18:12:15 -08003725
3726func TestInstallSharedLibs(t *testing.T) {
3727 bp := `
3728 cc_binary {
3729 name: "bin",
3730 host_supported: true,
3731 shared_libs: ["libshared"],
3732 runtime_libs: ["libruntime"],
3733 srcs: [":gen"],
3734 }
3735
3736 cc_library_shared {
3737 name: "libshared",
3738 host_supported: true,
3739 shared_libs: ["libtransitive"],
3740 }
3741
3742 cc_library_shared {
3743 name: "libtransitive",
3744 host_supported: true,
3745 }
3746
3747 cc_library_shared {
3748 name: "libruntime",
3749 host_supported: true,
3750 }
3751
3752 cc_binary_host {
3753 name: "tool",
3754 srcs: ["foo.cpp"],
3755 }
3756
3757 genrule {
3758 name: "gen",
3759 tools: ["tool"],
3760 out: ["gen.cpp"],
3761 cmd: "$(location tool) $(out)",
3762 }
3763 `
3764
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003765 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003766 ctx := testCcWithConfig(t, config)
3767
3768 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3769 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3770 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3771 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3772 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3773
3774 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3775 t.Errorf("expected host bin dependency %q, got %q", w, g)
3776 }
3777
3778 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3779 t.Errorf("expected host bin dependency %q, got %q", w, g)
3780 }
3781
3782 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3783 t.Errorf("expected host bin dependency %q, got %q", w, g)
3784 }
3785
3786 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3787 t.Errorf("expected host bin dependency %q, got %q", w, g)
3788 }
3789
3790 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3791 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3792 }
3793
3794 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3795 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3796 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3797 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3798
3799 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3800 t.Errorf("expected device bin dependency %q, got %q", w, g)
3801 }
3802
3803 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3804 t.Errorf("expected device bin dependency %q, got %q", w, g)
3805 }
3806
3807 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3808 t.Errorf("expected device bin dependency %q, got %q", w, g)
3809 }
3810
3811 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3812 t.Errorf("expected device bin dependency %q, got %q", w, g)
3813 }
3814
3815 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3816 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3817 }
3818
3819}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003820
3821func TestStubsLibReexportsHeaders(t *testing.T) {
3822 ctx := testCc(t, `
3823 cc_library_shared {
3824 name: "libclient",
3825 srcs: ["foo.c"],
3826 shared_libs: ["libfoo#1"],
3827 }
3828
3829 cc_library_shared {
3830 name: "libfoo",
3831 srcs: ["foo.c"],
3832 shared_libs: ["libbar"],
3833 export_shared_lib_headers: ["libbar"],
3834 stubs: {
3835 symbol_file: "foo.map.txt",
3836 versions: ["1", "2", "3"],
3837 },
3838 }
3839
3840 cc_library_shared {
3841 name: "libbar",
3842 export_include_dirs: ["include/libbar"],
3843 srcs: ["foo.c"],
3844 }`)
3845
3846 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3847
3848 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3849 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3850 }
3851}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003852
3853func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3854 ctx := testCc(t, `
3855 cc_library {
3856 name: "libfoo",
3857 srcs: ["a/Foo.aidl"],
3858 aidl: { flags: ["-Werror"], },
3859 }
3860 `)
3861
3862 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3863 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3864 aidlCommand := manifest.Commands[0].GetCommand()
3865 expectedAidlFlag := "-Werror"
3866 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3867 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3868 }
3869}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003870
Jooyung Han07f70c02021-11-06 07:08:45 +09003871func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
3872 for _, tc := range []struct {
3873 name string
3874 sdkVersion string
3875 variant string
3876 expected string
3877 }{
3878 {
3879 name: "default is current",
3880 sdkVersion: "",
3881 variant: "android_arm64_armv8-a_static",
3882 expected: "platform_apis",
3883 },
3884 {
3885 name: "use sdk_version",
3886 sdkVersion: `sdk_version: "29"`,
3887 variant: "android_arm64_armv8-a_static",
3888 expected: "platform_apis",
3889 },
3890 {
3891 name: "use sdk_version(sdk variant)",
3892 sdkVersion: `sdk_version: "29"`,
3893 variant: "android_arm64_armv8-a_sdk_static",
3894 expected: "29",
3895 },
3896 {
3897 name: "use min_sdk_version",
3898 sdkVersion: `min_sdk_version: "29"`,
3899 variant: "android_arm64_armv8-a_static",
3900 expected: "29",
3901 },
3902 } {
3903 t.Run(tc.name, func(t *testing.T) {
3904 ctx := testCc(t, `
3905 cc_library {
3906 name: "libfoo",
3907 stl: "none",
3908 srcs: ["a/Foo.aidl"],
3909 `+tc.sdkVersion+`
3910 }
3911 `)
3912 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
3913 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3914 aidlCommand := manifest.Commands[0].GetCommand()
3915 expectedAidlFlag := "--min_sdk_version=" + tc.expected
3916 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3917 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3918 }
3919 })
3920 }
3921}
3922
Jiyong Parka008fb02021-03-16 17:15:53 +09003923func TestMinSdkVersionInClangTriple(t *testing.T) {
3924 ctx := testCc(t, `
3925 cc_library_shared {
3926 name: "libfoo",
3927 srcs: ["foo.c"],
3928 min_sdk_version: "29",
3929 }`)
3930
3931 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3932 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3933}
3934
Vinh Tranf1924742022-06-24 16:40:11 -04003935func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
3936 bp := `
3937 cc_library_shared {
3938 name: "libfoo",
3939 srcs: ["foo.c"],
3940 min_sdk_version: "S",
3941 }
3942 `
3943 result := android.GroupFixturePreparers(
3944 prepareForCcTest,
3945 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3946 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
3947 }),
3948 ).RunTestWithBp(t, bp)
3949 ctx := result.TestContext
3950 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3951 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
3952}
3953
Paul Duffin3cb603e2021-02-19 13:57:10 +00003954func TestIncludeDirsExporting(t *testing.T) {
3955
3956 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3957 // embedded newline characters alone.
3958 trimIndentingSpaces := func(s string) string {
3959 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3960 }
3961
3962 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
3963 t.Helper()
3964 expected = trimIndentingSpaces(expected)
3965 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
3966 if expected != actual {
3967 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
3968 }
3969 }
3970
3971 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
3972
3973 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
3974 t.Helper()
3975 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
3976 name := module.Name()
3977
3978 for _, checker := range checkers {
3979 checker(t, name, exported)
3980 }
3981 }
3982
3983 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
3984 return func(t *testing.T, name string, exported FlagExporterInfo) {
3985 t.Helper()
3986 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
3987 }
3988 }
3989
3990 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
3991 return func(t *testing.T, name string, exported FlagExporterInfo) {
3992 t.Helper()
3993 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
3994 }
3995 }
3996
3997 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
3998 return func(t *testing.T, name string, exported FlagExporterInfo) {
3999 t.Helper()
4000 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4001 }
4002 }
4003
4004 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4005 return func(t *testing.T, name string, exported FlagExporterInfo) {
4006 t.Helper()
4007 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4008 }
4009 }
4010
4011 genRuleModules := `
4012 genrule {
4013 name: "genrule_foo",
4014 cmd: "generate-foo",
4015 out: [
4016 "generated_headers/foo/generated_header.h",
4017 ],
4018 export_include_dirs: [
4019 "generated_headers",
4020 ],
4021 }
4022
4023 genrule {
4024 name: "genrule_bar",
4025 cmd: "generate-bar",
4026 out: [
4027 "generated_headers/bar/generated_header.h",
4028 ],
4029 export_include_dirs: [
4030 "generated_headers",
4031 ],
4032 }
4033 `
4034
4035 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4036 ctx := testCc(t, genRuleModules+`
4037 cc_library {
4038 name: "libfoo",
4039 srcs: ["foo.c"],
4040 export_include_dirs: ["foo/standard"],
4041 export_system_include_dirs: ["foo/system"],
4042 generated_headers: ["genrule_foo"],
4043 export_generated_headers: ["genrule_foo"],
4044 }
4045
4046 cc_library {
4047 name: "libbar",
4048 srcs: ["bar.c"],
4049 shared_libs: ["libfoo"],
4050 export_include_dirs: ["bar/standard"],
4051 export_system_include_dirs: ["bar/system"],
4052 generated_headers: ["genrule_bar"],
4053 export_generated_headers: ["genrule_bar"],
4054 }
4055 `)
4056 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4057 checkIncludeDirs(t, ctx, foo,
4058 expectedIncludeDirs(`
4059 foo/standard
4060 .intermediates/genrule_foo/gen/generated_headers
4061 `),
4062 expectedSystemIncludeDirs(`foo/system`),
4063 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4064 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4065 )
4066
4067 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4068 checkIncludeDirs(t, ctx, bar,
4069 expectedIncludeDirs(`
4070 bar/standard
4071 .intermediates/genrule_bar/gen/generated_headers
4072 `),
4073 expectedSystemIncludeDirs(`bar/system`),
4074 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4075 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4076 )
4077 })
4078
4079 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4080 ctx := testCc(t, genRuleModules+`
4081 cc_library {
4082 name: "libfoo",
4083 srcs: ["foo.c"],
4084 export_include_dirs: ["foo/standard"],
4085 export_system_include_dirs: ["foo/system"],
4086 generated_headers: ["genrule_foo"],
4087 export_generated_headers: ["genrule_foo"],
4088 }
4089
4090 cc_library {
4091 name: "libbar",
4092 srcs: ["bar.c"],
4093 whole_static_libs: ["libfoo"],
4094 export_include_dirs: ["bar/standard"],
4095 export_system_include_dirs: ["bar/system"],
4096 generated_headers: ["genrule_bar"],
4097 export_generated_headers: ["genrule_bar"],
4098 }
4099 `)
4100 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4101 checkIncludeDirs(t, ctx, foo,
4102 expectedIncludeDirs(`
4103 foo/standard
4104 .intermediates/genrule_foo/gen/generated_headers
4105 `),
4106 expectedSystemIncludeDirs(`foo/system`),
4107 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4108 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4109 )
4110
4111 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4112 checkIncludeDirs(t, ctx, bar,
4113 expectedIncludeDirs(`
4114 bar/standard
4115 foo/standard
4116 .intermediates/genrule_foo/gen/generated_headers
4117 .intermediates/genrule_bar/gen/generated_headers
4118 `),
4119 expectedSystemIncludeDirs(`
4120 bar/system
4121 foo/system
4122 `),
4123 expectedGeneratedHeaders(`
4124 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4125 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4126 `),
4127 expectedOrderOnlyDeps(`
4128 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4129 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4130 `),
4131 )
4132 })
4133
Paul Duffin3cb603e2021-02-19 13:57:10 +00004134 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4135 ctx := testCc(t, genRuleModules+`
4136 cc_library_shared {
4137 name: "libfoo",
4138 srcs: [
4139 "foo.c",
4140 "b.aidl",
4141 "a.proto",
4142 ],
4143 aidl: {
4144 export_aidl_headers: true,
4145 }
4146 }
4147 `)
4148 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4149 checkIncludeDirs(t, ctx, foo,
4150 expectedIncludeDirs(`
4151 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4152 `),
4153 expectedSystemIncludeDirs(``),
4154 expectedGeneratedHeaders(`
4155 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4156 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4157 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004158 `),
4159 expectedOrderOnlyDeps(`
4160 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4161 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4162 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004163 `),
4164 )
4165 })
4166
Paul Duffin3cb603e2021-02-19 13:57:10 +00004167 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4168 ctx := testCc(t, genRuleModules+`
4169 cc_library_shared {
4170 name: "libfoo",
4171 srcs: [
4172 "foo.c",
4173 "b.aidl",
4174 "a.proto",
4175 ],
4176 proto: {
4177 export_proto_headers: true,
4178 }
4179 }
4180 `)
4181 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4182 checkIncludeDirs(t, ctx, foo,
4183 expectedIncludeDirs(`
4184 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4185 `),
4186 expectedSystemIncludeDirs(``),
4187 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004188 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4189 `),
4190 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004191 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4192 `),
4193 )
4194 })
4195
Paul Duffin33056e82021-02-19 13:49:08 +00004196 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004197 ctx := testCc(t, genRuleModules+`
4198 cc_library_shared {
4199 name: "libfoo",
4200 srcs: [
4201 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004202 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004203 "b.aidl",
4204 "a.proto",
4205 ],
4206 }
4207 `)
4208 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4209 checkIncludeDirs(t, ctx, foo,
4210 expectedIncludeDirs(`
4211 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4212 `),
4213 expectedSystemIncludeDirs(``),
4214 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004215 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004216 `),
4217 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004218 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4219 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004220 `),
4221 )
4222 })
4223}
Colin Crossae628182021-06-14 16:52:28 -07004224
4225func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer08572c62021-09-30 10:11:04 -04004226 baseExpectedFlags := []string{
4227 "${config.ArmThumbCflags}",
4228 "${config.ArmCflags}",
4229 "${config.CommonGlobalCflags}",
4230 "${config.DeviceGlobalCflags}",
4231 "${config.ExternalCflags}",
4232 "${config.ArmToolchainCflags}",
4233 "${config.ArmArmv7ANeonCflags}",
4234 "${config.ArmGenericCflags}",
4235 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004236 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004237 }
4238
4239 expectedIncludes := []string{
4240 "external/foo/android_arm_export_include_dirs",
4241 "external/foo/lib32_export_include_dirs",
4242 "external/foo/arm_export_include_dirs",
4243 "external/foo/android_export_include_dirs",
4244 "external/foo/linux_export_include_dirs",
4245 "external/foo/export_include_dirs",
4246 "external/foo/android_arm_local_include_dirs",
4247 "external/foo/lib32_local_include_dirs",
4248 "external/foo/arm_local_include_dirs",
4249 "external/foo/android_local_include_dirs",
4250 "external/foo/linux_local_include_dirs",
4251 "external/foo/local_include_dirs",
4252 "external/foo",
4253 "external/foo/libheader1",
4254 "external/foo/libheader2",
4255 "external/foo/libwhole1",
4256 "external/foo/libwhole2",
4257 "external/foo/libstatic1",
4258 "external/foo/libstatic2",
4259 "external/foo/libshared1",
4260 "external/foo/libshared2",
4261 "external/foo/liblinux",
4262 "external/foo/libandroid",
4263 "external/foo/libarm",
4264 "external/foo/lib32",
4265 "external/foo/libandroid_arm",
4266 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004267 }
4268
4269 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4270 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4271
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004272 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004273 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004274 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004275
4276 lastIncludes := []string{
4277 "out/soong/ndk/sysroot/usr/include",
4278 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4279 }
4280
4281 combineSlices := func(slices ...[]string) []string {
4282 var ret []string
4283 for _, s := range slices {
4284 ret = append(ret, s...)
4285 }
4286 return ret
4287 }
4288
4289 testCases := []struct {
4290 name string
4291 src string
4292 expected []string
4293 }{
4294 {
4295 name: "c",
4296 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004297 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004298 },
4299 {
4300 name: "cc",
4301 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004302 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004303 },
4304 {
4305 name: "assemble",
4306 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004307 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004308 },
4309 }
4310
4311 for _, tc := range testCases {
4312 t.Run(tc.name, func(t *testing.T) {
4313 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004314 cc_library {
4315 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004316 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004317 cflags: ["-std=candcpp"],
4318 conlyflags: ["-std=conly"],
4319 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004320 local_include_dirs: ["local_include_dirs"],
4321 export_include_dirs: ["export_include_dirs"],
4322 export_system_include_dirs: ["export_system_include_dirs"],
4323 static_libs: ["libstatic1", "libstatic2"],
4324 whole_static_libs: ["libwhole1", "libwhole2"],
4325 shared_libs: ["libshared1", "libshared2"],
4326 header_libs: ["libheader1", "libheader2"],
4327 target: {
4328 android: {
4329 shared_libs: ["libandroid"],
4330 local_include_dirs: ["android_local_include_dirs"],
4331 export_include_dirs: ["android_export_include_dirs"],
4332 },
4333 android_arm: {
4334 shared_libs: ["libandroid_arm"],
4335 local_include_dirs: ["android_arm_local_include_dirs"],
4336 export_include_dirs: ["android_arm_export_include_dirs"],
4337 },
4338 linux: {
4339 shared_libs: ["liblinux"],
4340 local_include_dirs: ["linux_local_include_dirs"],
4341 export_include_dirs: ["linux_export_include_dirs"],
4342 },
4343 },
4344 multilib: {
4345 lib32: {
4346 shared_libs: ["lib32"],
4347 local_include_dirs: ["lib32_local_include_dirs"],
4348 export_include_dirs: ["lib32_export_include_dirs"],
4349 },
4350 },
4351 arch: {
4352 arm: {
4353 shared_libs: ["libarm"],
4354 local_include_dirs: ["arm_local_include_dirs"],
4355 export_include_dirs: ["arm_export_include_dirs"],
4356 },
4357 },
4358 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004359 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004360 }
4361
4362 cc_library_headers {
4363 name: "libheader1",
4364 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004365 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004366 stl: "none",
4367 }
4368
4369 cc_library_headers {
4370 name: "libheader2",
4371 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004372 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004373 stl: "none",
4374 }
Liz Kammer08572c62021-09-30 10:11:04 -04004375 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004376
Liz Kammer08572c62021-09-30 10:11:04 -04004377 libs := []string{
4378 "libstatic1",
4379 "libstatic2",
4380 "libwhole1",
4381 "libwhole2",
4382 "libshared1",
4383 "libshared2",
4384 "libandroid",
4385 "libandroid_arm",
4386 "liblinux",
4387 "lib32",
4388 "libarm",
4389 }
Colin Crossae628182021-06-14 16:52:28 -07004390
Liz Kammer08572c62021-09-30 10:11:04 -04004391 for _, lib := range libs {
4392 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004393 cc_library {
4394 name: "%s",
4395 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004396 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004397 stl: "none",
4398 }
4399 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004400 }
4401
4402 ctx := android.GroupFixturePreparers(
4403 PrepareForIntegrationTestWithCc,
4404 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4405 ).RunTest(t)
4406 // Use the arm variant instead of the arm64 variant so that it gets headers from
4407 // ndk_libandroid_support to test LateStaticLibs.
4408 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4409
4410 var includes []string
4411 flags := strings.Split(cflags, " ")
4412 for _, flag := range flags {
4413 if strings.HasPrefix(flag, "-I") {
4414 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4415 } else if flag == "-isystem" {
4416 // skip isystem, include next
4417 } else if len(flag) > 0 {
4418 includes = append(includes, flag)
4419 }
4420 }
4421
4422 android.AssertArrayString(t, "includes", tc.expected, includes)
4423 })
Colin Crossae628182021-06-14 16:52:28 -07004424 }
4425
Colin Crossae628182021-06-14 16:52:28 -07004426}
Alixb5f6d9e2022-04-20 23:00:58 +00004427
4428func TestCcBuildBrokenClangProperty(t *testing.T) {
4429 tests := []struct {
4430 name string
4431 clang bool
4432 BuildBrokenClangProperty bool
4433 err string
4434 }{
4435 {
4436 name: "error when clang is set to false",
4437 clang: false,
4438 err: "is no longer supported",
4439 },
4440 {
4441 name: "error when clang is set to true",
4442 clang: true,
4443 err: "property is deprecated, see Changes.md",
4444 },
4445 {
4446 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4447 clang: true,
4448 BuildBrokenClangProperty: true,
4449 },
4450 }
4451
4452 for _, test := range tests {
4453 t.Run(test.name, func(t *testing.T) {
4454 bp := fmt.Sprintf(`
4455 cc_library {
4456 name: "foo",
4457 clang: %t,
4458 }`, test.clang)
4459
4460 if test.err == "" {
4461 android.GroupFixturePreparers(
4462 prepareForCcTest,
4463 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4464 if test.BuildBrokenClangProperty {
4465 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4466 }
4467 }),
4468 ).RunTestWithBp(t, bp)
4469 } else {
4470 prepareForCcTest.
4471 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4472 RunTestWithBp(t, bp)
4473 }
4474 })
4475 }
4476}
Alix Espinoef47e542022-09-14 19:10:51 +00004477
4478func TestCcBuildBrokenClangAsFlags(t *testing.T) {
4479 tests := []struct {
4480 name string
4481 clangAsFlags []string
4482 BuildBrokenClangAsFlags bool
4483 err string
4484 }{
4485 {
4486 name: "error when clang_asflags is set",
4487 clangAsFlags: []string{"-a", "-b"},
4488 err: "clang_asflags: property is deprecated",
4489 },
4490 {
4491 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4492 clangAsFlags: []string{"-a", "-b"},
4493 BuildBrokenClangAsFlags: true,
4494 },
4495 }
4496
4497 for _, test := range tests {
4498 t.Run(test.name, func(t *testing.T) {
4499 bp := fmt.Sprintf(`
4500 cc_library {
4501 name: "foo",
4502 clang_asflags: %s,
4503 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4504
4505 if test.err == "" {
4506 android.GroupFixturePreparers(
4507 prepareForCcTest,
4508 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4509 if test.BuildBrokenClangAsFlags {
4510 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4511 }
4512 }),
4513 ).RunTestWithBp(t, bp)
4514 } else {
4515 prepareForCcTest.
4516 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4517 RunTestWithBp(t, bp)
4518 }
4519 })
4520 }
4521}
4522
4523func TestCcBuildBrokenClangCFlags(t *testing.T) {
4524 tests := []struct {
4525 name string
4526 clangCFlags []string
4527 BuildBrokenClangCFlags bool
4528 err string
4529 }{
4530 {
4531 name: "error when clang_cflags is set",
4532 clangCFlags: []string{"-a", "-b"},
4533 err: "clang_cflags: property is deprecated",
4534 },
4535 {
4536 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4537 clangCFlags: []string{"-a", "-b"},
4538 BuildBrokenClangCFlags: true,
4539 },
4540 }
4541
4542 for _, test := range tests {
4543 t.Run(test.name, func(t *testing.T) {
4544 bp := fmt.Sprintf(`
4545 cc_library {
4546 name: "foo",
4547 clang_cflags: %s,
4548 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4549
4550 if test.err == "" {
4551 android.GroupFixturePreparers(
4552 prepareForCcTest,
4553 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4554 if test.BuildBrokenClangCFlags {
4555 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4556 }
4557 }),
4558 ).RunTestWithBp(t, bp)
4559 } else {
4560 prepareForCcTest.
4561 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4562 RunTestWithBp(t, bp)
4563 }
4564 })
4565 }
4566}