blob: 36174d639f4fd8fd7eef13cddd309f4673de3ced [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
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003263func TestStaticExecutable(t *testing.T) {
3264 ctx := testCc(t, `
3265 cc_binary {
3266 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003267 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003268 static_executable: true,
3269 }`)
3270
Colin Cross7113d202019-11-20 16:39:12 -08003271 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003272 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3273 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003274 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003275 for _, lib := range systemStaticLibs {
3276 if !strings.Contains(libFlags, lib) {
3277 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3278 }
3279 }
3280 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3281 for _, lib := range systemSharedLibs {
3282 if strings.Contains(libFlags, lib) {
3283 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3284 }
3285 }
3286}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003287
3288func TestStaticDepsOrderWithStubs(t *testing.T) {
3289 ctx := testCc(t, `
3290 cc_binary {
3291 name: "mybin",
3292 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003293 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003294 static_executable: true,
3295 stl: "none",
3296 }
3297
3298 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003299 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003300 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003301 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003302 stl: "none",
3303 }
3304
3305 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003306 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003307 srcs: ["foo.c"],
3308 stl: "none",
3309 stubs: {
3310 versions: ["1"],
3311 },
3312 }`)
3313
Colin Cross0de8a1e2020-09-18 14:15:30 -07003314 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3315 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003316 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003317
3318 if !reflect.DeepEqual(actual, expected) {
3319 t.Errorf("staticDeps orderings were not propagated correctly"+
3320 "\nactual: %v"+
3321 "\nexpected: %v",
3322 actual,
3323 expected,
3324 )
3325 }
3326}
Jooyung Han38002912019-05-16 04:01:54 +09003327
Jooyung Hand48f3c32019-08-23 11:18:57 +09003328func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
3329 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3330 cc_library {
3331 name: "libA",
3332 srcs: ["foo.c"],
3333 shared_libs: ["libB"],
3334 stl: "none",
3335 }
3336
3337 cc_library {
3338 name: "libB",
3339 srcs: ["foo.c"],
3340 enabled: false,
3341 stl: "none",
3342 }
3343 `)
3344}
3345
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003346func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3347 bp := `
3348 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003349 name: "test_afl_fuzz_target",
3350 srcs: ["foo.c"],
3351 host_supported: true,
3352 static_libs: [
3353 "afl_fuzz_static_lib",
3354 ],
3355 shared_libs: [
3356 "afl_fuzz_shared_lib",
3357 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003358 fuzzing_frameworks: {
3359 afl: true,
3360 libfuzzer: false,
3361 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003362 }
3363 cc_library {
3364 name: "afl_fuzz_static_lib",
3365 host_supported: true,
3366 srcs: ["static_file.c"],
3367 }
3368 cc_library {
3369 name: "libfuzzer_only_static_lib",
3370 host_supported: true,
3371 srcs: ["static_file.c"],
3372 }
3373 cc_library {
3374 name: "afl_fuzz_shared_lib",
3375 host_supported: true,
3376 srcs: ["shared_file.c"],
3377 static_libs: [
3378 "second_static_lib",
3379 ],
3380 }
3381 cc_library_headers {
3382 name: "libafl_headers",
3383 vendor_available: true,
3384 host_supported: true,
3385 export_include_dirs: [
3386 "include",
3387 "instrumentation",
3388 ],
3389 }
3390 cc_object {
3391 name: "afl-compiler-rt",
3392 vendor_available: true,
3393 host_supported: true,
3394 cflags: [
3395 "-fPIC",
3396 ],
3397 srcs: [
3398 "instrumentation/afl-compiler-rt.o.c",
3399 ],
3400 }
3401 cc_library {
3402 name: "second_static_lib",
3403 host_supported: true,
3404 srcs: ["second_file.c"],
3405 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003406 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003407 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003408 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003409 srcs: [
3410 "aflpp_driver.c",
3411 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003412 }`
3413
3414 testEnv := map[string]string{
3415 "FUZZ_FRAMEWORK": "AFL",
3416 }
3417
3418 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003419
3420 checkPcGuardFlag := func(
3421 modName string, variantName string, shouldHave bool) {
3422 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3423
3424 cFlags, ok := cc.Args["cFlags"]
3425 if !ok {
3426 t.Errorf("Could not find cFlags for module %s and variant %s",
3427 modName, variantName)
3428 }
3429
3430 if strings.Contains(
3431 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3432 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3433 "Test failed for module %s and variant %s",
3434 !shouldHave, shouldHave, modName, variantName)
3435 }
3436 }
3437
Cory Barkera1da26f2022-06-07 20:12:06 +00003438 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003439 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003440
3441 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003442 checkPcGuardFlag(moduleName, variant+"_static", false)
3443 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003444
3445 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003446 checkPcGuardFlag(moduleName, variant+"_static", false)
3447 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003448
3449 ctx.ModuleForTests("afl_fuzz_shared_lib",
3450 "android_arm64_armv8-a_shared").Rule("cc")
3451 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003452 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3453}
3454
3455func TestAFLFuzzTargetForDevice(t *testing.T) {
3456 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3457}
3458
3459func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
3460 if runtime.GOOS != "linux" {
3461 t.Skip("requires linux")
3462 }
3463
3464 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003465}
3466
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003467// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3468// correctly.
3469func TestFuzzTarget(t *testing.T) {
3470 ctx := testCc(t, `
3471 cc_fuzz {
3472 name: "fuzz_smoke_test",
3473 srcs: ["foo.c"],
3474 }`)
3475
Paul Duffin075c4172019-12-19 19:06:13 +00003476 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003477 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3478}
3479
Jiyong Park29074592019-07-07 16:27:47 +09003480func TestAidl(t *testing.T) {
3481}
3482
Jooyung Han38002912019-05-16 04:01:54 +09003483func assertString(t *testing.T, got, expected string) {
3484 t.Helper()
3485 if got != expected {
3486 t.Errorf("expected %q got %q", expected, got)
3487 }
3488}
3489
3490func assertArrayString(t *testing.T, got, expected []string) {
3491 t.Helper()
3492 if len(got) != len(expected) {
3493 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3494 return
3495 }
3496 for i := range got {
3497 if got[i] != expected[i] {
3498 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3499 i, expected[i], expected, got[i], got)
3500 return
3501 }
3502 }
3503}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003504
Jooyung Han0302a842019-10-30 18:43:49 +09003505func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3506 t.Helper()
3507 assertArrayString(t, android.SortedStringKeys(m), expected)
3508}
3509
Colin Crosse1bb5d02019-09-24 14:55:04 -07003510func TestDefaults(t *testing.T) {
3511 ctx := testCc(t, `
3512 cc_defaults {
3513 name: "defaults",
3514 srcs: ["foo.c"],
3515 static: {
3516 srcs: ["bar.c"],
3517 },
3518 shared: {
3519 srcs: ["baz.c"],
3520 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003521 bazel_module: {
3522 bp2build_available: true,
3523 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003524 }
3525
3526 cc_library_static {
3527 name: "libstatic",
3528 defaults: ["defaults"],
3529 }
3530
3531 cc_library_shared {
3532 name: "libshared",
3533 defaults: ["defaults"],
3534 }
3535
3536 cc_library {
3537 name: "libboth",
3538 defaults: ["defaults"],
3539 }
3540
3541 cc_binary {
3542 name: "binary",
3543 defaults: ["defaults"],
3544 }`)
3545
3546 pathsToBase := func(paths android.Paths) []string {
3547 var ret []string
3548 for _, p := range paths {
3549 ret = append(ret, p.Base())
3550 }
3551 return ret
3552 }
3553
Colin Cross7113d202019-11-20 16:39:12 -08003554 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003555 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3556 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3557 }
Colin Cross7113d202019-11-20 16:39:12 -08003558 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003559 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3560 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3561 }
Colin Cross7113d202019-11-20 16:39:12 -08003562 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003563 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3564 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3565 }
3566
Colin Cross7113d202019-11-20 16:39:12 -08003567 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003568 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3569 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3570 }
Colin Cross7113d202019-11-20 16:39:12 -08003571 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003572 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3573 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3574 }
3575}
Colin Crosseabaedd2020-02-06 17:01:55 -08003576
3577func TestProductVariableDefaults(t *testing.T) {
3578 bp := `
3579 cc_defaults {
3580 name: "libfoo_defaults",
3581 srcs: ["foo.c"],
3582 cppflags: ["-DFOO"],
3583 product_variables: {
3584 debuggable: {
3585 cppflags: ["-DBAR"],
3586 },
3587 },
3588 }
3589
3590 cc_library {
3591 name: "libfoo",
3592 defaults: ["libfoo_defaults"],
3593 }
3594 `
3595
Paul Duffin8567f222021-03-23 00:02:06 +00003596 result := android.GroupFixturePreparers(
3597 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003598 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003599
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003600 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3601 variables.Debuggable = BoolPtr(true)
3602 }),
3603 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003604
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003605 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003606 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003607}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003608
3609func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3610 t.Parallel()
3611 bp := `
3612 cc_library_static {
3613 name: "libfoo",
3614 srcs: ["foo.c"],
3615 whole_static_libs: ["libbar"],
3616 }
3617
3618 cc_library_static {
3619 name: "libbar",
3620 whole_static_libs: ["libmissing"],
3621 }
3622 `
3623
Paul Duffin8567f222021-03-23 00:02:06 +00003624 result := android.GroupFixturePreparers(
3625 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003626 android.PrepareForTestWithAllowMissingDependencies,
3627 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003628
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003629 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003630 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003631
Paul Duffine84b1332021-03-12 11:59:43 +00003632 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003633
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003634 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003635 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003636}
Colin Crosse9fe2942020-11-10 18:12:15 -08003637
3638func TestInstallSharedLibs(t *testing.T) {
3639 bp := `
3640 cc_binary {
3641 name: "bin",
3642 host_supported: true,
3643 shared_libs: ["libshared"],
3644 runtime_libs: ["libruntime"],
3645 srcs: [":gen"],
3646 }
3647
3648 cc_library_shared {
3649 name: "libshared",
3650 host_supported: true,
3651 shared_libs: ["libtransitive"],
3652 }
3653
3654 cc_library_shared {
3655 name: "libtransitive",
3656 host_supported: true,
3657 }
3658
3659 cc_library_shared {
3660 name: "libruntime",
3661 host_supported: true,
3662 }
3663
3664 cc_binary_host {
3665 name: "tool",
3666 srcs: ["foo.cpp"],
3667 }
3668
3669 genrule {
3670 name: "gen",
3671 tools: ["tool"],
3672 out: ["gen.cpp"],
3673 cmd: "$(location tool) $(out)",
3674 }
3675 `
3676
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003677 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003678 ctx := testCcWithConfig(t, config)
3679
3680 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3681 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3682 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3683 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3684 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3685
3686 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3687 t.Errorf("expected host bin dependency %q, got %q", w, g)
3688 }
3689
3690 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3691 t.Errorf("expected host bin dependency %q, got %q", w, g)
3692 }
3693
3694 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3695 t.Errorf("expected host bin dependency %q, got %q", w, g)
3696 }
3697
3698 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3699 t.Errorf("expected host bin dependency %q, got %q", w, g)
3700 }
3701
3702 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3703 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3704 }
3705
3706 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3707 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3708 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3709 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3710
3711 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3712 t.Errorf("expected device bin dependency %q, got %q", w, g)
3713 }
3714
3715 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3716 t.Errorf("expected device bin dependency %q, got %q", w, g)
3717 }
3718
3719 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3720 t.Errorf("expected device bin dependency %q, got %q", w, g)
3721 }
3722
3723 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3724 t.Errorf("expected device bin dependency %q, got %q", w, g)
3725 }
3726
3727 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3728 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3729 }
3730
3731}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003732
3733func TestStubsLibReexportsHeaders(t *testing.T) {
3734 ctx := testCc(t, `
3735 cc_library_shared {
3736 name: "libclient",
3737 srcs: ["foo.c"],
3738 shared_libs: ["libfoo#1"],
3739 }
3740
3741 cc_library_shared {
3742 name: "libfoo",
3743 srcs: ["foo.c"],
3744 shared_libs: ["libbar"],
3745 export_shared_lib_headers: ["libbar"],
3746 stubs: {
3747 symbol_file: "foo.map.txt",
3748 versions: ["1", "2", "3"],
3749 },
3750 }
3751
3752 cc_library_shared {
3753 name: "libbar",
3754 export_include_dirs: ["include/libbar"],
3755 srcs: ["foo.c"],
3756 }`)
3757
3758 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3759
3760 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3761 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3762 }
3763}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003764
3765func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
3766 ctx := testCc(t, `
3767 cc_library {
3768 name: "libfoo",
3769 srcs: ["a/Foo.aidl"],
3770 aidl: { flags: ["-Werror"], },
3771 }
3772 `)
3773
3774 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3775 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3776 aidlCommand := manifest.Commands[0].GetCommand()
3777 expectedAidlFlag := "-Werror"
3778 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3779 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3780 }
3781}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003782
Jooyung Han07f70c02021-11-06 07:08:45 +09003783func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
3784 for _, tc := range []struct {
3785 name string
3786 sdkVersion string
3787 variant string
3788 expected string
3789 }{
3790 {
3791 name: "default is current",
3792 sdkVersion: "",
3793 variant: "android_arm64_armv8-a_static",
3794 expected: "platform_apis",
3795 },
3796 {
3797 name: "use sdk_version",
3798 sdkVersion: `sdk_version: "29"`,
3799 variant: "android_arm64_armv8-a_static",
3800 expected: "platform_apis",
3801 },
3802 {
3803 name: "use sdk_version(sdk variant)",
3804 sdkVersion: `sdk_version: "29"`,
3805 variant: "android_arm64_armv8-a_sdk_static",
3806 expected: "29",
3807 },
3808 {
3809 name: "use min_sdk_version",
3810 sdkVersion: `min_sdk_version: "29"`,
3811 variant: "android_arm64_armv8-a_static",
3812 expected: "29",
3813 },
3814 } {
3815 t.Run(tc.name, func(t *testing.T) {
3816 ctx := testCc(t, `
3817 cc_library {
3818 name: "libfoo",
3819 stl: "none",
3820 srcs: ["a/Foo.aidl"],
3821 `+tc.sdkVersion+`
3822 }
3823 `)
3824 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
3825 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3826 aidlCommand := manifest.Commands[0].GetCommand()
3827 expectedAidlFlag := "--min_sdk_version=" + tc.expected
3828 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3829 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3830 }
3831 })
3832 }
3833}
3834
Jiyong Parka008fb02021-03-16 17:15:53 +09003835func TestMinSdkVersionInClangTriple(t *testing.T) {
3836 ctx := testCc(t, `
3837 cc_library_shared {
3838 name: "libfoo",
3839 srcs: ["foo.c"],
3840 min_sdk_version: "29",
3841 }`)
3842
3843 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3844 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3845}
3846
Vinh Tranf1924742022-06-24 16:40:11 -04003847func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
3848 bp := `
3849 cc_library_shared {
3850 name: "libfoo",
3851 srcs: ["foo.c"],
3852 min_sdk_version: "S",
3853 }
3854 `
3855 result := android.GroupFixturePreparers(
3856 prepareForCcTest,
3857 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3858 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
3859 }),
3860 ).RunTestWithBp(t, bp)
3861 ctx := result.TestContext
3862 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3863 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
3864}
3865
Paul Duffin3cb603e2021-02-19 13:57:10 +00003866func TestIncludeDirsExporting(t *testing.T) {
3867
3868 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
3869 // embedded newline characters alone.
3870 trimIndentingSpaces := func(s string) string {
3871 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
3872 }
3873
3874 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
3875 t.Helper()
3876 expected = trimIndentingSpaces(expected)
3877 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
3878 if expected != actual {
3879 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
3880 }
3881 }
3882
3883 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
3884
3885 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
3886 t.Helper()
3887 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
3888 name := module.Name()
3889
3890 for _, checker := range checkers {
3891 checker(t, name, exported)
3892 }
3893 }
3894
3895 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
3896 return func(t *testing.T, name string, exported FlagExporterInfo) {
3897 t.Helper()
3898 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
3899 }
3900 }
3901
3902 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
3903 return func(t *testing.T, name string, exported FlagExporterInfo) {
3904 t.Helper()
3905 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
3906 }
3907 }
3908
3909 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
3910 return func(t *testing.T, name string, exported FlagExporterInfo) {
3911 t.Helper()
3912 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
3913 }
3914 }
3915
3916 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
3917 return func(t *testing.T, name string, exported FlagExporterInfo) {
3918 t.Helper()
3919 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
3920 }
3921 }
3922
3923 genRuleModules := `
3924 genrule {
3925 name: "genrule_foo",
3926 cmd: "generate-foo",
3927 out: [
3928 "generated_headers/foo/generated_header.h",
3929 ],
3930 export_include_dirs: [
3931 "generated_headers",
3932 ],
3933 }
3934
3935 genrule {
3936 name: "genrule_bar",
3937 cmd: "generate-bar",
3938 out: [
3939 "generated_headers/bar/generated_header.h",
3940 ],
3941 export_include_dirs: [
3942 "generated_headers",
3943 ],
3944 }
3945 `
3946
3947 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
3948 ctx := testCc(t, genRuleModules+`
3949 cc_library {
3950 name: "libfoo",
3951 srcs: ["foo.c"],
3952 export_include_dirs: ["foo/standard"],
3953 export_system_include_dirs: ["foo/system"],
3954 generated_headers: ["genrule_foo"],
3955 export_generated_headers: ["genrule_foo"],
3956 }
3957
3958 cc_library {
3959 name: "libbar",
3960 srcs: ["bar.c"],
3961 shared_libs: ["libfoo"],
3962 export_include_dirs: ["bar/standard"],
3963 export_system_include_dirs: ["bar/system"],
3964 generated_headers: ["genrule_bar"],
3965 export_generated_headers: ["genrule_bar"],
3966 }
3967 `)
3968 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
3969 checkIncludeDirs(t, ctx, foo,
3970 expectedIncludeDirs(`
3971 foo/standard
3972 .intermediates/genrule_foo/gen/generated_headers
3973 `),
3974 expectedSystemIncludeDirs(`foo/system`),
3975 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3976 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
3977 )
3978
3979 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
3980 checkIncludeDirs(t, ctx, bar,
3981 expectedIncludeDirs(`
3982 bar/standard
3983 .intermediates/genrule_bar/gen/generated_headers
3984 `),
3985 expectedSystemIncludeDirs(`bar/system`),
3986 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
3987 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
3988 )
3989 })
3990
3991 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
3992 ctx := testCc(t, genRuleModules+`
3993 cc_library {
3994 name: "libfoo",
3995 srcs: ["foo.c"],
3996 export_include_dirs: ["foo/standard"],
3997 export_system_include_dirs: ["foo/system"],
3998 generated_headers: ["genrule_foo"],
3999 export_generated_headers: ["genrule_foo"],
4000 }
4001
4002 cc_library {
4003 name: "libbar",
4004 srcs: ["bar.c"],
4005 whole_static_libs: ["libfoo"],
4006 export_include_dirs: ["bar/standard"],
4007 export_system_include_dirs: ["bar/system"],
4008 generated_headers: ["genrule_bar"],
4009 export_generated_headers: ["genrule_bar"],
4010 }
4011 `)
4012 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4013 checkIncludeDirs(t, ctx, foo,
4014 expectedIncludeDirs(`
4015 foo/standard
4016 .intermediates/genrule_foo/gen/generated_headers
4017 `),
4018 expectedSystemIncludeDirs(`foo/system`),
4019 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4020 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4021 )
4022
4023 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4024 checkIncludeDirs(t, ctx, bar,
4025 expectedIncludeDirs(`
4026 bar/standard
4027 foo/standard
4028 .intermediates/genrule_foo/gen/generated_headers
4029 .intermediates/genrule_bar/gen/generated_headers
4030 `),
4031 expectedSystemIncludeDirs(`
4032 bar/system
4033 foo/system
4034 `),
4035 expectedGeneratedHeaders(`
4036 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4037 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4038 `),
4039 expectedOrderOnlyDeps(`
4040 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4041 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4042 `),
4043 )
4044 })
4045
Paul Duffin3cb603e2021-02-19 13:57:10 +00004046 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4047 ctx := testCc(t, genRuleModules+`
4048 cc_library_shared {
4049 name: "libfoo",
4050 srcs: [
4051 "foo.c",
4052 "b.aidl",
4053 "a.proto",
4054 ],
4055 aidl: {
4056 export_aidl_headers: true,
4057 }
4058 }
4059 `)
4060 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4061 checkIncludeDirs(t, ctx, foo,
4062 expectedIncludeDirs(`
4063 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4064 `),
4065 expectedSystemIncludeDirs(``),
4066 expectedGeneratedHeaders(`
4067 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4068 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4069 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004070 `),
4071 expectedOrderOnlyDeps(`
4072 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4073 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4074 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004075 `),
4076 )
4077 })
4078
Paul Duffin3cb603e2021-02-19 13:57:10 +00004079 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4080 ctx := testCc(t, genRuleModules+`
4081 cc_library_shared {
4082 name: "libfoo",
4083 srcs: [
4084 "foo.c",
4085 "b.aidl",
4086 "a.proto",
4087 ],
4088 proto: {
4089 export_proto_headers: true,
4090 }
4091 }
4092 `)
4093 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4094 checkIncludeDirs(t, ctx, foo,
4095 expectedIncludeDirs(`
4096 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4097 `),
4098 expectedSystemIncludeDirs(``),
4099 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004100 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4101 `),
4102 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004103 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4104 `),
4105 )
4106 })
4107
Paul Duffin33056e82021-02-19 13:49:08 +00004108 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004109 ctx := testCc(t, genRuleModules+`
4110 cc_library_shared {
4111 name: "libfoo",
4112 srcs: [
4113 "foo.c",
4114 "a.sysprop",
4115 "b.aidl",
4116 "a.proto",
4117 ],
4118 }
4119 `)
4120 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4121 checkIncludeDirs(t, ctx, foo,
4122 expectedIncludeDirs(`
4123 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4124 `),
4125 expectedSystemIncludeDirs(``),
4126 expectedGeneratedHeaders(`
4127 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004128 `),
4129 expectedOrderOnlyDeps(`
4130 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/a.sysprop.h
4131 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004132 `),
4133 )
4134 })
4135}
Colin Crossae628182021-06-14 16:52:28 -07004136
4137func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer08572c62021-09-30 10:11:04 -04004138 baseExpectedFlags := []string{
4139 "${config.ArmThumbCflags}",
4140 "${config.ArmCflags}",
4141 "${config.CommonGlobalCflags}",
4142 "${config.DeviceGlobalCflags}",
4143 "${config.ExternalCflags}",
4144 "${config.ArmToolchainCflags}",
4145 "${config.ArmArmv7ANeonCflags}",
4146 "${config.ArmGenericCflags}",
4147 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004148 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004149 }
4150
4151 expectedIncludes := []string{
4152 "external/foo/android_arm_export_include_dirs",
4153 "external/foo/lib32_export_include_dirs",
4154 "external/foo/arm_export_include_dirs",
4155 "external/foo/android_export_include_dirs",
4156 "external/foo/linux_export_include_dirs",
4157 "external/foo/export_include_dirs",
4158 "external/foo/android_arm_local_include_dirs",
4159 "external/foo/lib32_local_include_dirs",
4160 "external/foo/arm_local_include_dirs",
4161 "external/foo/android_local_include_dirs",
4162 "external/foo/linux_local_include_dirs",
4163 "external/foo/local_include_dirs",
4164 "external/foo",
4165 "external/foo/libheader1",
4166 "external/foo/libheader2",
4167 "external/foo/libwhole1",
4168 "external/foo/libwhole2",
4169 "external/foo/libstatic1",
4170 "external/foo/libstatic2",
4171 "external/foo/libshared1",
4172 "external/foo/libshared2",
4173 "external/foo/liblinux",
4174 "external/foo/libandroid",
4175 "external/foo/libarm",
4176 "external/foo/lib32",
4177 "external/foo/libandroid_arm",
4178 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004179 }
4180
4181 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4182 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4183
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004184 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004185 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004186 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004187
4188 lastIncludes := []string{
4189 "out/soong/ndk/sysroot/usr/include",
4190 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4191 }
4192
4193 combineSlices := func(slices ...[]string) []string {
4194 var ret []string
4195 for _, s := range slices {
4196 ret = append(ret, s...)
4197 }
4198 return ret
4199 }
4200
4201 testCases := []struct {
4202 name string
4203 src string
4204 expected []string
4205 }{
4206 {
4207 name: "c",
4208 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004209 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004210 },
4211 {
4212 name: "cc",
4213 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004214 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004215 },
4216 {
4217 name: "assemble",
4218 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004219 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004220 },
4221 }
4222
4223 for _, tc := range testCases {
4224 t.Run(tc.name, func(t *testing.T) {
4225 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004226 cc_library {
4227 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004228 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004229 cflags: ["-std=candcpp"],
4230 conlyflags: ["-std=conly"],
4231 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004232 local_include_dirs: ["local_include_dirs"],
4233 export_include_dirs: ["export_include_dirs"],
4234 export_system_include_dirs: ["export_system_include_dirs"],
4235 static_libs: ["libstatic1", "libstatic2"],
4236 whole_static_libs: ["libwhole1", "libwhole2"],
4237 shared_libs: ["libshared1", "libshared2"],
4238 header_libs: ["libheader1", "libheader2"],
4239 target: {
4240 android: {
4241 shared_libs: ["libandroid"],
4242 local_include_dirs: ["android_local_include_dirs"],
4243 export_include_dirs: ["android_export_include_dirs"],
4244 },
4245 android_arm: {
4246 shared_libs: ["libandroid_arm"],
4247 local_include_dirs: ["android_arm_local_include_dirs"],
4248 export_include_dirs: ["android_arm_export_include_dirs"],
4249 },
4250 linux: {
4251 shared_libs: ["liblinux"],
4252 local_include_dirs: ["linux_local_include_dirs"],
4253 export_include_dirs: ["linux_export_include_dirs"],
4254 },
4255 },
4256 multilib: {
4257 lib32: {
4258 shared_libs: ["lib32"],
4259 local_include_dirs: ["lib32_local_include_dirs"],
4260 export_include_dirs: ["lib32_export_include_dirs"],
4261 },
4262 },
4263 arch: {
4264 arm: {
4265 shared_libs: ["libarm"],
4266 local_include_dirs: ["arm_local_include_dirs"],
4267 export_include_dirs: ["arm_export_include_dirs"],
4268 },
4269 },
4270 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004271 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004272 }
4273
4274 cc_library_headers {
4275 name: "libheader1",
4276 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004277 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004278 stl: "none",
4279 }
4280
4281 cc_library_headers {
4282 name: "libheader2",
4283 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004284 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004285 stl: "none",
4286 }
Liz Kammer08572c62021-09-30 10:11:04 -04004287 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004288
Liz Kammer08572c62021-09-30 10:11:04 -04004289 libs := []string{
4290 "libstatic1",
4291 "libstatic2",
4292 "libwhole1",
4293 "libwhole2",
4294 "libshared1",
4295 "libshared2",
4296 "libandroid",
4297 "libandroid_arm",
4298 "liblinux",
4299 "lib32",
4300 "libarm",
4301 }
Colin Crossae628182021-06-14 16:52:28 -07004302
Liz Kammer08572c62021-09-30 10:11:04 -04004303 for _, lib := range libs {
4304 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004305 cc_library {
4306 name: "%s",
4307 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004308 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004309 stl: "none",
4310 }
4311 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004312 }
4313
4314 ctx := android.GroupFixturePreparers(
4315 PrepareForIntegrationTestWithCc,
4316 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4317 ).RunTest(t)
4318 // Use the arm variant instead of the arm64 variant so that it gets headers from
4319 // ndk_libandroid_support to test LateStaticLibs.
4320 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4321
4322 var includes []string
4323 flags := strings.Split(cflags, " ")
4324 for _, flag := range flags {
4325 if strings.HasPrefix(flag, "-I") {
4326 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4327 } else if flag == "-isystem" {
4328 // skip isystem, include next
4329 } else if len(flag) > 0 {
4330 includes = append(includes, flag)
4331 }
4332 }
4333
4334 android.AssertArrayString(t, "includes", tc.expected, includes)
4335 })
Colin Crossae628182021-06-14 16:52:28 -07004336 }
4337
Colin Crossae628182021-06-14 16:52:28 -07004338}
Alixb5f6d9e2022-04-20 23:00:58 +00004339
4340func TestCcBuildBrokenClangProperty(t *testing.T) {
4341 tests := []struct {
4342 name string
4343 clang bool
4344 BuildBrokenClangProperty bool
4345 err string
4346 }{
4347 {
4348 name: "error when clang is set to false",
4349 clang: false,
4350 err: "is no longer supported",
4351 },
4352 {
4353 name: "error when clang is set to true",
4354 clang: true,
4355 err: "property is deprecated, see Changes.md",
4356 },
4357 {
4358 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4359 clang: true,
4360 BuildBrokenClangProperty: true,
4361 },
4362 }
4363
4364 for _, test := range tests {
4365 t.Run(test.name, func(t *testing.T) {
4366 bp := fmt.Sprintf(`
4367 cc_library {
4368 name: "foo",
4369 clang: %t,
4370 }`, test.clang)
4371
4372 if test.err == "" {
4373 android.GroupFixturePreparers(
4374 prepareForCcTest,
4375 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4376 if test.BuildBrokenClangProperty {
4377 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4378 }
4379 }),
4380 ).RunTestWithBp(t, bp)
4381 } else {
4382 prepareForCcTest.
4383 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4384 RunTestWithBp(t, bp)
4385 }
4386 })
4387 }
4388}