blob: 8293f2db77c196b4dbd58f7c32e8de24aa850b11 [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) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400157 t.Parallel()
Jiyong Park6a43f042017-10-12 23:05:00 +0900158 ctx := testCc(t, `
159 cc_library {
160 name: "libTest",
161 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -0700162 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +0800163 nocrt: true,
164 system_shared_libs: [],
Jiyong Park6a43f042017-10-12 23:05:00 +0900165 vendor_available: true,
166 target: {
167 vendor: {
168 srcs: ["bar.c"],
169 },
170 },
171 }
Jiyong Park6a43f042017-10-12 23:05:00 +0900172 `)
173
Logan Chienf3511742017-10-31 18:04:35 +0800174 ld := ctx.ModuleForTests("libTest", vendorVariant).Rule("ld")
Jiyong Park6a43f042017-10-12 23:05:00 +0900175 var objs []string
176 for _, o := range ld.Inputs {
177 objs = append(objs, o.Base())
178 }
Colin Cross95d33fe2018-01-03 13:40:46 -0800179 if len(objs) != 2 || objs[0] != "foo.o" || objs[1] != "bar.o" {
Jiyong Park6a43f042017-10-12 23:05:00 +0900180 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
181 }
182}
183
Justin Yun7f99ec72021-04-12 13:19:28 +0900184func checkInstallPartition(t *testing.T, ctx *android.TestContext, name, variant, expected string) {
185 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
186 partitionDefined := false
187 checkPartition := func(specific bool, partition string) {
188 if specific {
189 if expected != partition && !partitionDefined {
190 // The variant is installed to the 'partition'
191 t.Errorf("%s variant of %q must not be installed to %s partition", variant, name, partition)
192 }
193 partitionDefined = true
194 } else {
195 // The variant is not installed to the 'partition'
196 if expected == partition {
197 t.Errorf("%s variant of %q must be installed to %s partition", variant, name, partition)
198 }
199 }
200 }
201 socSpecific := func(m *Module) bool {
202 return m.SocSpecific() || m.socSpecificModuleContext()
203 }
204 deviceSpecific := func(m *Module) bool {
205 return m.DeviceSpecific() || m.deviceSpecificModuleContext()
206 }
207 productSpecific := func(m *Module) bool {
208 return m.ProductSpecific() || m.productSpecificModuleContext()
209 }
210 systemExtSpecific := func(m *Module) bool {
211 return m.SystemExtSpecific()
212 }
213 checkPartition(socSpecific(mod), "vendor")
214 checkPartition(deviceSpecific(mod), "odm")
215 checkPartition(productSpecific(mod), "product")
216 checkPartition(systemExtSpecific(mod), "system_ext")
217 if !partitionDefined && expected != "system" {
218 t.Errorf("%s variant of %q is expected to be installed to %s partition,"+
219 " but installed to system partition", variant, name, expected)
220 }
221}
222
223func TestInstallPartition(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400224 t.Parallel()
Justin Yun7f99ec72021-04-12 13:19:28 +0900225 t.Helper()
226 ctx := prepareForCcTest.RunTestWithBp(t, `
227 cc_library {
228 name: "libsystem",
229 }
230 cc_library {
231 name: "libsystem_ext",
232 system_ext_specific: true,
233 }
234 cc_library {
235 name: "libproduct",
236 product_specific: true,
237 }
238 cc_library {
239 name: "libvendor",
240 vendor: true,
241 }
242 cc_library {
243 name: "libodm",
244 device_specific: true,
245 }
246 cc_library {
247 name: "liball_available",
248 vendor_available: true,
249 product_available: true,
250 }
251 cc_library {
252 name: "libsystem_ext_all_available",
253 system_ext_specific: true,
254 vendor_available: true,
255 product_available: true,
256 }
257 cc_library {
258 name: "liball_available_odm",
259 odm_available: true,
260 product_available: true,
261 }
262 cc_library {
263 name: "libproduct_vendoravailable",
264 product_specific: true,
265 vendor_available: true,
266 }
267 cc_library {
268 name: "libproduct_odmavailable",
269 product_specific: true,
270 odm_available: true,
271 }
272 `).TestContext
273
274 checkInstallPartition(t, ctx, "libsystem", coreVariant, "system")
275 checkInstallPartition(t, ctx, "libsystem_ext", coreVariant, "system_ext")
276 checkInstallPartition(t, ctx, "libproduct", productVariant, "product")
277 checkInstallPartition(t, ctx, "libvendor", vendorVariant, "vendor")
278 checkInstallPartition(t, ctx, "libodm", vendorVariant, "odm")
279
280 checkInstallPartition(t, ctx, "liball_available", coreVariant, "system")
281 checkInstallPartition(t, ctx, "liball_available", productVariant, "product")
282 checkInstallPartition(t, ctx, "liball_available", vendorVariant, "vendor")
283
284 checkInstallPartition(t, ctx, "libsystem_ext_all_available", coreVariant, "system_ext")
285 checkInstallPartition(t, ctx, "libsystem_ext_all_available", productVariant, "product")
286 checkInstallPartition(t, ctx, "libsystem_ext_all_available", vendorVariant, "vendor")
287
288 checkInstallPartition(t, ctx, "liball_available_odm", coreVariant, "system")
289 checkInstallPartition(t, ctx, "liball_available_odm", productVariant, "product")
290 checkInstallPartition(t, ctx, "liball_available_odm", vendorVariant, "odm")
291
292 checkInstallPartition(t, ctx, "libproduct_vendoravailable", productVariant, "product")
293 checkInstallPartition(t, ctx, "libproduct_vendoravailable", vendorVariant, "vendor")
294
295 checkInstallPartition(t, ctx, "libproduct_odmavailable", productVariant, "product")
296 checkInstallPartition(t, ctx, "libproduct_odmavailable", vendorVariant, "odm")
297}
298
Logan Chienf3511742017-10-31 18:04:35 +0800299func checkVndkModule(t *testing.T, ctx *android.TestContext, name, subDir string,
Justin Yun0ecf0b22020-02-28 15:07:59 +0900300 isVndkSp bool, extends string, variant string) {
Logan Chienf3511742017-10-31 18:04:35 +0800301
Logan Chiend3c59a22018-03-29 14:08:15 +0800302 t.Helper()
303
Justin Yun0ecf0b22020-02-28 15:07:59 +0900304 mod := ctx.ModuleForTests(name, variant).Module().(*Module)
Logan Chienf3511742017-10-31 18:04:35 +0800305
306 // Check library properties.
307 lib, ok := mod.compiler.(*libraryDecorator)
308 if !ok {
309 t.Errorf("%q must have libraryDecorator", name)
310 } else if lib.baseInstaller.subDir != subDir {
311 t.Errorf("%q must use %q as subdir but it is using %q", name, subDir,
312 lib.baseInstaller.subDir)
313 }
314
315 // Check VNDK properties.
316 if mod.vndkdep == nil {
317 t.Fatalf("%q must have `vndkdep`", name)
318 }
Ivan Lozano52767be2019-10-18 14:49:46 -0700319 if !mod.IsVndk() {
320 t.Errorf("%q IsVndk() must equal to true", name)
Logan Chienf3511742017-10-31 18:04:35 +0800321 }
Ivan Lozanod7586b62021-04-01 09:49:36 -0400322 if mod.IsVndkSp() != isVndkSp {
323 t.Errorf("%q IsVndkSp() must equal to %t", name, isVndkSp)
Logan Chienf3511742017-10-31 18:04:35 +0800324 }
325
326 // Check VNDK extension properties.
327 isVndkExt := extends != ""
Ivan Lozanof9e21722020-12-02 09:00:51 -0500328 if mod.IsVndkExt() != isVndkExt {
329 t.Errorf("%q IsVndkExt() must equal to %t", name, isVndkExt)
Logan Chienf3511742017-10-31 18:04:35 +0800330 }
331
332 if actualExtends := mod.getVndkExtendsModuleName(); actualExtends != extends {
333 t.Errorf("%q must extend from %q but get %q", name, extends, actualExtends)
334 }
335}
336
Jooyung Han2216fb12019-11-06 16:46:15 +0900337func checkWriteFileOutput(t *testing.T, params android.TestingBuildParams, expected []string) {
338 t.Helper()
Colin Crosscf371cc2020-11-13 11:48:42 -0800339 content := android.ContentFromFileRuleForTests(t, params)
340 actual := strings.FieldsFunc(content, func(r rune) bool { return r == '\n' })
Jooyung Han2216fb12019-11-06 16:46:15 +0900341 assertArrayString(t, actual, expected)
342}
343
Jooyung Han097087b2019-10-22 19:32:18 +0900344func checkVndkOutput(t *testing.T, ctx *android.TestContext, output string, expected []string) {
345 t.Helper()
346 vndkSnapshot := ctx.SingletonForTests("vndk-snapshot")
Jooyung Han2216fb12019-11-06 16:46:15 +0900347 checkWriteFileOutput(t, vndkSnapshot.Output(output), expected)
348}
349
350func checkVndkLibrariesOutput(t *testing.T, ctx *android.TestContext, module string, expected []string) {
351 t.Helper()
Colin Cross45bce852021-11-11 22:47:54 -0800352 got := ctx.ModuleForTests(module, "android_common").Module().(*vndkLibrariesTxt).fileNames
Colin Cross78212242021-01-06 14:51:30 -0800353 assertArrayString(t, got, expected)
Jooyung Han097087b2019-10-22 19:32:18 +0900354}
355
Logan Chienf3511742017-10-31 18:04:35 +0800356func TestVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400357 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800358 bp := `
Logan Chienf3511742017-10-31 18:04:35 +0800359 cc_library {
360 name: "libvndk",
361 vendor_available: true,
362 vndk: {
363 enabled: true,
364 },
365 nocrt: true,
366 }
367
368 cc_library {
369 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900370 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800371 vndk: {
372 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900373 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800374 },
375 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900376 stem: "libvndk-private",
Logan Chienf3511742017-10-31 18:04:35 +0800377 }
378
379 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +0900380 name: "libvndk_product",
Logan Chienf3511742017-10-31 18:04:35 +0800381 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900382 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800383 vndk: {
384 enabled: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900385 },
386 nocrt: true,
387 target: {
388 vendor: {
389 cflags: ["-DTEST"],
390 },
391 product: {
392 cflags: ["-DTEST"],
393 },
394 },
395 }
396
397 cc_library {
398 name: "libvndk_sp",
399 vendor_available: true,
400 vndk: {
401 enabled: true,
Logan Chienf3511742017-10-31 18:04:35 +0800402 support_system_process: true,
403 },
404 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900405 suffix: "-x",
Logan Chienf3511742017-10-31 18:04:35 +0800406 }
407
408 cc_library {
409 name: "libvndk_sp_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900410 vendor_available: true,
Logan Chienf3511742017-10-31 18:04:35 +0800411 vndk: {
412 enabled: true,
413 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900414 private: true,
Logan Chienf3511742017-10-31 18:04:35 +0800415 },
416 nocrt: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900417 target: {
418 vendor: {
419 suffix: "-x",
420 },
421 },
Logan Chienf3511742017-10-31 18:04:35 +0800422 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900423
424 cc_library {
425 name: "libvndk_sp_product_private",
Justin Yunfd9e8042020-12-23 18:23:14 +0900426 vendor_available: true,
427 product_available: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900428 vndk: {
429 enabled: true,
430 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900431 private: true,
Justin Yun6977e8a2020-10-29 18:24:11 +0900432 },
433 nocrt: true,
434 target: {
435 vendor: {
436 suffix: "-x",
437 },
438 product: {
439 suffix: "-x",
440 },
441 },
442 }
443
Justin Yun450ae722021-04-16 19:58:18 +0900444 cc_library {
445 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700446 llndk: {
447 symbol_file: "libllndk.map.txt",
448 export_llndk_headers: ["libllndk_headers"],
449 }
Justin Yun450ae722021-04-16 19:58:18 +0900450 }
451
Justin Yun611e8862021-05-24 18:17:33 +0900452 cc_library {
453 name: "libclang_rt.hwasan-llndk",
454 llndk: {
455 symbol_file: "libclang_rt.hwasan.map.txt",
456 }
457 }
458
Colin Cross627280f2021-04-26 16:53:58 -0700459 cc_library_headers {
Justin Yun450ae722021-04-16 19:58:18 +0900460 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700461 llndk: {
462 llndk_headers: true,
463 },
Justin Yun450ae722021-04-16 19:58:18 +0900464 export_include_dirs: ["include"],
465 }
466
Colin Crosse4e44bc2020-12-28 13:50:21 -0800467 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900468 name: "llndk.libraries.txt",
469 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800470 vndkcore_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900471 name: "vndkcore.libraries.txt",
472 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800473 vndksp_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900474 name: "vndksp.libraries.txt",
475 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800476 vndkprivate_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900477 name: "vndkprivate.libraries.txt",
478 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800479 vndkproduct_libraries_txt {
Justin Yun8a2600c2020-12-07 12:44:03 +0900480 name: "vndkproduct.libraries.txt",
481 }
Colin Crosse4e44bc2020-12-28 13:50:21 -0800482 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900483 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800484 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900485 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800486 `
487
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000488 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800489 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Justin Yun63e9ec72020-10-29 16:49:43 +0900490 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900491 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800492
493 ctx := testCcWithConfig(t, config)
Logan Chienf3511742017-10-31 18:04:35 +0800494
Jooyung Han261e1582020-10-20 18:54:21 +0900495 // subdir == "" because VNDK libs are not supposed to be installed separately.
496 // They are installed as part of VNDK APEX instead.
497 checkVndkModule(t, ctx, "libvndk", "", false, "", vendorVariant)
498 checkVndkModule(t, ctx, "libvndk_private", "", false, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900499 checkVndkModule(t, ctx, "libvndk_product", "", false, "", vendorVariant)
Jooyung Han261e1582020-10-20 18:54:21 +0900500 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", vendorVariant)
501 checkVndkModule(t, ctx, "libvndk_sp_private", "", true, "", vendorVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +0900502 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", vendorVariant)
Inseob Kim1f086e22019-05-09 13:29:15 +0900503
Justin Yun6977e8a2020-10-29 18:24:11 +0900504 checkVndkModule(t, ctx, "libvndk_product", "", false, "", productVariant)
505 checkVndkModule(t, ctx, "libvndk_sp_product_private", "", true, "", productVariant)
Justin Yun63e9ec72020-10-29 16:49:43 +0900506
Inseob Kim1f086e22019-05-09 13:29:15 +0900507 // Check VNDK snapshot output.
Inseob Kim1f086e22019-05-09 13:29:15 +0900508 snapshotDir := "vndk-snapshot"
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000509 snapshotVariantPath := filepath.Join("out/soong", snapshotDir, "arm64")
Inseob Kim1f086e22019-05-09 13:29:15 +0900510
511 vndkLibPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
512 "arm64", "armv8-a"))
513 vndkLib2ndPath := filepath.Join(snapshotVariantPath, fmt.Sprintf("arch-%s-%s",
514 "arm", "armv7-a-neon"))
515
516 vndkCoreLibPath := filepath.Join(vndkLibPath, "shared", "vndk-core")
517 vndkSpLibPath := filepath.Join(vndkLibPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900518 llndkLibPath := filepath.Join(vndkLibPath, "shared", "llndk-stub")
519
Inseob Kim1f086e22019-05-09 13:29:15 +0900520 vndkCoreLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-core")
521 vndkSpLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "vndk-sp")
Justin Yun450ae722021-04-16 19:58:18 +0900522 llndkLib2ndPath := filepath.Join(vndkLib2ndPath, "shared", "llndk-stub")
Inseob Kim1f086e22019-05-09 13:29:15 +0900523
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900524 variant := "android_vendor.29_arm64_armv8-a_shared"
525 variant2nd := "android_vendor.29_arm_armv7-a-neon_shared"
Inseob Kim1f086e22019-05-09 13:29:15 +0900526
Inseob Kim7f283f42020-06-01 21:53:49 +0900527 snapshotSingleton := ctx.SingletonForTests("vndk-snapshot")
528
Ivan Lozanod67a6b02021-05-20 13:01:32 -0400529 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLibPath, variant)
530 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk", "libvndk.so", vndkCoreLib2ndPath, variant2nd)
531 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLibPath, variant)
532 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_product", "libvndk_product.so", vndkCoreLib2ndPath, variant2nd)
533 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLibPath, variant)
534 CheckSnapshot(t, ctx, snapshotSingleton, "libvndk_sp", "libvndk_sp-x.so", vndkSpLib2ndPath, variant2nd)
535 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLibPath, variant)
536 CheckSnapshot(t, ctx, snapshotSingleton, "libllndk", "libllndk.so", llndkLib2ndPath, variant2nd)
Jooyung Han097087b2019-10-22 19:32:18 +0900537
Jooyung Han39edb6c2019-11-06 16:53:07 +0900538 snapshotConfigsPath := filepath.Join(snapshotVariantPath, "configs")
Colin Cross45bce852021-11-11 22:47:54 -0800539 CheckSnapshot(t, ctx, snapshotSingleton, "llndk.libraries.txt", "llndk.libraries.txt", snapshotConfigsPath, "android_common")
540 CheckSnapshot(t, ctx, snapshotSingleton, "vndkcore.libraries.txt", "vndkcore.libraries.txt", snapshotConfigsPath, "android_common")
541 CheckSnapshot(t, ctx, snapshotSingleton, "vndksp.libraries.txt", "vndksp.libraries.txt", snapshotConfigsPath, "android_common")
542 CheckSnapshot(t, ctx, snapshotSingleton, "vndkprivate.libraries.txt", "vndkprivate.libraries.txt", snapshotConfigsPath, "android_common")
543 CheckSnapshot(t, ctx, snapshotSingleton, "vndkproduct.libraries.txt", "vndkproduct.libraries.txt", snapshotConfigsPath, "android_common")
Jooyung Han39edb6c2019-11-06 16:53:07 +0900544
Jooyung Han097087b2019-10-22 19:32:18 +0900545 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
546 "LLNDK: libc.so",
547 "LLNDK: libdl.so",
548 "LLNDK: libft2.so",
Justin Yun450ae722021-04-16 19:58:18 +0900549 "LLNDK: libllndk.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900550 "LLNDK: libm.so",
551 "VNDK-SP: libc++.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900552 "VNDK-SP: libvndk_sp-x.so",
553 "VNDK-SP: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900554 "VNDK-SP: libvndk_sp_product_private-x.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900555 "VNDK-core: libvndk-private.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900556 "VNDK-core: libvndk.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900557 "VNDK-core: libvndk_product.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900558 "VNDK-private: libft2.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900559 "VNDK-private: libvndk-private.so",
560 "VNDK-private: libvndk_sp_private-x.so",
Justin Yun6977e8a2020-10-29 18:24:11 +0900561 "VNDK-private: libvndk_sp_product_private-x.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900562 "VNDK-product: libc++.so",
563 "VNDK-product: libvndk_product.so",
564 "VNDK-product: libvndk_sp_product_private-x.so",
Jooyung Han097087b2019-10-22 19:32:18 +0900565 })
Justin Yun611e8862021-05-24 18:17:33 +0900566 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 +0900567 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk-private.so", "libvndk.so", "libvndk_product.so"})
568 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt", []string{"libc++.so", "libvndk_sp-x.so", "libvndk_sp_private-x.so", "libvndk_sp_product_private-x.so"})
569 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 +0900570 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 +0900571 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", nil)
572}
573
Yo Chiangbba545e2020-06-09 16:15:37 +0800574func TestVndkWithHostSupported(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400575 t.Parallel()
Yo Chiangbba545e2020-06-09 16:15:37 +0800576 ctx := testCc(t, `
577 cc_library {
578 name: "libvndk_host_supported",
579 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900580 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800581 vndk: {
582 enabled: true,
583 },
584 host_supported: true,
585 }
586
587 cc_library {
588 name: "libvndk_host_supported_but_disabled_on_device",
589 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900590 product_available: true,
Yo Chiangbba545e2020-06-09 16:15:37 +0800591 vndk: {
592 enabled: true,
593 },
594 host_supported: true,
595 enabled: false,
596 target: {
597 host: {
598 enabled: true,
599 }
600 }
601 }
602
Colin Crosse4e44bc2020-12-28 13:50:21 -0800603 vndkcore_libraries_txt {
Yo Chiangbba545e2020-06-09 16:15:37 +0800604 name: "vndkcore.libraries.txt",
605 }
606 `)
607
608 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt", []string{"libvndk_host_supported.so"})
609}
610
Jooyung Han2216fb12019-11-06 16:46:15 +0900611func TestVndkLibrariesTxtAndroidMk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400612 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800613 bp := `
Colin Crosse4e44bc2020-12-28 13:50:21 -0800614 llndk_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900615 name: "llndk.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800616 insert_vndk_version: true,
Colin Cross98be1bb2019-12-13 20:41:13 -0800617 }`
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000618 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800619 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900620 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800621 ctx := testCcWithConfig(t, config)
Jooyung Han2216fb12019-11-06 16:46:15 +0900622
Colin Cross45bce852021-11-11 22:47:54 -0800623 module := ctx.ModuleForTests("llndk.libraries.txt", "android_common")
Colin Crossaa255532020-07-03 13:18:24 -0700624 entries := android.AndroidMkEntriesForTest(t, ctx, module.Module())[0]
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900625 assertArrayString(t, entries.EntryMap["LOCAL_MODULE_STEM"], []string{"llndk.libraries.29.txt"})
Jooyung Han097087b2019-10-22 19:32:18 +0900626}
627
628func TestVndkUsingCoreVariant(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400629 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -0800630 bp := `
Jooyung Han097087b2019-10-22 19:32:18 +0900631 cc_library {
632 name: "libvndk",
633 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900634 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900635 vndk: {
636 enabled: true,
637 },
638 nocrt: true,
639 }
640
641 cc_library {
642 name: "libvndk_sp",
643 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900644 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900645 vndk: {
646 enabled: true,
647 support_system_process: true,
648 },
649 nocrt: true,
650 }
651
652 cc_library {
653 name: "libvndk2",
Justin Yunfd9e8042020-12-23 18:23:14 +0900654 vendor_available: true,
655 product_available: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900656 vndk: {
657 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +0900658 private: true,
Jooyung Han097087b2019-10-22 19:32:18 +0900659 },
660 nocrt: true,
661 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900662
Colin Crosse4e44bc2020-12-28 13:50:21 -0800663 vndkcorevariant_libraries_txt {
Jooyung Han2216fb12019-11-06 16:46:15 +0900664 name: "vndkcorevariant.libraries.txt",
Colin Crosse4e44bc2020-12-28 13:50:21 -0800665 insert_vndk_version: false,
Jooyung Han2216fb12019-11-06 16:46:15 +0900666 }
Colin Cross98be1bb2019-12-13 20:41:13 -0800667 `
668
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000669 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Cross98be1bb2019-12-13 20:41:13 -0800670 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900671 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Colin Cross98be1bb2019-12-13 20:41:13 -0800672 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
673
674 setVndkMustUseVendorVariantListForTest(config, []string{"libvndk"})
675
676 ctx := testCcWithConfig(t, config)
Jooyung Han097087b2019-10-22 19:32:18 +0900677
Jooyung Han2216fb12019-11-06 16:46:15 +0900678 checkVndkLibrariesOutput(t, ctx, "vndkcorevariant.libraries.txt", []string{"libc++.so", "libvndk2.so", "libvndk_sp.so"})
Jooyung Han0302a842019-10-30 18:43:49 +0900679}
680
Chris Parsons79d66a52020-06-05 17:26:16 -0400681func TestDataLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400682 t.Parallel()
Chris Parsons79d66a52020-06-05 17:26:16 -0400683 bp := `
684 cc_test_library {
685 name: "test_lib",
686 srcs: ["test_lib.cpp"],
687 gtest: false,
688 }
689
690 cc_test {
691 name: "main_test",
692 data_libs: ["test_lib"],
693 gtest: false,
694 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400695 `
Chris Parsons79d66a52020-06-05 17:26:16 -0400696
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000697 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons79d66a52020-06-05 17:26:16 -0400698 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900699 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons79d66a52020-06-05 17:26:16 -0400700 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
701
702 ctx := testCcWithConfig(t, config)
703 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
704 testBinary := module.(*Module).linker.(*testBinary)
705 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
706 if err != nil {
707 t.Errorf("Expected cc_test to produce output files, error: %s", err)
708 return
709 }
710 if len(outputFiles) != 1 {
711 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
712 return
713 }
714 if len(testBinary.dataPaths()) != 1 {
715 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
716 return
717 }
718
719 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400720 testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
Chris Parsons79d66a52020-06-05 17:26:16 -0400721
722 if !strings.HasSuffix(outputPath, "/main_test") {
723 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
724 return
725 }
726 if !strings.HasSuffix(testBinaryPath, "/test_lib.so") {
727 t.Errorf("expected test data file to be 'test_lib.so', but was '%s'", testBinaryPath)
728 return
729 }
730}
731
Chris Parsons216e10a2020-07-09 17:12:52 -0400732func TestDataLibsRelativeInstallPath(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400733 t.Parallel()
Chris Parsons216e10a2020-07-09 17:12:52 -0400734 bp := `
735 cc_test_library {
736 name: "test_lib",
737 srcs: ["test_lib.cpp"],
738 relative_install_path: "foo/bar/baz",
739 gtest: false,
740 }
741
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400742 cc_binary {
743 name: "test_bin",
744 relative_install_path: "foo/bar/baz",
745 compile_multilib: "both",
746 }
747
Chris Parsons216e10a2020-07-09 17:12:52 -0400748 cc_test {
749 name: "main_test",
750 data_libs: ["test_lib"],
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400751 data_bins: ["test_bin"],
Chris Parsons216e10a2020-07-09 17:12:52 -0400752 gtest: false,
753 }
754 `
755
Paul Duffinc3e6ce02021-03-22 23:21:32 +0000756 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons216e10a2020-07-09 17:12:52 -0400757 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +0900758 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons216e10a2020-07-09 17:12:52 -0400759 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
760
761 ctx := testCcWithConfig(t, config)
762 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
763 testBinary := module.(*Module).linker.(*testBinary)
764 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
765 if err != nil {
766 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
767 }
768 if len(outputFiles) != 1 {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400769 t.Fatalf("expected exactly one output file. output files: [%s]", outputFiles)
Chris Parsons216e10a2020-07-09 17:12:52 -0400770 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400771 if len(testBinary.dataPaths()) != 2 {
772 t.Fatalf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
Chris Parsons216e10a2020-07-09 17:12:52 -0400773 }
774
775 outputPath := outputFiles[0].String()
Chris Parsons216e10a2020-07-09 17:12:52 -0400776
777 if !strings.HasSuffix(outputPath, "/main_test") {
778 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
779 }
Colin Crossaa255532020-07-03 13:18:24 -0700780 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons216e10a2020-07-09 17:12:52 -0400781 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
782 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
Chris Parsons1f6d90f2020-06-17 16:10:42 -0400783 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
Chris Parsons216e10a2020-07-09 17:12:52 -0400784 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400785 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][1], ":test_bin:foo/bar/baz") {
786 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_bin:foo/bar/baz`,"+
787 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][1])
788 }
Chris Parsons216e10a2020-07-09 17:12:52 -0400789}
790
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000791func TestTestBinaryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400792 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000793 bp := `
794 cc_test {
795 name: "main_test",
796 srcs: ["main_test.cpp"],
797 test_suites: [
798 "suite_1",
799 "suite_2",
800 ],
801 gtest: false,
802 }
803 `
804
805 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
806 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
807
808 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
809 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
810 if len(compatEntries) != 2 {
811 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
812 }
813 if compatEntries[0] != "suite_1" {
814 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
815 " but was '%s'", compatEntries[0])
816 }
817 if compatEntries[1] != "suite_2" {
818 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
819 " but was '%s'", compatEntries[1])
820 }
821}
822
823func TestTestLibraryTestSuites(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400824 t.Parallel()
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000825 bp := `
826 cc_test_library {
827 name: "main_test_lib",
828 srcs: ["main_test_lib.cpp"],
829 test_suites: [
830 "suite_1",
831 "suite_2",
832 ],
833 gtest: false,
834 }
835 `
836
837 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
838 module := ctx.ModuleForTests("main_test_lib", "android_arm_armv7-a-neon_shared").Module()
839
840 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
841 compatEntries := entries.EntryMap["LOCAL_COMPATIBILITY_SUITE"]
842 if len(compatEntries) != 2 {
843 t.Errorf("expected two elements in LOCAL_COMPATIBILITY_SUITE. got %d", len(compatEntries))
844 }
845 if compatEntries[0] != "suite_1" {
846 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_1`,"+
847 " but was '%s'", compatEntries[0])
848 }
849 if compatEntries[1] != "suite_2" {
850 t.Errorf("expected LOCAL_COMPATIBILITY_SUITE to be`suite_2`,"+
851 " but was '%s'", compatEntries[1])
852 }
853}
854
Jooyung Han0302a842019-10-30 18:43:49 +0900855func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400856 t.Parallel()
Jooyung Han2216fb12019-11-06 16:46:15 +0900857 ctx := testCcNoVndk(t, `
Jooyung Han0302a842019-10-30 18:43:49 +0900858 cc_library {
859 name: "libvndk",
860 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900861 product_available: true,
Jooyung Han0302a842019-10-30 18:43:49 +0900862 vndk: {
863 enabled: true,
864 },
865 nocrt: true,
866 }
Justin Yun8a2600c2020-12-07 12:44:03 +0900867 cc_library {
868 name: "libvndk-private",
Justin Yunc0d8c492021-01-07 17:45:31 +0900869 vendor_available: true,
870 product_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900871 vndk: {
872 enabled: true,
Justin Yunc0d8c492021-01-07 17:45:31 +0900873 private: true,
Justin Yun8a2600c2020-12-07 12:44:03 +0900874 },
875 nocrt: true,
876 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800877
878 cc_library {
879 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -0700880 llndk: {
881 symbol_file: "libllndk.map.txt",
882 export_llndk_headers: ["libllndk_headers"],
883 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800884 }
885
Colin Cross627280f2021-04-26 16:53:58 -0700886 cc_library_headers {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800887 name: "libllndk_headers",
Colin Cross627280f2021-04-26 16:53:58 -0700888 llndk: {
889 symbol_file: "libllndk.map.txt",
890 },
Colin Crossb5f6fa62021-01-06 17:05:04 -0800891 export_include_dirs: ["include"],
892 }
Jooyung Han2216fb12019-11-06 16:46:15 +0900893 `)
Jooyung Han0302a842019-10-30 18:43:49 +0900894
895 checkVndkOutput(t, ctx, "vndk/vndk.libraries.txt", []string{
896 "LLNDK: libc.so",
897 "LLNDK: libdl.so",
898 "LLNDK: libft2.so",
Colin Crossb5f6fa62021-01-06 17:05:04 -0800899 "LLNDK: libllndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900900 "LLNDK: libm.so",
901 "VNDK-SP: libc++.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900902 "VNDK-core: libvndk-private.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900903 "VNDK-core: libvndk.so",
904 "VNDK-private: libft2.so",
Justin Yun8a2600c2020-12-07 12:44:03 +0900905 "VNDK-private: libvndk-private.so",
906 "VNDK-product: libc++.so",
907 "VNDK-product: libvndk-private.so",
908 "VNDK-product: libvndk.so",
Jooyung Han0302a842019-10-30 18:43:49 +0900909 })
Logan Chienf3511742017-10-31 18:04:35 +0800910}
911
Justin Yun63e9ec72020-10-29 16:49:43 +0900912func TestVndkModuleError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400913 t.Parallel()
Justin Yun63e9ec72020-10-29 16:49:43 +0900914 // Check the error message for vendor_available and product_available properties.
Justin Yunc0d8c492021-01-07 17:45:31 +0900915 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900916 cc_library {
917 name: "libvndk",
918 vndk: {
919 enabled: true,
920 },
921 nocrt: true,
922 }
923 `)
924
Justin Yunc0d8c492021-01-07 17:45:31 +0900925 testCcErrorProductVndk(t, "vndk: vendor_available must be set to true when `vndk: {enabled: true}`", `
Justin Yun6977e8a2020-10-29 18:24:11 +0900926 cc_library {
927 name: "libvndk",
928 product_available: true,
929 vndk: {
930 enabled: true,
931 },
932 nocrt: true,
933 }
934 `)
935
Justin Yun6977e8a2020-10-29 18:24:11 +0900936 testCcErrorProductVndk(t, "product properties must have the same values with the vendor properties for VNDK modules", `
937 cc_library {
938 name: "libvndkprop",
939 vendor_available: true,
940 product_available: true,
941 vndk: {
942 enabled: true,
943 },
944 nocrt: true,
945 target: {
946 vendor: {
947 cflags: ["-DTEST",],
948 },
949 },
950 }
951 `)
Justin Yun63e9ec72020-10-29 16:49:43 +0900952}
953
Logan Chiend3c59a22018-03-29 14:08:15 +0800954func TestVndkDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -0400955 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +0800956 // Check whether an error is emitted when a VNDK lib depends on a system lib.
957 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
958 cc_library {
959 name: "libvndk",
960 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900961 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800962 vndk: {
963 enabled: true,
964 },
965 shared_libs: ["libfwk"], // Cause error
966 nocrt: true,
967 }
968
969 cc_library {
970 name: "libfwk",
971 nocrt: true,
972 }
973 `)
974
975 // Check whether an error is emitted when a VNDK lib depends on a vendor lib.
976 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
977 cc_library {
978 name: "libvndk",
979 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +0900980 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +0800981 vndk: {
982 enabled: true,
983 },
984 shared_libs: ["libvendor"], // Cause error
985 nocrt: true,
986 }
987
988 cc_library {
989 name: "libvendor",
990 vendor: true,
991 nocrt: true,
992 }
993 `)
994
995 // Check whether an error is emitted when a VNDK-SP lib depends on a system lib.
996 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
997 cc_library {
998 name: "libvndk_sp",
999 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001000 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001001 vndk: {
1002 enabled: true,
1003 support_system_process: true,
1004 },
1005 shared_libs: ["libfwk"], // Cause error
1006 nocrt: true,
1007 }
1008
1009 cc_library {
1010 name: "libfwk",
1011 nocrt: true,
1012 }
1013 `)
1014
1015 // Check whether an error is emitted when a VNDK-SP lib depends on a vendor lib.
1016 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1017 cc_library {
1018 name: "libvndk_sp",
1019 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001020 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001021 vndk: {
1022 enabled: true,
1023 support_system_process: true,
1024 },
1025 shared_libs: ["libvendor"], // Cause error
1026 nocrt: true,
1027 }
1028
1029 cc_library {
1030 name: "libvendor",
1031 vendor: true,
1032 nocrt: true,
1033 }
1034 `)
1035
1036 // Check whether an error is emitted when a VNDK-SP lib depends on a VNDK lib.
1037 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1038 cc_library {
1039 name: "libvndk_sp",
1040 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001041 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001042 vndk: {
1043 enabled: true,
1044 support_system_process: true,
1045 },
1046 shared_libs: ["libvndk"], // Cause error
1047 nocrt: true,
1048 }
1049
1050 cc_library {
1051 name: "libvndk",
1052 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001053 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001054 vndk: {
1055 enabled: true,
1056 },
1057 nocrt: true,
1058 }
1059 `)
Jooyung Hana70f0672019-01-18 15:20:43 +09001060
1061 // Check whether an error is emitted when a VNDK lib depends on a non-VNDK lib.
1062 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1063 cc_library {
1064 name: "libvndk",
1065 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001066 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001067 vndk: {
1068 enabled: true,
1069 },
1070 shared_libs: ["libnonvndk"],
1071 nocrt: true,
1072 }
1073
1074 cc_library {
1075 name: "libnonvndk",
1076 vendor_available: true,
1077 nocrt: true,
1078 }
1079 `)
1080
1081 // Check whether an error is emitted when a VNDK-private lib depends on a non-VNDK lib.
1082 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1083 cc_library {
1084 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001085 vendor_available: true,
1086 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001087 vndk: {
1088 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001089 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001090 },
1091 shared_libs: ["libnonvndk"],
1092 nocrt: true,
1093 }
1094
1095 cc_library {
1096 name: "libnonvndk",
1097 vendor_available: true,
1098 nocrt: true,
1099 }
1100 `)
1101
1102 // Check whether an error is emitted when a VNDK-sp lib depends on a non-VNDK lib.
1103 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1104 cc_library {
1105 name: "libvndksp",
1106 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001107 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001108 vndk: {
1109 enabled: true,
1110 support_system_process: true,
1111 },
1112 shared_libs: ["libnonvndk"],
1113 nocrt: true,
1114 }
1115
1116 cc_library {
1117 name: "libnonvndk",
1118 vendor_available: true,
1119 nocrt: true,
1120 }
1121 `)
1122
1123 // Check whether an error is emitted when a VNDK-sp-private lib depends on a non-VNDK lib.
1124 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1125 cc_library {
1126 name: "libvndkspprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09001127 vendor_available: true,
1128 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001129 vndk: {
1130 enabled: true,
1131 support_system_process: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001132 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001133 },
1134 shared_libs: ["libnonvndk"],
1135 nocrt: true,
1136 }
1137
1138 cc_library {
1139 name: "libnonvndk",
1140 vendor_available: true,
1141 nocrt: true,
1142 }
1143 `)
1144}
1145
1146func TestDoubleLoadbleDep(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001147 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001148 // okay to link : LLNDK -> double_loadable VNDK
1149 testCc(t, `
1150 cc_library {
1151 name: "libllndk",
1152 shared_libs: ["libdoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001153 llndk: {
1154 symbol_file: "libllndk.map.txt",
1155 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001156 }
1157
1158 cc_library {
1159 name: "libdoubleloadable",
1160 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001161 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001162 vndk: {
1163 enabled: true,
1164 },
1165 double_loadable: true,
1166 }
1167 `)
1168 // okay to link : LLNDK -> VNDK-SP
1169 testCc(t, `
1170 cc_library {
1171 name: "libllndk",
1172 shared_libs: ["libvndksp"],
Colin Cross203b4212021-04-26 17:19:41 -07001173 llndk: {
1174 symbol_file: "libllndk.map.txt",
1175 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001176 }
1177
1178 cc_library {
1179 name: "libvndksp",
1180 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001181 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001182 vndk: {
1183 enabled: true,
1184 support_system_process: true,
1185 },
1186 }
1187 `)
1188 // okay to link : double_loadable -> double_loadable
1189 testCc(t, `
1190 cc_library {
1191 name: "libdoubleloadable1",
1192 shared_libs: ["libdoubleloadable2"],
1193 vendor_available: true,
1194 double_loadable: true,
1195 }
1196
1197 cc_library {
1198 name: "libdoubleloadable2",
1199 vendor_available: true,
1200 double_loadable: true,
1201 }
1202 `)
1203 // okay to link : double_loadable VNDK -> double_loadable VNDK private
1204 testCc(t, `
1205 cc_library {
1206 name: "libdoubleloadable",
1207 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001208 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001209 vndk: {
1210 enabled: true,
1211 },
1212 double_loadable: true,
1213 shared_libs: ["libnondoubleloadable"],
1214 }
1215
1216 cc_library {
1217 name: "libnondoubleloadable",
Justin Yunfd9e8042020-12-23 18:23:14 +09001218 vendor_available: true,
1219 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001220 vndk: {
1221 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001222 private: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001223 },
1224 double_loadable: true,
1225 }
1226 `)
1227 // okay to link : LLNDK -> core-only -> vendor_available & double_loadable
1228 testCc(t, `
1229 cc_library {
1230 name: "libllndk",
1231 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001232 llndk: {
1233 symbol_file: "libllndk.map.txt",
1234 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001235 }
1236
1237 cc_library {
1238 name: "libcoreonly",
1239 shared_libs: ["libvendoravailable"],
1240 }
1241
1242 // indirect dependency of LLNDK
1243 cc_library {
1244 name: "libvendoravailable",
1245 vendor_available: true,
1246 double_loadable: true,
1247 }
1248 `)
1249}
1250
1251func TestDoubleLoadableDepError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001252 t.Parallel()
Jooyung Hana70f0672019-01-18 15:20:43 +09001253 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable VNDK lib.
1254 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1255 cc_library {
1256 name: "libllndk",
1257 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001258 llndk: {
1259 symbol_file: "libllndk.map.txt",
1260 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001261 }
1262
1263 cc_library {
1264 name: "libnondoubleloadable",
1265 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001266 product_available: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001267 vndk: {
1268 enabled: true,
1269 },
1270 }
1271 `)
1272
1273 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable vendor_available lib.
1274 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1275 cc_library {
1276 name: "libllndk",
Yi Konge7fe9912019-06-02 00:53:50 -07001277 no_libcrt: true,
Jooyung Hana70f0672019-01-18 15:20:43 +09001278 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001279 llndk: {
1280 symbol_file: "libllndk.map.txt",
1281 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001282 }
1283
1284 cc_library {
1285 name: "libnondoubleloadable",
1286 vendor_available: true,
1287 }
1288 `)
1289
Jooyung Hana70f0672019-01-18 15:20:43 +09001290 // Check whether an error is emitted when a LLNDK depends on a non-double_loadable indirectly.
1291 testCcError(t, "module \".*\" variant \".*\": link.* \".*\" which is not LL-NDK, VNDK-SP, .*double_loadable", `
1292 cc_library {
1293 name: "libllndk",
1294 shared_libs: ["libcoreonly"],
Colin Cross203b4212021-04-26 17:19:41 -07001295 llndk: {
1296 symbol_file: "libllndk.map.txt",
1297 }
Jooyung Hana70f0672019-01-18 15:20:43 +09001298 }
1299
1300 cc_library {
1301 name: "libcoreonly",
1302 shared_libs: ["libvendoravailable"],
1303 }
1304
1305 // indirect dependency of LLNDK
1306 cc_library {
1307 name: "libvendoravailable",
1308 vendor_available: true,
1309 }
1310 `)
Jiyong Park0474e1f2021-01-14 14:26:06 +09001311
1312 // The error is not from 'client' but from 'libllndk'
1313 testCcError(t, "module \"libllndk\".* links a library \"libnondoubleloadable\".*double_loadable", `
1314 cc_library {
1315 name: "client",
1316 vendor_available: true,
1317 double_loadable: true,
1318 shared_libs: ["libllndk"],
1319 }
1320 cc_library {
1321 name: "libllndk",
1322 shared_libs: ["libnondoubleloadable"],
Colin Cross203b4212021-04-26 17:19:41 -07001323 llndk: {
1324 symbol_file: "libllndk.map.txt",
1325 }
Jiyong Park0474e1f2021-01-14 14:26:06 +09001326 }
1327 cc_library {
1328 name: "libnondoubleloadable",
1329 vendor_available: true,
1330 }
1331 `)
Logan Chiend3c59a22018-03-29 14:08:15 +08001332}
1333
Jooyung Han479ca172020-10-19 18:51:07 +09001334func TestCheckVndkMembershipBeforeDoubleLoadable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001335 t.Parallel()
Jooyung Han479ca172020-10-19 18:51:07 +09001336 testCcError(t, "module \"libvndksp\" variant .*: .*: VNDK-SP must only depend on VNDK-SP", `
1337 cc_library {
1338 name: "libvndksp",
1339 shared_libs: ["libanothervndksp"],
1340 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001341 product_available: true,
Jooyung Han479ca172020-10-19 18:51:07 +09001342 vndk: {
1343 enabled: true,
1344 support_system_process: true,
1345 }
1346 }
1347
1348 cc_library {
1349 name: "libllndk",
1350 shared_libs: ["libanothervndksp"],
1351 }
1352
Jooyung Han479ca172020-10-19 18:51:07 +09001353 cc_library {
1354 name: "libanothervndksp",
1355 vendor_available: true,
1356 }
1357 `)
1358}
1359
Logan Chienf3511742017-10-31 18:04:35 +08001360func TestVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001361 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001362 // This test checks the VNDK-Ext properties.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001363 bp := `
Logan Chienf3511742017-10-31 18:04:35 +08001364 cc_library {
1365 name: "libvndk",
1366 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001367 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001368 vndk: {
1369 enabled: true,
1370 },
1371 nocrt: true,
1372 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001373 cc_library {
1374 name: "libvndk2",
1375 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001376 product_available: true,
Jooyung Han4c2b9422019-10-22 19:53:47 +09001377 vndk: {
1378 enabled: true,
1379 },
1380 target: {
1381 vendor: {
1382 suffix: "-suffix",
1383 },
Justin Yun63e9ec72020-10-29 16:49:43 +09001384 product: {
1385 suffix: "-suffix",
1386 },
Jooyung Han4c2b9422019-10-22 19:53:47 +09001387 },
1388 nocrt: true,
1389 }
Logan Chienf3511742017-10-31 18:04:35 +08001390
1391 cc_library {
1392 name: "libvndk_ext",
1393 vendor: true,
1394 vndk: {
1395 enabled: true,
1396 extends: "libvndk",
1397 },
1398 nocrt: true,
1399 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001400
1401 cc_library {
1402 name: "libvndk2_ext",
1403 vendor: true,
1404 vndk: {
1405 enabled: true,
1406 extends: "libvndk2",
1407 },
1408 nocrt: true,
1409 }
Logan Chienf3511742017-10-31 18:04:35 +08001410
Justin Yun0ecf0b22020-02-28 15:07:59 +09001411 cc_library {
1412 name: "libvndk_ext_product",
1413 product_specific: true,
1414 vndk: {
1415 enabled: true,
1416 extends: "libvndk",
1417 },
1418 nocrt: true,
1419 }
Jooyung Han4c2b9422019-10-22 19:53:47 +09001420
Justin Yun0ecf0b22020-02-28 15:07:59 +09001421 cc_library {
1422 name: "libvndk2_ext_product",
1423 product_specific: true,
1424 vndk: {
1425 enabled: true,
1426 extends: "libvndk2",
1427 },
1428 nocrt: true,
1429 }
1430 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001431 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001432 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1433 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001434 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001435
1436 ctx := testCcWithConfig(t, config)
1437
1438 checkVndkModule(t, ctx, "libvndk_ext", "vndk", false, "libvndk", vendorVariant)
1439 checkVndkModule(t, ctx, "libvndk_ext_product", "vndk", false, "libvndk", productVariant)
1440
1441 mod_vendor := ctx.ModuleForTests("libvndk2_ext", vendorVariant).Module().(*Module)
1442 assertString(t, mod_vendor.outputFile.Path().Base(), "libvndk2-suffix.so")
1443
1444 mod_product := ctx.ModuleForTests("libvndk2_ext_product", productVariant).Module().(*Module)
1445 assertString(t, mod_product.outputFile.Path().Base(), "libvndk2-suffix.so")
Logan Chienf3511742017-10-31 18:04:35 +08001446}
1447
Logan Chiend3c59a22018-03-29 14:08:15 +08001448func TestVndkExtWithoutBoardVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001449 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001450 // This test checks the VNDK-Ext properties when BOARD_VNDK_VERSION is not set.
1451 ctx := testCcNoVndk(t, `
1452 cc_library {
1453 name: "libvndk",
1454 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001455 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001456 vndk: {
1457 enabled: true,
1458 },
1459 nocrt: true,
1460 }
1461
1462 cc_library {
1463 name: "libvndk_ext",
1464 vendor: true,
1465 vndk: {
1466 enabled: true,
1467 extends: "libvndk",
1468 },
1469 nocrt: true,
1470 }
1471 `)
1472
1473 // Ensures that the core variant of "libvndk_ext" can be found.
1474 mod := ctx.ModuleForTests("libvndk_ext", coreVariant).Module().(*Module)
1475 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1476 t.Errorf("\"libvndk_ext\" must extend from \"libvndk\" but get %q", extends)
1477 }
1478}
1479
Justin Yun0ecf0b22020-02-28 15:07:59 +09001480func TestVndkExtWithoutProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001481 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001482 // This test checks the VNDK-Ext properties when PRODUCT_PRODUCT_VNDK_VERSION is not set.
Justin Yun8a2600c2020-12-07 12:44:03 +09001483 ctx := testCcNoProductVndk(t, `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001484 cc_library {
1485 name: "libvndk",
1486 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001487 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001488 vndk: {
1489 enabled: true,
1490 },
1491 nocrt: true,
1492 }
1493
1494 cc_library {
1495 name: "libvndk_ext_product",
1496 product_specific: true,
1497 vndk: {
1498 enabled: true,
1499 extends: "libvndk",
1500 },
1501 nocrt: true,
1502 }
1503 `)
1504
1505 // Ensures that the core variant of "libvndk_ext_product" can be found.
1506 mod := ctx.ModuleForTests("libvndk_ext_product", coreVariant).Module().(*Module)
1507 if extends := mod.getVndkExtendsModuleName(); extends != "libvndk" {
1508 t.Errorf("\"libvndk_ext_product\" must extend from \"libvndk\" but get %q", extends)
1509 }
1510}
1511
Logan Chienf3511742017-10-31 18:04:35 +08001512func TestVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001513 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001514 // This test ensures an error is emitted in ill-formed vndk-ext definition.
Justin Yun0ecf0b22020-02-28 15:07:59 +09001515 testCcError(t, "must set `vendor: true` or `product_specific: true` to set `extends: \".*\"`", `
Logan Chienf3511742017-10-31 18:04:35 +08001516 cc_library {
1517 name: "libvndk",
1518 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001519 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001520 vndk: {
1521 enabled: true,
1522 },
1523 nocrt: true,
1524 }
1525
1526 cc_library {
1527 name: "libvndk_ext",
1528 vndk: {
1529 enabled: true,
1530 extends: "libvndk",
1531 },
1532 nocrt: true,
1533 }
1534 `)
1535
1536 testCcError(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1537 cc_library {
1538 name: "libvndk",
1539 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001540 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001541 vndk: {
1542 enabled: true,
1543 },
1544 nocrt: true,
1545 }
1546
1547 cc_library {
1548 name: "libvndk_ext",
1549 vendor: true,
1550 vndk: {
1551 enabled: true,
1552 },
1553 nocrt: true,
1554 }
1555 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001556
1557 testCcErrorProductVndk(t, "must set `extends: \"\\.\\.\\.\"` to vndk extension", `
1558 cc_library {
1559 name: "libvndk",
1560 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001561 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001562 vndk: {
1563 enabled: true,
1564 },
1565 nocrt: true,
1566 }
1567
1568 cc_library {
1569 name: "libvndk_ext_product",
1570 product_specific: true,
1571 vndk: {
1572 enabled: true,
1573 },
1574 nocrt: true,
1575 }
1576 `)
1577
1578 testCcErrorProductVndk(t, "must not set at the same time as `vndk: {extends: \"\\.\\.\\.\"}`", `
1579 cc_library {
1580 name: "libvndk",
1581 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001582 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001583 vndk: {
1584 enabled: true,
1585 },
1586 nocrt: true,
1587 }
1588
1589 cc_library {
1590 name: "libvndk_ext_product",
1591 product_specific: true,
1592 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001593 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001594 vndk: {
1595 enabled: true,
1596 extends: "libvndk",
1597 },
1598 nocrt: true,
1599 }
1600 `)
Logan Chienf3511742017-10-31 18:04:35 +08001601}
1602
1603func TestVndkExtInconsistentSupportSystemProcessError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001604 t.Parallel()
Logan Chienf3511742017-10-31 18:04:35 +08001605 // This test ensures an error is emitted for inconsistent support_system_process.
1606 testCcError(t, "module \".*\" with mismatched support_system_process", `
1607 cc_library {
1608 name: "libvndk",
1609 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001610 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001611 vndk: {
1612 enabled: true,
1613 },
1614 nocrt: true,
1615 }
1616
1617 cc_library {
1618 name: "libvndk_sp_ext",
1619 vendor: true,
1620 vndk: {
1621 enabled: true,
1622 extends: "libvndk",
1623 support_system_process: true,
1624 },
1625 nocrt: true,
1626 }
1627 `)
1628
1629 testCcError(t, "module \".*\" with mismatched support_system_process", `
1630 cc_library {
1631 name: "libvndk_sp",
1632 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001633 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001634 vndk: {
1635 enabled: true,
1636 support_system_process: true,
1637 },
1638 nocrt: true,
1639 }
1640
1641 cc_library {
1642 name: "libvndk_ext",
1643 vendor: true,
1644 vndk: {
1645 enabled: true,
1646 extends: "libvndk_sp",
1647 },
1648 nocrt: true,
1649 }
1650 `)
1651}
1652
1653func TestVndkExtVendorAvailableFalseError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001654 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001655 // This test ensures an error is emitted when a VNDK-Ext library extends a VNDK library
Justin Yunfd9e8042020-12-23 18:23:14 +09001656 // with `private: true`.
1657 testCcError(t, "`extends` refers module \".*\" which has `private: true`", `
Logan Chienf3511742017-10-31 18:04:35 +08001658 cc_library {
1659 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001660 vendor_available: true,
1661 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001662 vndk: {
1663 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001664 private: true,
Logan Chienf3511742017-10-31 18:04:35 +08001665 },
1666 nocrt: true,
1667 }
1668
1669 cc_library {
1670 name: "libvndk_ext",
1671 vendor: true,
1672 vndk: {
1673 enabled: true,
1674 extends: "libvndk",
1675 },
1676 nocrt: true,
1677 }
1678 `)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001679
Justin Yunfd9e8042020-12-23 18:23:14 +09001680 testCcErrorProductVndk(t, "`extends` refers module \".*\" which has `private: true`", `
Justin Yun0ecf0b22020-02-28 15:07:59 +09001681 cc_library {
1682 name: "libvndk",
Justin Yunfd9e8042020-12-23 18:23:14 +09001683 vendor_available: true,
1684 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001685 vndk: {
1686 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09001687 private: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001688 },
1689 nocrt: true,
1690 }
1691
1692 cc_library {
1693 name: "libvndk_ext_product",
1694 product_specific: true,
1695 vndk: {
1696 enabled: true,
1697 extends: "libvndk",
1698 },
1699 nocrt: true,
1700 }
1701 `)
Logan Chienf3511742017-10-31 18:04:35 +08001702}
1703
Logan Chiend3c59a22018-03-29 14:08:15 +08001704func TestVendorModuleUseVndkExt(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001705 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001706 // This test ensures a vendor module can depend on a VNDK-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001707 testCc(t, `
1708 cc_library {
1709 name: "libvndk",
1710 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001711 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001712 vndk: {
1713 enabled: true,
1714 },
1715 nocrt: true,
1716 }
1717
1718 cc_library {
1719 name: "libvndk_ext",
1720 vendor: true,
1721 vndk: {
1722 enabled: true,
1723 extends: "libvndk",
1724 },
1725 nocrt: true,
1726 }
1727
1728 cc_library {
Logan Chienf3511742017-10-31 18:04:35 +08001729 name: "libvndk_sp",
1730 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001731 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001732 vndk: {
1733 enabled: true,
1734 support_system_process: true,
1735 },
1736 nocrt: true,
1737 }
1738
1739 cc_library {
1740 name: "libvndk_sp_ext",
1741 vendor: true,
1742 vndk: {
1743 enabled: true,
1744 extends: "libvndk_sp",
1745 support_system_process: true,
1746 },
1747 nocrt: true,
1748 }
1749
1750 cc_library {
1751 name: "libvendor",
1752 vendor: true,
1753 shared_libs: ["libvndk_ext", "libvndk_sp_ext"],
1754 nocrt: true,
1755 }
1756 `)
1757}
1758
Logan Chiend3c59a22018-03-29 14:08:15 +08001759func TestVndkExtUseVendorLib(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001760 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001761 // This test ensures a VNDK-Ext library can depend on a vendor library.
Logan Chienf3511742017-10-31 18:04:35 +08001762 testCc(t, `
1763 cc_library {
1764 name: "libvndk",
1765 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001766 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001767 vndk: {
1768 enabled: true,
1769 },
1770 nocrt: true,
1771 }
1772
1773 cc_library {
1774 name: "libvndk_ext",
1775 vendor: true,
1776 vndk: {
1777 enabled: true,
1778 extends: "libvndk",
1779 },
1780 shared_libs: ["libvendor"],
1781 nocrt: true,
1782 }
1783
1784 cc_library {
1785 name: "libvendor",
1786 vendor: true,
1787 nocrt: true,
1788 }
1789 `)
Logan Chienf3511742017-10-31 18:04:35 +08001790
Logan Chiend3c59a22018-03-29 14:08:15 +08001791 // This test ensures a VNDK-SP-Ext library can depend on a vendor library.
1792 testCc(t, `
Logan Chienf3511742017-10-31 18:04:35 +08001793 cc_library {
1794 name: "libvndk_sp",
1795 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001796 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001797 vndk: {
1798 enabled: true,
1799 support_system_process: true,
1800 },
1801 nocrt: true,
1802 }
1803
1804 cc_library {
1805 name: "libvndk_sp_ext",
1806 vendor: true,
1807 vndk: {
1808 enabled: true,
1809 extends: "libvndk_sp",
1810 support_system_process: true,
1811 },
1812 shared_libs: ["libvendor"], // Cause an error
1813 nocrt: true,
1814 }
1815
1816 cc_library {
1817 name: "libvendor",
1818 vendor: true,
1819 nocrt: true,
1820 }
1821 `)
1822}
1823
Justin Yun0ecf0b22020-02-28 15:07:59 +09001824func TestProductVndkExtDependency(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001825 t.Parallel()
Justin Yun0ecf0b22020-02-28 15:07:59 +09001826 bp := `
1827 cc_library {
1828 name: "libvndk",
1829 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001830 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001831 vndk: {
1832 enabled: true,
1833 },
1834 nocrt: true,
1835 }
1836
1837 cc_library {
1838 name: "libvndk_ext_product",
1839 product_specific: true,
1840 vndk: {
1841 enabled: true,
1842 extends: "libvndk",
1843 },
1844 shared_libs: ["libproduct_for_vndklibs"],
1845 nocrt: true,
1846 }
1847
1848 cc_library {
1849 name: "libvndk_sp",
1850 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001851 product_available: true,
Justin Yun0ecf0b22020-02-28 15:07:59 +09001852 vndk: {
1853 enabled: true,
1854 support_system_process: true,
1855 },
1856 nocrt: true,
1857 }
1858
1859 cc_library {
1860 name: "libvndk_sp_ext_product",
1861 product_specific: true,
1862 vndk: {
1863 enabled: true,
1864 extends: "libvndk_sp",
1865 support_system_process: true,
1866 },
1867 shared_libs: ["libproduct_for_vndklibs"],
1868 nocrt: true,
1869 }
1870
1871 cc_library {
1872 name: "libproduct",
1873 product_specific: true,
1874 shared_libs: ["libvndk_ext_product", "libvndk_sp_ext_product"],
1875 nocrt: true,
1876 }
1877
1878 cc_library {
1879 name: "libproduct_for_vndklibs",
1880 product_specific: true,
1881 nocrt: true,
1882 }
1883 `
Paul Duffinc3e6ce02021-03-22 23:21:32 +00001884 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Justin Yun0ecf0b22020-02-28 15:07:59 +09001885 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
1886 config.TestProductVariables.ProductVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09001887 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Justin Yun0ecf0b22020-02-28 15:07:59 +09001888
1889 testCcWithConfig(t, config)
1890}
1891
Logan Chiend3c59a22018-03-29 14:08:15 +08001892func TestVndkSpExtUseVndkError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001893 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001894 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK
1895 // library.
1896 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1897 cc_library {
1898 name: "libvndk",
1899 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001900 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001901 vndk: {
1902 enabled: true,
1903 },
1904 nocrt: true,
1905 }
1906
1907 cc_library {
1908 name: "libvndk_sp",
1909 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001910 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001911 vndk: {
1912 enabled: true,
1913 support_system_process: true,
1914 },
1915 nocrt: true,
1916 }
1917
1918 cc_library {
1919 name: "libvndk_sp_ext",
1920 vendor: true,
1921 vndk: {
1922 enabled: true,
1923 extends: "libvndk_sp",
1924 support_system_process: true,
1925 },
1926 shared_libs: ["libvndk"], // Cause an error
1927 nocrt: true,
1928 }
1929 `)
1930
1931 // This test ensures an error is emitted if a VNDK-SP-Ext library depends on a VNDK-Ext
1932 // library.
1933 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
1934 cc_library {
1935 name: "libvndk",
1936 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001937 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001938 vndk: {
1939 enabled: true,
1940 },
1941 nocrt: true,
1942 }
1943
1944 cc_library {
1945 name: "libvndk_ext",
1946 vendor: true,
1947 vndk: {
1948 enabled: true,
1949 extends: "libvndk",
1950 },
1951 nocrt: true,
1952 }
1953
1954 cc_library {
1955 name: "libvndk_sp",
1956 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001957 product_available: true,
Logan Chiend3c59a22018-03-29 14:08:15 +08001958 vndk: {
1959 enabled: true,
1960 support_system_process: true,
1961 },
1962 nocrt: true,
1963 }
1964
1965 cc_library {
1966 name: "libvndk_sp_ext",
1967 vendor: true,
1968 vndk: {
1969 enabled: true,
1970 extends: "libvndk_sp",
1971 support_system_process: true,
1972 },
1973 shared_libs: ["libvndk_ext"], // Cause an error
1974 nocrt: true,
1975 }
1976 `)
1977}
1978
1979func TestVndkUseVndkExtError(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04001980 t.Parallel()
Logan Chiend3c59a22018-03-29 14:08:15 +08001981 // This test ensures an error is emitted if a VNDK/VNDK-SP library depends on a
1982 // VNDK-Ext/VNDK-SP-Ext library.
Logan Chienf3511742017-10-31 18:04:35 +08001983 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
1984 cc_library {
1985 name: "libvndk",
1986 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09001987 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08001988 vndk: {
1989 enabled: true,
1990 },
1991 nocrt: true,
1992 }
1993
1994 cc_library {
1995 name: "libvndk_ext",
1996 vendor: true,
1997 vndk: {
1998 enabled: true,
1999 extends: "libvndk",
2000 },
2001 nocrt: true,
2002 }
2003
2004 cc_library {
2005 name: "libvndk2",
2006 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002007 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002008 vndk: {
2009 enabled: true,
2010 },
2011 shared_libs: ["libvndk_ext"],
2012 nocrt: true,
2013 }
2014 `)
2015
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002016 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002017 cc_library {
2018 name: "libvndk",
2019 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002020 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002021 vndk: {
2022 enabled: true,
2023 },
2024 nocrt: true,
2025 }
2026
2027 cc_library {
2028 name: "libvndk_ext",
2029 vendor: true,
2030 vndk: {
2031 enabled: true,
2032 extends: "libvndk",
2033 },
2034 nocrt: true,
2035 }
2036
2037 cc_library {
2038 name: "libvndk2",
2039 vendor_available: true,
2040 vndk: {
2041 enabled: true,
2042 },
2043 target: {
2044 vendor: {
2045 shared_libs: ["libvndk_ext"],
2046 },
2047 },
2048 nocrt: true,
2049 }
2050 `)
2051
2052 testCcError(t, "dependency \".*\" of \".*\" missing variant", `
2053 cc_library {
2054 name: "libvndk_sp",
2055 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002056 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002057 vndk: {
2058 enabled: true,
2059 support_system_process: true,
2060 },
2061 nocrt: true,
2062 }
2063
2064 cc_library {
2065 name: "libvndk_sp_ext",
2066 vendor: true,
2067 vndk: {
2068 enabled: true,
2069 extends: "libvndk_sp",
2070 support_system_process: true,
2071 },
2072 nocrt: true,
2073 }
2074
2075 cc_library {
2076 name: "libvndk_sp_2",
2077 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002078 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002079 vndk: {
2080 enabled: true,
2081 support_system_process: true,
2082 },
2083 shared_libs: ["libvndk_sp_ext"],
2084 nocrt: true,
2085 }
2086 `)
2087
Martin Stjernholmef449fe2018-11-06 16:12:13 +00002088 testCcError(t, "module \".*\" variant \".*\": \\(.*\\) should not link to \".*\"", `
Logan Chienf3511742017-10-31 18:04:35 +08002089 cc_library {
2090 name: "libvndk_sp",
2091 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002092 product_available: true,
Logan Chienf3511742017-10-31 18:04:35 +08002093 vndk: {
2094 enabled: true,
2095 },
2096 nocrt: true,
2097 }
2098
2099 cc_library {
2100 name: "libvndk_sp_ext",
2101 vendor: true,
2102 vndk: {
2103 enabled: true,
2104 extends: "libvndk_sp",
2105 },
2106 nocrt: true,
2107 }
2108
2109 cc_library {
2110 name: "libvndk_sp2",
2111 vendor_available: true,
2112 vndk: {
2113 enabled: true,
2114 },
2115 target: {
2116 vendor: {
2117 shared_libs: ["libvndk_sp_ext"],
2118 },
2119 },
2120 nocrt: true,
2121 }
2122 `)
2123}
2124
Justin Yun5f7f7e82019-11-18 19:52:14 +09002125func TestEnforceProductVndkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002126 t.Parallel()
Justin Yun5f7f7e82019-11-18 19:52:14 +09002127 bp := `
2128 cc_library {
2129 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002130 llndk: {
2131 symbol_file: "libllndk.map.txt",
2132 }
Justin Yun5f7f7e82019-11-18 19:52:14 +09002133 }
2134 cc_library {
2135 name: "libvndk",
2136 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002137 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002138 vndk: {
2139 enabled: true,
2140 },
2141 nocrt: true,
2142 }
2143 cc_library {
2144 name: "libvndk_sp",
2145 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002146 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002147 vndk: {
2148 enabled: true,
2149 support_system_process: true,
2150 },
2151 nocrt: true,
2152 }
2153 cc_library {
2154 name: "libva",
2155 vendor_available: true,
2156 nocrt: true,
2157 }
2158 cc_library {
Justin Yun63e9ec72020-10-29 16:49:43 +09002159 name: "libpa",
2160 product_available: true,
2161 nocrt: true,
2162 }
2163 cc_library {
Justin Yun6977e8a2020-10-29 18:24:11 +09002164 name: "libboth_available",
2165 vendor_available: true,
2166 product_available: true,
2167 nocrt: true,
Justin Yun13decfb2021-03-08 19:25:55 +09002168 srcs: ["foo.c"],
Justin Yun6977e8a2020-10-29 18:24:11 +09002169 target: {
2170 vendor: {
2171 suffix: "-vendor",
2172 },
2173 product: {
2174 suffix: "-product",
2175 },
2176 }
2177 }
2178 cc_library {
Justin Yun5f7f7e82019-11-18 19:52:14 +09002179 name: "libproduct_va",
2180 product_specific: true,
2181 vendor_available: true,
2182 nocrt: true,
2183 }
2184 cc_library {
2185 name: "libprod",
2186 product_specific: true,
2187 shared_libs: [
2188 "libllndk",
2189 "libvndk",
2190 "libvndk_sp",
Justin Yun63e9ec72020-10-29 16:49:43 +09002191 "libpa",
Justin Yun6977e8a2020-10-29 18:24:11 +09002192 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002193 "libproduct_va",
2194 ],
2195 nocrt: true,
2196 }
2197 cc_library {
2198 name: "libvendor",
2199 vendor: true,
2200 shared_libs: [
2201 "libllndk",
2202 "libvndk",
2203 "libvndk_sp",
2204 "libva",
Justin Yun6977e8a2020-10-29 18:24:11 +09002205 "libboth_available",
Justin Yun5f7f7e82019-11-18 19:52:14 +09002206 "libproduct_va",
2207 ],
2208 nocrt: true,
2209 }
2210 `
2211
Paul Duffin8567f222021-03-23 00:02:06 +00002212 ctx := prepareForCcTest.RunTestWithBp(t, bp).TestContext
Justin Yun5f7f7e82019-11-18 19:52:14 +09002213
Jooyung Han261e1582020-10-20 18:54:21 +09002214 checkVndkModule(t, ctx, "libvndk", "", false, "", productVariant)
2215 checkVndkModule(t, ctx, "libvndk_sp", "", true, "", productVariant)
Justin Yun6977e8a2020-10-29 18:24:11 +09002216
2217 mod_vendor := ctx.ModuleForTests("libboth_available", vendorVariant).Module().(*Module)
2218 assertString(t, mod_vendor.outputFile.Path().Base(), "libboth_available-vendor.so")
2219
2220 mod_product := ctx.ModuleForTests("libboth_available", productVariant).Module().(*Module)
2221 assertString(t, mod_product.outputFile.Path().Base(), "libboth_available-product.so")
Justin Yun13decfb2021-03-08 19:25:55 +09002222
2223 ensureStringContains := func(t *testing.T, str string, substr string) {
2224 t.Helper()
2225 if !strings.Contains(str, substr) {
2226 t.Errorf("%q is not found in %v", substr, str)
2227 }
2228 }
2229 ensureStringNotContains := func(t *testing.T, str string, substr string) {
2230 t.Helper()
2231 if strings.Contains(str, substr) {
2232 t.Errorf("%q is found in %v", substr, str)
2233 }
2234 }
2235
2236 // _static variant is used since _shared reuses *.o from the static variant
2237 vendor_static := ctx.ModuleForTests("libboth_available", strings.Replace(vendorVariant, "_shared", "_static", 1))
2238 product_static := ctx.ModuleForTests("libboth_available", strings.Replace(productVariant, "_shared", "_static", 1))
2239
2240 vendor_cflags := vendor_static.Rule("cc").Args["cFlags"]
2241 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VNDK__")
2242 ensureStringContains(t, vendor_cflags, "-D__ANDROID_VENDOR__")
2243 ensureStringNotContains(t, vendor_cflags, "-D__ANDROID_PRODUCT__")
2244
2245 product_cflags := product_static.Rule("cc").Args["cFlags"]
2246 ensureStringContains(t, product_cflags, "-D__ANDROID_VNDK__")
2247 ensureStringContains(t, product_cflags, "-D__ANDROID_PRODUCT__")
2248 ensureStringNotContains(t, product_cflags, "-D__ANDROID_VENDOR__")
Justin Yun5f7f7e82019-11-18 19:52:14 +09002249}
2250
2251func TestEnforceProductVndkVersionErrors(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002252 t.Parallel()
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002253 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002254 cc_library {
2255 name: "libprod",
2256 product_specific: true,
2257 shared_libs: [
2258 "libvendor",
2259 ],
2260 nocrt: true,
2261 }
2262 cc_library {
2263 name: "libvendor",
2264 vendor: true,
2265 nocrt: true,
2266 }
2267 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002268 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002269 cc_library {
2270 name: "libprod",
2271 product_specific: true,
2272 shared_libs: [
2273 "libsystem",
2274 ],
2275 nocrt: true,
2276 }
2277 cc_library {
2278 name: "libsystem",
2279 nocrt: true,
2280 }
2281 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002282 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun6977e8a2020-10-29 18:24:11 +09002283 cc_library {
2284 name: "libprod",
2285 product_specific: true,
2286 shared_libs: [
2287 "libva",
2288 ],
2289 nocrt: true,
2290 }
2291 cc_library {
2292 name: "libva",
2293 vendor_available: true,
2294 nocrt: true,
2295 }
2296 `)
Justin Yunfd9e8042020-12-23 18:23:14 +09002297 testCcErrorProductVndk(t, "non-VNDK module should not link to \".*\" which has `private: true`", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002298 cc_library {
2299 name: "libprod",
2300 product_specific: true,
2301 shared_libs: [
2302 "libvndk_private",
2303 ],
2304 nocrt: true,
2305 }
2306 cc_library {
2307 name: "libvndk_private",
Justin Yunfd9e8042020-12-23 18:23:14 +09002308 vendor_available: true,
2309 product_available: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002310 vndk: {
2311 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002312 private: true,
Justin Yun5f7f7e82019-11-18 19:52:14 +09002313 },
2314 nocrt: true,
2315 }
2316 `)
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002317 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:product.29", `
Justin Yun5f7f7e82019-11-18 19:52:14 +09002318 cc_library {
2319 name: "libprod",
2320 product_specific: true,
2321 shared_libs: [
2322 "libsystem_ext",
2323 ],
2324 nocrt: true,
2325 }
2326 cc_library {
2327 name: "libsystem_ext",
2328 system_ext_specific: true,
2329 nocrt: true,
2330 }
2331 `)
2332 testCcErrorProductVndk(t, "dependency \".*\" of \".*\" missing variant:\n.*image:", `
2333 cc_library {
2334 name: "libsystem",
2335 shared_libs: [
2336 "libproduct_va",
2337 ],
2338 nocrt: true,
2339 }
2340 cc_library {
2341 name: "libproduct_va",
2342 product_specific: true,
2343 vendor_available: true,
2344 nocrt: true,
2345 }
2346 `)
2347}
2348
Jooyung Han38002912019-05-16 04:01:54 +09002349func TestMakeLinkType(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002350 t.Parallel()
Colin Cross98be1bb2019-12-13 20:41:13 -08002351 bp := `
2352 cc_library {
2353 name: "libvndk",
2354 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002355 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002356 vndk: {
2357 enabled: true,
2358 },
2359 }
2360 cc_library {
2361 name: "libvndksp",
2362 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002363 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002364 vndk: {
2365 enabled: true,
2366 support_system_process: true,
2367 },
2368 }
2369 cc_library {
2370 name: "libvndkprivate",
Justin Yunfd9e8042020-12-23 18:23:14 +09002371 vendor_available: true,
2372 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002373 vndk: {
2374 enabled: true,
Justin Yunfd9e8042020-12-23 18:23:14 +09002375 private: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002376 },
2377 }
2378 cc_library {
2379 name: "libvendor",
2380 vendor: true,
2381 }
2382 cc_library {
2383 name: "libvndkext",
2384 vendor: true,
2385 vndk: {
2386 enabled: true,
2387 extends: "libvndk",
2388 },
2389 }
2390 vndk_prebuilt_shared {
2391 name: "prevndk",
2392 version: "27",
2393 target_arch: "arm",
2394 binder32bit: true,
2395 vendor_available: true,
Justin Yun63e9ec72020-10-29 16:49:43 +09002396 product_available: true,
Colin Cross98be1bb2019-12-13 20:41:13 -08002397 vndk: {
2398 enabled: true,
2399 },
2400 arch: {
2401 arm: {
2402 srcs: ["liba.so"],
2403 },
2404 },
2405 }
2406 cc_library {
2407 name: "libllndk",
Colin Cross203b4212021-04-26 17:19:41 -07002408 llndk: {
2409 symbol_file: "libllndk.map.txt",
2410 }
Colin Cross98be1bb2019-12-13 20:41:13 -08002411 }
2412 cc_library {
2413 name: "libllndkprivate",
Colin Cross203b4212021-04-26 17:19:41 -07002414 llndk: {
2415 symbol_file: "libllndkprivate.map.txt",
2416 private: true,
2417 }
Colin Cross78212242021-01-06 14:51:30 -08002418 }
2419
2420 llndk_libraries_txt {
2421 name: "llndk.libraries.txt",
2422 }
2423 vndkcore_libraries_txt {
2424 name: "vndkcore.libraries.txt",
2425 }
2426 vndksp_libraries_txt {
2427 name: "vndksp.libraries.txt",
2428 }
2429 vndkprivate_libraries_txt {
2430 name: "vndkprivate.libraries.txt",
2431 }
2432 vndkcorevariant_libraries_txt {
2433 name: "vndkcorevariant.libraries.txt",
2434 insert_vndk_version: false,
2435 }
2436 `
Colin Cross98be1bb2019-12-13 20:41:13 -08002437
Paul Duffinc3e6ce02021-03-22 23:21:32 +00002438 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Jooyung Han38002912019-05-16 04:01:54 +09002439 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002440 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Jooyung Han38002912019-05-16 04:01:54 +09002441 // native:vndk
Colin Cross98be1bb2019-12-13 20:41:13 -08002442 ctx := testCcWithConfig(t, config)
Jooyung Han38002912019-05-16 04:01:54 +09002443
Colin Cross78212242021-01-06 14:51:30 -08002444 checkVndkLibrariesOutput(t, ctx, "vndkcore.libraries.txt",
2445 []string{"libvndk.so", "libvndkprivate.so"})
2446 checkVndkLibrariesOutput(t, ctx, "vndksp.libraries.txt",
2447 []string{"libc++.so", "libvndksp.so"})
2448 checkVndkLibrariesOutput(t, ctx, "llndk.libraries.txt",
2449 []string{"libc.so", "libdl.so", "libft2.so", "libllndk.so", "libllndkprivate.so", "libm.so"})
2450 checkVndkLibrariesOutput(t, ctx, "vndkprivate.libraries.txt",
2451 []string{"libft2.so", "libllndkprivate.so", "libvndkprivate.so"})
Jooyung Han38002912019-05-16 04:01:54 +09002452
Colin Crossfb0c16e2019-11-20 17:12:35 -08002453 vendorVariant27 := "android_vendor.27_arm64_armv8-a_shared"
Inseob Kim64c43952019-08-26 16:52:35 +09002454
Jooyung Han38002912019-05-16 04:01:54 +09002455 tests := []struct {
2456 variant string
2457 name string
2458 expected string
2459 }{
2460 {vendorVariant, "libvndk", "native:vndk"},
2461 {vendorVariant, "libvndksp", "native:vndk"},
2462 {vendorVariant, "libvndkprivate", "native:vndk_private"},
2463 {vendorVariant, "libvendor", "native:vendor"},
2464 {vendorVariant, "libvndkext", "native:vendor"},
Colin Cross127bb8b2020-12-16 16:46:01 -08002465 {vendorVariant, "libllndk", "native:vndk"},
Inseob Kim64c43952019-08-26 16:52:35 +09002466 {vendorVariant27, "prevndk.vndk.27.arm.binder32", "native:vndk"},
Jooyung Han38002912019-05-16 04:01:54 +09002467 {coreVariant, "libvndk", "native:platform"},
2468 {coreVariant, "libvndkprivate", "native:platform"},
2469 {coreVariant, "libllndk", "native:platform"},
2470 }
2471 for _, test := range tests {
2472 t.Run(test.name, func(t *testing.T) {
2473 module := ctx.ModuleForTests(test.name, test.variant).Module().(*Module)
2474 assertString(t, module.makeLinkType, test.expected)
2475 })
2476 }
2477}
2478
Jeff Gaston294356f2017-09-27 17:05:30 -07002479var staticLinkDepOrderTestCases = []struct {
2480 // This is a string representation of a map[moduleName][]moduleDependency .
2481 // It models the dependencies declared in an Android.bp file.
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002482 inStatic string
2483
2484 // This is a string representation of a map[moduleName][]moduleDependency .
2485 // It models the dependencies declared in an Android.bp file.
2486 inShared string
Jeff Gaston294356f2017-09-27 17:05:30 -07002487
2488 // allOrdered is a string representation of a map[moduleName][]moduleDependency .
2489 // The keys of allOrdered specify which modules we would like to check.
2490 // The values of allOrdered specify the expected result (of the transitive closure of all
2491 // dependencies) for each module to test
2492 allOrdered string
2493
2494 // outOrdered is a string representation of a map[moduleName][]moduleDependency .
2495 // The keys of outOrdered specify which modules we would like to check.
2496 // The values of outOrdered specify the expected result (of the ordered linker command line)
2497 // for each module to test.
2498 outOrdered string
2499}{
2500 // Simple tests
2501 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002502 inStatic: "",
Jeff Gaston294356f2017-09-27 17:05:30 -07002503 outOrdered: "",
2504 },
2505 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002506 inStatic: "a:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002507 outOrdered: "a:",
2508 },
2509 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002510 inStatic: "a:b; b:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002511 outOrdered: "a:b; b:",
2512 },
2513 // Tests of reordering
2514 {
2515 // diamond example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002516 inStatic: "a:d,b,c; b:d; c:d; d:",
Jeff Gaston294356f2017-09-27 17:05:30 -07002517 outOrdered: "a:b,c,d; b:d; c:d; d:",
2518 },
2519 {
2520 // somewhat real example
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002521 inStatic: "bsdiff_unittest:b,c,d,e,f,g,h,i; e:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002522 outOrdered: "bsdiff_unittest:c,d,e,b,f,g,h,i; e:b",
2523 },
2524 {
2525 // multiple reorderings
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002526 inStatic: "a:b,c,d,e; d:b; e:c",
Jeff Gaston294356f2017-09-27 17:05:30 -07002527 outOrdered: "a:d,b,e,c; d:b; e:c",
2528 },
2529 {
2530 // should reorder without adding new transitive dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002531 inStatic: "bin:lib2,lib1; lib1:lib2,liboptional",
Jeff Gaston294356f2017-09-27 17:05:30 -07002532 allOrdered: "bin:lib1,lib2,liboptional; lib1:lib2,liboptional",
2533 outOrdered: "bin:lib1,lib2; lib1:lib2,liboptional",
2534 },
2535 {
2536 // multiple levels of dependencies
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002537 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 -07002538 allOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2539 outOrdered: "a:e,f,b,c,d,g,h; f:b,c,d; b:c,d; c:d",
2540 },
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002541 // shared dependencies
2542 {
2543 // Note that this test doesn't recurse, to minimize the amount of logic it tests.
2544 // So, we don't actually have to check that a shared dependency of c will change the order
2545 // of a library that depends statically on b and on c. We only need to check that if c has
2546 // a shared dependency on b, that that shows up in allOrdered.
2547 inShared: "c:b",
2548 allOrdered: "c:b",
2549 outOrdered: "c:",
2550 },
2551 {
2552 // This test doesn't actually include any shared dependencies but it's a reminder of what
2553 // the second phase of the above test would look like
2554 inStatic: "a:b,c; c:b",
2555 allOrdered: "a:c,b; c:b",
2556 outOrdered: "a:c,b; c:b",
2557 },
Jeff Gaston294356f2017-09-27 17:05:30 -07002558 // tiebreakers for when two modules specifying different orderings and there is no dependency
2559 // to dictate an order
2560 {
2561 // 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 -08002562 inStatic: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
Jeff Gaston294356f2017-09-27 17:05:30 -07002563 outOrdered: "a1:b,c,d,e; a2:b,c,e,d; b:d,e; c:e,d",
2564 },
2565 {
2566 // 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 -08002567 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 -07002568 outOrdered: "a1:b1,c1,e,d; b1:d,e; c1:e,d; a2:b2,c2,d,e; b2:d,e; c2:d,e",
2569 },
2570 // Tests involving duplicate dependencies
2571 {
2572 // simple duplicate
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002573 inStatic: "a:b,c,c,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002574 outOrdered: "a:c,b",
2575 },
2576 {
2577 // duplicates with reordering
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002578 inStatic: "a:b,c,d,c; c:b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002579 outOrdered: "a:d,c,b",
2580 },
2581 // Tests to confirm the nonexistence of infinite loops.
2582 // These cases should never happen, so as long as the test terminates and the
2583 // result is deterministic then that should be fine.
2584 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002585 inStatic: "a:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002586 outOrdered: "a:a",
2587 },
2588 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002589 inStatic: "a:b; b:c; c:a",
Jeff Gaston294356f2017-09-27 17:05:30 -07002590 allOrdered: "a:b,c; b:c,a; c:a,b",
2591 outOrdered: "a:b; b:c; c:a",
2592 },
2593 {
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002594 inStatic: "a:b,c; b:c,a; c:a,b",
Jeff Gaston294356f2017-09-27 17:05:30 -07002595 allOrdered: "a:c,a,b; b:a,b,c; c:b,c,a",
2596 outOrdered: "a:c,b; b:a,c; c:b,a",
2597 },
2598}
2599
2600// converts from a string like "a:b,c; d:e" to (["a","b"], {"a":["b","c"], "d":["e"]}, [{"a", "a.o"}, {"b", "b.o"}])
2601func parseModuleDeps(text string) (modulesInOrder []android.Path, allDeps map[android.Path][]android.Path) {
2602 // convert from "a:b,c; d:e" to "a:b,c;d:e"
2603 strippedText := strings.Replace(text, " ", "", -1)
2604 if len(strippedText) < 1 {
2605 return []android.Path{}, make(map[android.Path][]android.Path, 0)
2606 }
2607 allDeps = make(map[android.Path][]android.Path, 0)
2608
2609 // convert from "a:b,c;d:e" to ["a:b,c", "d:e"]
2610 moduleTexts := strings.Split(strippedText, ";")
2611
2612 outputForModuleName := func(moduleName string) android.Path {
2613 return android.PathForTesting(moduleName)
2614 }
2615
2616 for _, moduleText := range moduleTexts {
2617 // convert from "a:b,c" to ["a", "b,c"]
2618 components := strings.Split(moduleText, ":")
2619 if len(components) != 2 {
2620 panic(fmt.Sprintf("illegal module dep string %q from larger string %q; must contain one ':', not %v", moduleText, text, len(components)-1))
2621 }
2622 moduleName := components[0]
2623 moduleOutput := outputForModuleName(moduleName)
2624 modulesInOrder = append(modulesInOrder, moduleOutput)
2625
2626 depString := components[1]
2627 // convert from "b,c" to ["b", "c"]
2628 depNames := strings.Split(depString, ",")
2629 if len(depString) < 1 {
2630 depNames = []string{}
2631 }
2632 var deps []android.Path
2633 for _, depName := range depNames {
2634 deps = append(deps, outputForModuleName(depName))
2635 }
2636 allDeps[moduleOutput] = deps
2637 }
2638 return modulesInOrder, allDeps
2639}
2640
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002641func TestStaticLibDepReordering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002642 t.Parallel()
Jeff Gaston294356f2017-09-27 17:05:30 -07002643 ctx := testCc(t, `
2644 cc_library {
2645 name: "a",
2646 static_libs: ["b", "c", "d"],
Jiyong Park374510b2018-03-19 18:23:01 +09002647 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002648 }
2649 cc_library {
2650 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002651 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002652 }
2653 cc_library {
2654 name: "c",
2655 static_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002656 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002657 }
2658 cc_library {
2659 name: "d",
Jiyong Park374510b2018-03-19 18:23:01 +09002660 stl: "none",
Jeff Gaston294356f2017-09-27 17:05:30 -07002661 }
2662
2663 `)
2664
Colin Cross7113d202019-11-20 16:39:12 -08002665 variant := "android_arm64_armv8-a_static"
Jeff Gaston294356f2017-09-27 17:05:30 -07002666 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002667 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2668 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002669 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b", "d"})
Jeff Gaston294356f2017-09-27 17:05:30 -07002670
2671 if !reflect.DeepEqual(actual, expected) {
2672 t.Errorf("staticDeps orderings were not propagated correctly"+
2673 "\nactual: %v"+
2674 "\nexpected: %v",
2675 actual,
2676 expected,
2677 )
2678 }
Jiyong Parkd08b6972017-09-26 10:50:54 +09002679}
Jeff Gaston294356f2017-09-27 17:05:30 -07002680
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002681func TestStaticLibDepReorderingWithShared(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002682 t.Parallel()
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002683 ctx := testCc(t, `
2684 cc_library {
2685 name: "a",
2686 static_libs: ["b", "c"],
Jiyong Park374510b2018-03-19 18:23:01 +09002687 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002688 }
2689 cc_library {
2690 name: "b",
Jiyong Park374510b2018-03-19 18:23:01 +09002691 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002692 }
2693 cc_library {
2694 name: "c",
2695 shared_libs: ["b"],
Jiyong Park374510b2018-03-19 18:23:01 +09002696 stl: "none",
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002697 }
2698
2699 `)
2700
Colin Cross7113d202019-11-20 16:39:12 -08002701 variant := "android_arm64_armv8-a_static"
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002702 moduleA := ctx.ModuleForTests("a", variant).Module().(*Module)
Paul Duffine8366da2021-03-24 10:40:38 +00002703 actual := ctx.ModuleProvider(moduleA, StaticLibraryInfoProvider).(StaticLibraryInfo).
2704 TransitiveStaticLibrariesForOrdering.ToList().RelativeToTop()
Ivan Lozanod67a6b02021-05-20 13:01:32 -04002705 expected := GetOutputPaths(ctx, variant, []string{"a", "c", "b"})
Jeff Gastonf5b6e8f2017-11-27 15:48:57 -08002706
2707 if !reflect.DeepEqual(actual, expected) {
2708 t.Errorf("staticDeps orderings did not account for shared libs"+
2709 "\nactual: %v"+
2710 "\nexpected: %v",
2711 actual,
2712 expected,
2713 )
2714 }
2715}
2716
Jooyung Hanb04a4992020-03-13 18:57:35 +09002717func checkEquals(t *testing.T, message string, expected, actual interface{}) {
Colin Crossd1f898e2020-08-18 18:35:15 -07002718 t.Helper()
Jooyung Hanb04a4992020-03-13 18:57:35 +09002719 if !reflect.DeepEqual(actual, expected) {
2720 t.Errorf(message+
2721 "\nactual: %v"+
2722 "\nexpected: %v",
2723 actual,
2724 expected,
2725 )
2726 }
2727}
2728
Jooyung Han61b66e92020-03-21 14:21:46 +00002729func TestLlndkLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002730 t.Parallel()
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002731 result := prepareForCcTest.RunTestWithBp(t, `
2732 cc_library {
2733 name: "libllndk",
2734 stubs: { versions: ["1", "2"] },
2735 llndk: {
2736 symbol_file: "libllndk.map.txt",
2737 },
2738 export_include_dirs: ["include"],
2739 }
2740
2741 cc_prebuilt_library_shared {
2742 name: "libllndkprebuilt",
2743 stubs: { versions: ["1", "2"] },
2744 llndk: {
2745 symbol_file: "libllndkprebuilt.map.txt",
2746 },
2747 }
2748
2749 cc_library {
2750 name: "libllndk_with_external_headers",
2751 stubs: { versions: ["1", "2"] },
2752 llndk: {
2753 symbol_file: "libllndk.map.txt",
2754 export_llndk_headers: ["libexternal_llndk_headers"],
2755 },
2756 header_libs: ["libexternal_headers"],
2757 export_header_lib_headers: ["libexternal_headers"],
2758 }
2759 cc_library_headers {
2760 name: "libexternal_headers",
2761 export_include_dirs: ["include"],
2762 vendor_available: true,
2763 }
2764 cc_library_headers {
2765 name: "libexternal_llndk_headers",
2766 export_include_dirs: ["include_llndk"],
2767 llndk: {
2768 symbol_file: "libllndk.map.txt",
2769 },
2770 vendor_available: true,
2771 }
2772
2773 cc_library {
2774 name: "libllndk_with_override_headers",
2775 stubs: { versions: ["1", "2"] },
2776 llndk: {
2777 symbol_file: "libllndk.map.txt",
2778 override_export_include_dirs: ["include_llndk"],
2779 },
2780 export_include_dirs: ["include"],
2781 }
2782 `)
2783 actual := result.ModuleVariantsForTests("libllndk")
2784 for i := 0; i < len(actual); i++ {
2785 if !strings.HasPrefix(actual[i], "android_vendor.29_") {
2786 actual = append(actual[:i], actual[i+1:]...)
2787 i--
2788 }
2789 }
2790 expected := []string{
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002791 "android_vendor.29_arm64_armv8-a_shared_current",
2792 "android_vendor.29_arm64_armv8-a_shared",
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002793 "android_vendor.29_arm_armv7-a-neon_shared_current",
2794 "android_vendor.29_arm_armv7-a-neon_shared",
2795 }
2796 android.AssertArrayString(t, "variants for llndk stubs", expected, actual)
2797
2798 params := result.ModuleForTests("libllndk", "android_vendor.29_arm_armv7-a-neon_shared").Description("generate stub")
2799 android.AssertSame(t, "use VNDK version for default stubs", "current", params.Args["apiLevel"])
2800
Colin Cross0fb7fcd2021-03-02 11:00:07 -08002801 checkExportedIncludeDirs := func(module, variant string, expectedDirs ...string) {
2802 t.Helper()
2803 m := result.ModuleForTests(module, variant).Module()
2804 f := result.ModuleProvider(m, FlagExporterInfoProvider).(FlagExporterInfo)
2805 android.AssertPathsRelativeToTopEquals(t, "exported include dirs for "+module+"["+variant+"]",
2806 expectedDirs, f.IncludeDirs)
2807 }
2808
2809 checkExportedIncludeDirs("libllndk", "android_arm64_armv8-a_shared", "include")
2810 checkExportedIncludeDirs("libllndk", "android_vendor.29_arm64_armv8-a_shared", "include")
2811 checkExportedIncludeDirs("libllndk_with_external_headers", "android_arm64_armv8-a_shared", "include")
2812 checkExportedIncludeDirs("libllndk_with_external_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2813 checkExportedIncludeDirs("libllndk_with_override_headers", "android_arm64_armv8-a_shared", "include")
2814 checkExportedIncludeDirs("libllndk_with_override_headers", "android_vendor.29_arm64_armv8-a_shared", "include_llndk")
2815}
2816
Jiyong Parka46a4d52017-12-14 19:54:34 +09002817func TestLlndkHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002818 t.Parallel()
Jiyong Parka46a4d52017-12-14 19:54:34 +09002819 ctx := testCc(t, `
Colin Cross627280f2021-04-26 16:53:58 -07002820 cc_library_headers {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002821 name: "libllndk_headers",
2822 export_include_dirs: ["my_include"],
Colin Cross627280f2021-04-26 16:53:58 -07002823 llndk: {
2824 llndk_headers: true,
2825 },
Jiyong Parka46a4d52017-12-14 19:54:34 +09002826 }
2827 cc_library {
Colin Cross0477b422020-10-13 18:43:54 -07002828 name: "libllndk",
Colin Cross627280f2021-04-26 16:53:58 -07002829 llndk: {
2830 symbol_file: "libllndk.map.txt",
2831 export_llndk_headers: ["libllndk_headers"],
2832 }
Colin Cross0477b422020-10-13 18:43:54 -07002833 }
2834
2835 cc_library {
Jiyong Parka46a4d52017-12-14 19:54:34 +09002836 name: "libvendor",
2837 shared_libs: ["libllndk"],
2838 vendor: true,
2839 srcs: ["foo.c"],
Yi Konge7fe9912019-06-02 00:53:50 -07002840 no_libcrt: true,
Logan Chienf3511742017-10-31 18:04:35 +08002841 nocrt: true,
Jiyong Parka46a4d52017-12-14 19:54:34 +09002842 }
2843 `)
2844
2845 // _static variant is used since _shared reuses *.o from the static variant
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002846 cc := ctx.ModuleForTests("libvendor", "android_vendor.29_arm_armv7-a-neon_static").Rule("cc")
Jiyong Parka46a4d52017-12-14 19:54:34 +09002847 cflags := cc.Args["cFlags"]
2848 if !strings.Contains(cflags, "-Imy_include") {
2849 t.Errorf("cflags for libvendor must contain -Imy_include, but was %#v.", cflags)
2850 }
2851}
2852
Logan Chien43d34c32017-12-20 01:17:32 +08002853func checkRuntimeLibs(t *testing.T, expected []string, module *Module) {
2854 actual := module.Properties.AndroidMkRuntimeLibs
2855 if !reflect.DeepEqual(actual, expected) {
2856 t.Errorf("incorrect runtime_libs for shared libs"+
2857 "\nactual: %v"+
2858 "\nexpected: %v",
2859 actual,
2860 expected,
2861 )
2862 }
2863}
2864
2865const runtimeLibAndroidBp = `
2866 cc_library {
Justin Yun8a2600c2020-12-07 12:44:03 +09002867 name: "liball_available",
2868 vendor_available: true,
2869 product_available: true,
2870 no_libcrt : true,
2871 nocrt : true,
2872 system_shared_libs : [],
2873 }
2874 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002875 name: "libvendor_available1",
2876 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002877 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002878 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002879 nocrt : true,
2880 system_shared_libs : [],
2881 }
2882 cc_library {
2883 name: "libvendor_available2",
2884 vendor_available: true,
Justin Yun8a2600c2020-12-07 12:44:03 +09002885 runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002886 target: {
2887 vendor: {
Justin Yun8a2600c2020-12-07 12:44:03 +09002888 exclude_runtime_libs: ["liball_available"],
Logan Chien43d34c32017-12-20 01:17:32 +08002889 }
2890 },
Yi Konge7fe9912019-06-02 00:53:50 -07002891 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002892 nocrt : true,
2893 system_shared_libs : [],
2894 }
2895 cc_library {
Justin Yuncbca3732021-02-03 19:24:13 +09002896 name: "libproduct_vendor",
2897 product_specific: true,
2898 vendor_available: true,
2899 no_libcrt : true,
2900 nocrt : true,
2901 system_shared_libs : [],
2902 }
2903 cc_library {
Logan Chien43d34c32017-12-20 01:17:32 +08002904 name: "libcore",
Justin Yun8a2600c2020-12-07 12:44:03 +09002905 runtime_libs: ["liball_available"],
Yi Konge7fe9912019-06-02 00:53:50 -07002906 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002907 nocrt : true,
2908 system_shared_libs : [],
2909 }
2910 cc_library {
2911 name: "libvendor1",
2912 vendor: true,
Yi Konge7fe9912019-06-02 00:53:50 -07002913 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002914 nocrt : true,
2915 system_shared_libs : [],
2916 }
2917 cc_library {
2918 name: "libvendor2",
2919 vendor: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002920 runtime_libs: ["liball_available", "libvendor1", "libproduct_vendor"],
Justin Yun8a2600c2020-12-07 12:44:03 +09002921 no_libcrt : true,
2922 nocrt : true,
2923 system_shared_libs : [],
2924 }
2925 cc_library {
2926 name: "libproduct_available1",
2927 product_available: true,
2928 runtime_libs: ["liball_available"],
2929 no_libcrt : true,
2930 nocrt : true,
2931 system_shared_libs : [],
2932 }
2933 cc_library {
2934 name: "libproduct1",
2935 product_specific: true,
2936 no_libcrt : true,
2937 nocrt : true,
2938 system_shared_libs : [],
2939 }
2940 cc_library {
2941 name: "libproduct2",
2942 product_specific: true,
Justin Yuncbca3732021-02-03 19:24:13 +09002943 runtime_libs: ["liball_available", "libproduct1", "libproduct_vendor"],
Yi Konge7fe9912019-06-02 00:53:50 -07002944 no_libcrt : true,
Logan Chien43d34c32017-12-20 01:17:32 +08002945 nocrt : true,
2946 system_shared_libs : [],
2947 }
2948`
2949
2950func TestRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002951 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002952 ctx := testCc(t, runtimeLibAndroidBp)
2953
2954 // runtime_libs for core variants use the module names without suffixes.
Colin Cross7113d202019-11-20 16:39:12 -08002955 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002956
Justin Yun8a2600c2020-12-07 12:44:03 +09002957 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2958 checkRuntimeLibs(t, []string{"liball_available"}, module)
2959
2960 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2961 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002962
2963 module = ctx.ModuleForTests("libcore", variant).Module().(*Module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002964 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002965
2966 // runtime_libs for vendor variants have '.vendor' suffixes if the modules have both core
2967 // and vendor variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002968 variant = "android_vendor.29_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08002969
Justin Yun8a2600c2020-12-07 12:44:03 +09002970 module = ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
2971 checkRuntimeLibs(t, []string{"liball_available.vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002972
2973 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09002974 checkRuntimeLibs(t, []string{"liball_available.vendor", "libvendor1", "libproduct_vendor.vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09002975
2976 // runtime_libs for product variants have '.product' suffixes if the modules have both core
2977 // and product variants.
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002978 variant = "android_product.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002979
2980 module = ctx.ModuleForTests("libproduct_available1", variant).Module().(*Module)
2981 checkRuntimeLibs(t, []string{"liball_available.product"}, module)
2982
2983 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yund00f5ca2021-02-03 19:43:02 +09002984 checkRuntimeLibs(t, []string{"liball_available.product", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002985}
2986
2987func TestExcludeRuntimeLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04002988 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08002989 ctx := testCc(t, runtimeLibAndroidBp)
2990
Colin Cross7113d202019-11-20 16:39:12 -08002991 variant := "android_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002992 module := ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
2993 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08002994
Jiyong Parkf58c46e2021-04-01 21:35:20 +09002995 variant = "android_vendor.29_arm64_armv8-a_shared"
Justin Yun8a2600c2020-12-07 12:44:03 +09002996 module = ctx.ModuleForTests("libvendor_available2", variant).Module().(*Module)
Logan Chien43d34c32017-12-20 01:17:32 +08002997 checkRuntimeLibs(t, nil, module)
2998}
2999
3000func TestRuntimeLibsNoVndk(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003001 t.Parallel()
Logan Chien43d34c32017-12-20 01:17:32 +08003002 ctx := testCcNoVndk(t, runtimeLibAndroidBp)
3003
3004 // If DeviceVndkVersion is not defined, then runtime_libs are copied as-is.
3005
Colin Cross7113d202019-11-20 16:39:12 -08003006 variant := "android_arm64_armv8-a_shared"
Logan Chien43d34c32017-12-20 01:17:32 +08003007
Justin Yun8a2600c2020-12-07 12:44:03 +09003008 module := ctx.ModuleForTests("libvendor_available1", variant).Module().(*Module)
3009 checkRuntimeLibs(t, []string{"liball_available"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003010
3011 module = ctx.ModuleForTests("libvendor2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003012 checkRuntimeLibs(t, []string{"liball_available", "libvendor1", "libproduct_vendor"}, module)
Justin Yun8a2600c2020-12-07 12:44:03 +09003013
3014 module = ctx.ModuleForTests("libproduct2", variant).Module().(*Module)
Justin Yuncbca3732021-02-03 19:24:13 +09003015 checkRuntimeLibs(t, []string{"liball_available", "libproduct1", "libproduct_vendor"}, module)
Logan Chien43d34c32017-12-20 01:17:32 +08003016}
3017
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003018func checkStaticLibs(t *testing.T, expected []string, module *Module) {
Jooyung Han03b51852020-02-26 22:45:42 +09003019 t.Helper()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003020 actual := module.Properties.AndroidMkStaticLibs
3021 if !reflect.DeepEqual(actual, expected) {
3022 t.Errorf("incorrect static_libs"+
3023 "\nactual: %v"+
3024 "\nexpected: %v",
3025 actual,
3026 expected,
3027 )
3028 }
3029}
3030
3031const staticLibAndroidBp = `
3032 cc_library {
3033 name: "lib1",
3034 }
3035 cc_library {
3036 name: "lib2",
3037 static_libs: ["lib1"],
3038 }
3039`
3040
3041func TestStaticLibDepExport(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003042 t.Parallel()
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003043 ctx := testCc(t, staticLibAndroidBp)
3044
3045 // Check the shared version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003046 variant := "android_arm64_armv8-a_shared"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003047 module := ctx.ModuleForTests("lib2", variant).Module().(*Module)
Colin Cross4c4c1be2022-02-10 11:41:18 -08003048 checkStaticLibs(t, []string{"lib1", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003049
3050 // Check the static version of lib2.
Colin Cross7113d202019-11-20 16:39:12 -08003051 variant = "android_arm64_armv8-a_static"
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003052 module = ctx.ModuleForTests("lib2", variant).Module().(*Module)
3053 // libc++_static is linked additionally.
Colin Cross4c4c1be2022-02-10 11:41:18 -08003054 checkStaticLibs(t, []string{"lib1", "libc++_static", "libc++demangle", "libclang_rt.builtins"}, module)
Jaewoong Jung16c7d3d2018-11-16 01:19:56 +00003055}
3056
Jiyong Parkd08b6972017-09-26 10:50:54 +09003057var compilerFlagsTestCases = []struct {
3058 in string
3059 out bool
3060}{
3061 {
3062 in: "a",
3063 out: false,
3064 },
3065 {
3066 in: "-a",
3067 out: true,
3068 },
3069 {
3070 in: "-Ipath/to/something",
3071 out: false,
3072 },
3073 {
3074 in: "-isystempath/to/something",
3075 out: false,
3076 },
3077 {
3078 in: "--coverage",
3079 out: false,
3080 },
3081 {
3082 in: "-include a/b",
3083 out: true,
3084 },
3085 {
3086 in: "-include a/b c/d",
3087 out: false,
3088 },
3089 {
3090 in: "-DMACRO",
3091 out: true,
3092 },
3093 {
3094 in: "-DMAC RO",
3095 out: false,
3096 },
3097 {
3098 in: "-a -b",
3099 out: false,
3100 },
3101 {
3102 in: "-DMACRO=definition",
3103 out: true,
3104 },
3105 {
3106 in: "-DMACRO=defi nition",
3107 out: true, // TODO(jiyong): this should be false
3108 },
3109 {
3110 in: "-DMACRO(x)=x + 1",
3111 out: true,
3112 },
3113 {
3114 in: "-DMACRO=\"defi nition\"",
3115 out: true,
3116 },
3117}
3118
3119type mockContext struct {
3120 BaseModuleContext
3121 result bool
3122}
3123
3124func (ctx *mockContext) PropertyErrorf(property, format string, args ...interface{}) {
3125 // CheckBadCompilerFlags calls this function when the flag should be rejected
3126 ctx.result = false
3127}
3128
3129func TestCompilerFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003130 t.Parallel()
Jiyong Parkd08b6972017-09-26 10:50:54 +09003131 for _, testCase := range compilerFlagsTestCases {
3132 ctx := &mockContext{result: true}
3133 CheckBadCompilerFlags(ctx, "", []string{testCase.in})
3134 if ctx.result != testCase.out {
3135 t.Errorf("incorrect output:")
3136 t.Errorf(" input: %#v", testCase.in)
3137 t.Errorf(" expected: %#v", testCase.out)
3138 t.Errorf(" got: %#v", ctx.result)
3139 }
3140 }
Jeff Gaston294356f2017-09-27 17:05:30 -07003141}
Jiyong Park374510b2018-03-19 18:23:01 +09003142
Jiyong Park37b25202018-07-11 10:49:27 +09003143func TestRecovery(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003144 t.Parallel()
Jiyong Park37b25202018-07-11 10:49:27 +09003145 ctx := testCc(t, `
3146 cc_library_shared {
3147 name: "librecovery",
3148 recovery: true,
3149 }
3150 cc_library_shared {
3151 name: "librecovery32",
3152 recovery: true,
3153 compile_multilib:"32",
3154 }
Jiyong Park5baac542018-08-28 09:55:37 +09003155 cc_library_shared {
3156 name: "libHalInRecovery",
3157 recovery_available: true,
3158 vendor: true,
3159 }
Jiyong Park37b25202018-07-11 10:49:27 +09003160 `)
3161
3162 variants := ctx.ModuleVariantsForTests("librecovery")
Colin Crossfb0c16e2019-11-20 17:12:35 -08003163 const arm64 = "android_recovery_arm64_armv8-a_shared"
Jiyong Park37b25202018-07-11 10:49:27 +09003164 if len(variants) != 1 || !android.InList(arm64, variants) {
3165 t.Errorf("variants of librecovery must be \"%s\" only, but was %#v", arm64, variants)
3166 }
3167
3168 variants = ctx.ModuleVariantsForTests("librecovery32")
3169 if android.InList(arm64, variants) {
3170 t.Errorf("multilib was set to 32 for librecovery32, but its variants has %s.", arm64)
3171 }
Jiyong Park5baac542018-08-28 09:55:37 +09003172
3173 recoveryModule := ctx.ModuleForTests("libHalInRecovery", recoveryVariant).Module().(*Module)
3174 if !recoveryModule.Platform() {
3175 t.Errorf("recovery variant of libHalInRecovery must not specific to device, soc, or product")
3176 }
Jiyong Park7ed9de32018-10-15 22:25:07 +09003177}
Jiyong Park5baac542018-08-28 09:55:37 +09003178
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003179func TestDataLibsPrebuiltSharedTestLibrary(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003180 t.Parallel()
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003181 bp := `
3182 cc_prebuilt_test_library_shared {
3183 name: "test_lib",
3184 relative_install_path: "foo/bar/baz",
3185 srcs: ["srcpath/dontusethispath/baz.so"],
3186 }
3187
3188 cc_test {
3189 name: "main_test",
3190 data_libs: ["test_lib"],
3191 gtest: false,
3192 }
3193 `
3194
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003195 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003196 config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
Jiyong Parkf58c46e2021-04-01 21:35:20 +09003197 config.TestProductVariables.Platform_vndk_version = StringPtr("29")
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003198 config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
3199
3200 ctx := testCcWithConfig(t, config)
3201 module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
3202 testBinary := module.(*Module).linker.(*testBinary)
3203 outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
3204 if err != nil {
3205 t.Fatalf("Expected cc_test to produce output files, error: %s", err)
3206 }
3207 if len(outputFiles) != 1 {
3208 t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
3209 }
3210 if len(testBinary.dataPaths()) != 1 {
3211 t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
3212 }
3213
3214 outputPath := outputFiles[0].String()
3215
3216 if !strings.HasSuffix(outputPath, "/main_test") {
3217 t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
3218 }
Colin Crossaa255532020-07-03 13:18:24 -07003219 entries := android.AndroidMkEntriesForTest(t, ctx, module)[0]
Chris Parsons1f6d90f2020-06-17 16:10:42 -04003220 if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
3221 t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
3222 " but was '%s'", entries.EntryMap["LOCAL_TEST_DATA"][0])
3223 }
3224}
3225
Jiyong Park7ed9de32018-10-15 22:25:07 +09003226func TestVersionedStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003227 t.Parallel()
Jiyong Park7ed9de32018-10-15 22:25:07 +09003228 ctx := testCc(t, `
3229 cc_library_shared {
3230 name: "libFoo",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003231 srcs: ["foo.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003232 stubs: {
3233 symbol_file: "foo.map.txt",
3234 versions: ["1", "2", "3"],
3235 },
3236 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003237
Jiyong Park7ed9de32018-10-15 22:25:07 +09003238 cc_library_shared {
3239 name: "libBar",
Jiyong Parkda732bd2018-11-02 18:23:15 +09003240 srcs: ["bar.c"],
Jiyong Park7ed9de32018-10-15 22:25:07 +09003241 shared_libs: ["libFoo#1"],
3242 }`)
3243
3244 variants := ctx.ModuleVariantsForTests("libFoo")
3245 expectedVariants := []string{
Colin Cross7113d202019-11-20 16:39:12 -08003246 "android_arm64_armv8-a_shared",
3247 "android_arm64_armv8-a_shared_1",
3248 "android_arm64_armv8-a_shared_2",
3249 "android_arm64_armv8-a_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003250 "android_arm64_armv8-a_shared_current",
Colin Cross7113d202019-11-20 16:39:12 -08003251 "android_arm_armv7-a-neon_shared",
3252 "android_arm_armv7-a-neon_shared_1",
3253 "android_arm_armv7-a-neon_shared_2",
3254 "android_arm_armv7-a-neon_shared_3",
Jiyong Parkd4a3a132021-03-17 20:21:35 +09003255 "android_arm_armv7-a-neon_shared_current",
Jiyong Park7ed9de32018-10-15 22:25:07 +09003256 }
3257 variantsMismatch := false
3258 if len(variants) != len(expectedVariants) {
3259 variantsMismatch = true
3260 } else {
3261 for _, v := range expectedVariants {
3262 if !inList(v, variants) {
3263 variantsMismatch = false
3264 }
3265 }
3266 }
3267 if variantsMismatch {
3268 t.Errorf("variants of libFoo expected:\n")
3269 for _, v := range expectedVariants {
3270 t.Errorf("%q\n", v)
3271 }
3272 t.Errorf(", but got:\n")
3273 for _, v := range variants {
3274 t.Errorf("%q\n", v)
3275 }
3276 }
3277
Colin Cross7113d202019-11-20 16:39:12 -08003278 libBarLinkRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("ld")
Jiyong Park7ed9de32018-10-15 22:25:07 +09003279 libFlags := libBarLinkRule.Args["libFlags"]
Colin Cross7113d202019-11-20 16:39:12 -08003280 libFoo1StubPath := "libFoo/android_arm64_armv8-a_shared_1/libFoo.so"
Jiyong Park7ed9de32018-10-15 22:25:07 +09003281 if !strings.Contains(libFlags, libFoo1StubPath) {
3282 t.Errorf("%q is not found in %q", libFoo1StubPath, libFlags)
3283 }
Jiyong Parkda732bd2018-11-02 18:23:15 +09003284
Colin Cross7113d202019-11-20 16:39:12 -08003285 libBarCompileRule := ctx.ModuleForTests("libBar", "android_arm64_armv8-a_shared").Rule("cc")
Jiyong Parkda732bd2018-11-02 18:23:15 +09003286 cFlags := libBarCompileRule.Args["cFlags"]
3287 libFoo1VersioningMacro := "-D__LIBFOO_API__=1"
3288 if !strings.Contains(cFlags, libFoo1VersioningMacro) {
3289 t.Errorf("%q is not found in %q", libFoo1VersioningMacro, cFlags)
3290 }
Jiyong Park37b25202018-07-11 10:49:27 +09003291}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003292
Jooyung Hanb04a4992020-03-13 18:57:35 +09003293func TestVersioningMacro(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003294 t.Parallel()
Jooyung Hanb04a4992020-03-13 18:57:35 +09003295 for _, tc := range []struct{ moduleName, expected string }{
3296 {"libc", "__LIBC_API__"},
3297 {"libfoo", "__LIBFOO_API__"},
3298 {"libfoo@1", "__LIBFOO_1_API__"},
3299 {"libfoo-v1", "__LIBFOO_V1_API__"},
3300 {"libfoo.v1", "__LIBFOO_V1_API__"},
3301 } {
3302 checkEquals(t, tc.moduleName, tc.expected, versioningMacroName(tc.moduleName))
3303 }
3304}
3305
Liz Kammer83cf81b2022-09-22 08:24:20 -04003306func pathsToBase(paths android.Paths) []string {
3307 var ret []string
3308 for _, p := range paths {
3309 ret = append(ret, p.Base())
3310 }
3311 return ret
3312}
3313
3314func TestStaticLibArchiveArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003315 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003316 ctx := testCc(t, `
3317 cc_library_static {
3318 name: "foo",
3319 srcs: ["foo.c"],
3320 }
3321
3322 cc_library_static {
3323 name: "bar",
3324 srcs: ["bar.c"],
3325 }
3326
3327 cc_library_shared {
3328 name: "qux",
3329 srcs: ["qux.c"],
3330 }
3331
3332 cc_library_static {
3333 name: "baz",
3334 srcs: ["baz.c"],
3335 static_libs: ["foo"],
3336 shared_libs: ["qux"],
3337 whole_static_libs: ["bar"],
3338 }`)
3339
3340 variant := "android_arm64_armv8-a_static"
3341 arRule := ctx.ModuleForTests("baz", variant).Rule("ar")
3342
3343 // For static libraries, the object files of a whole static dep are included in the archive
3344 // directly
3345 if g, w := pathsToBase(arRule.Inputs), []string{"bar.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3346 t.Errorf("Expected input objects %q, got %q", w, g)
3347 }
3348
3349 // non whole static dependencies are not linked into the archive
3350 if len(arRule.Implicits) > 0 {
3351 t.Errorf("Expected 0 additional deps, got %q", arRule.Implicits)
3352 }
3353}
3354
3355func TestSharedLibLinkingArgs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003356 t.Parallel()
Liz Kammer83cf81b2022-09-22 08:24:20 -04003357 ctx := testCc(t, `
3358 cc_library_static {
3359 name: "foo",
3360 srcs: ["foo.c"],
3361 }
3362
3363 cc_library_static {
3364 name: "bar",
3365 srcs: ["bar.c"],
3366 }
3367
3368 cc_library_shared {
3369 name: "qux",
3370 srcs: ["qux.c"],
3371 }
3372
3373 cc_library_shared {
3374 name: "baz",
3375 srcs: ["baz.c"],
3376 static_libs: ["foo"],
3377 shared_libs: ["qux"],
3378 whole_static_libs: ["bar"],
3379 }`)
3380
3381 variant := "android_arm64_armv8-a_shared"
3382 linkRule := ctx.ModuleForTests("baz", variant).Rule("ld")
3383 libFlags := linkRule.Args["libFlags"]
3384 // When dynamically linking, we expect static dependencies to be found on the command line
3385 if expected := "foo.a"; !strings.Contains(libFlags, expected) {
3386 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3387 }
3388 // When dynamically linking, we expect whole static dependencies to be found on the command line
3389 if expected := "bar.a"; !strings.Contains(libFlags, expected) {
3390 t.Errorf("Static lib %q was not found in %q", expected, libFlags)
3391 }
3392
3393 // When dynamically linking, we expect shared dependencies to be found on the command line
3394 if expected := "qux.so"; !strings.Contains(libFlags, expected) {
3395 t.Errorf("Shared lib %q was not found in %q", expected, libFlags)
3396 }
3397
3398 // We should only have the objects from the shared library srcs, not the whole static dependencies
3399 if g, w := pathsToBase(linkRule.Inputs), []string{"baz.o"}; !reflect.DeepEqual(w, g) {
3400 t.Errorf("Expected input objects %q, got %q", w, g)
3401 }
3402}
3403
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003404func TestStaticExecutable(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003405 t.Parallel()
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003406 ctx := testCc(t, `
3407 cc_binary {
3408 name: "static_test",
Pete Bentleyfcf55bf2019-08-16 20:14:32 +01003409 srcs: ["foo.c", "baz.o"],
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003410 static_executable: true,
3411 }`)
3412
Colin Cross7113d202019-11-20 16:39:12 -08003413 variant := "android_arm64_armv8-a"
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003414 binModuleRule := ctx.ModuleForTests("static_test", variant).Rule("ld")
3415 libFlags := binModuleRule.Args["libFlags"]
Ryan Prichardb49fe1b2019-10-11 15:03:34 -07003416 systemStaticLibs := []string{"libc.a", "libm.a"}
Jaewoong Jung232c07c2018-12-18 11:08:25 -08003417 for _, lib := range systemStaticLibs {
3418 if !strings.Contains(libFlags, lib) {
3419 t.Errorf("Static lib %q was not found in %q", lib, libFlags)
3420 }
3421 }
3422 systemSharedLibs := []string{"libc.so", "libm.so", "libdl.so"}
3423 for _, lib := range systemSharedLibs {
3424 if strings.Contains(libFlags, lib) {
3425 t.Errorf("Shared lib %q was found in %q", lib, libFlags)
3426 }
3427 }
3428}
Jiyong Parke4bb9862019-02-01 00:31:10 +09003429
3430func TestStaticDepsOrderWithStubs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003431 t.Parallel()
Jiyong Parke4bb9862019-02-01 00:31:10 +09003432 ctx := testCc(t, `
3433 cc_binary {
3434 name: "mybin",
3435 srcs: ["foo.c"],
Colin Cross0de8a1e2020-09-18 14:15:30 -07003436 static_libs: ["libfooC", "libfooB"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003437 static_executable: true,
3438 stl: "none",
3439 }
3440
3441 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003442 name: "libfooB",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003443 srcs: ["foo.c"],
Colin Crossf9aabd72020-02-15 11:29:50 -08003444 shared_libs: ["libfooC"],
Jiyong Parke4bb9862019-02-01 00:31:10 +09003445 stl: "none",
3446 }
3447
3448 cc_library {
Colin Crossf9aabd72020-02-15 11:29:50 -08003449 name: "libfooC",
Jiyong Parke4bb9862019-02-01 00:31:10 +09003450 srcs: ["foo.c"],
3451 stl: "none",
3452 stubs: {
3453 versions: ["1"],
3454 },
3455 }`)
3456
Colin Cross0de8a1e2020-09-18 14:15:30 -07003457 mybin := ctx.ModuleForTests("mybin", "android_arm64_armv8-a").Rule("ld")
3458 actual := mybin.Implicits[:2]
Ivan Lozanod67a6b02021-05-20 13:01:32 -04003459 expected := GetOutputPaths(ctx, "android_arm64_armv8-a_static", []string{"libfooB", "libfooC"})
Jiyong Parke4bb9862019-02-01 00:31:10 +09003460
3461 if !reflect.DeepEqual(actual, expected) {
3462 t.Errorf("staticDeps orderings were not propagated correctly"+
3463 "\nactual: %v"+
3464 "\nexpected: %v",
3465 actual,
3466 expected,
3467 )
3468 }
3469}
Jooyung Han38002912019-05-16 04:01:54 +09003470
Jooyung Hand48f3c32019-08-23 11:18:57 +09003471func TestErrorsIfAModuleDependsOnDisabled(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003472 t.Parallel()
Jooyung Hand48f3c32019-08-23 11:18:57 +09003473 testCcError(t, `module "libA" .* depends on disabled module "libB"`, `
3474 cc_library {
3475 name: "libA",
3476 srcs: ["foo.c"],
3477 shared_libs: ["libB"],
3478 stl: "none",
3479 }
3480
3481 cc_library {
3482 name: "libB",
3483 srcs: ["foo.c"],
3484 enabled: false,
3485 stl: "none",
3486 }
3487 `)
3488}
3489
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003490func VerifyAFLFuzzTargetVariant(t *testing.T, variant string) {
3491 bp := `
3492 cc_fuzz {
Cory Barkera1da26f2022-06-07 20:12:06 +00003493 name: "test_afl_fuzz_target",
3494 srcs: ["foo.c"],
3495 host_supported: true,
3496 static_libs: [
3497 "afl_fuzz_static_lib",
3498 ],
3499 shared_libs: [
3500 "afl_fuzz_shared_lib",
3501 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003502 fuzzing_frameworks: {
3503 afl: true,
3504 libfuzzer: false,
3505 },
Cory Barkera1da26f2022-06-07 20:12:06 +00003506 }
3507 cc_library {
3508 name: "afl_fuzz_static_lib",
3509 host_supported: true,
3510 srcs: ["static_file.c"],
3511 }
3512 cc_library {
3513 name: "libfuzzer_only_static_lib",
3514 host_supported: true,
3515 srcs: ["static_file.c"],
3516 }
3517 cc_library {
3518 name: "afl_fuzz_shared_lib",
3519 host_supported: true,
3520 srcs: ["shared_file.c"],
3521 static_libs: [
3522 "second_static_lib",
3523 ],
3524 }
3525 cc_library_headers {
3526 name: "libafl_headers",
3527 vendor_available: true,
3528 host_supported: true,
3529 export_include_dirs: [
3530 "include",
3531 "instrumentation",
3532 ],
3533 }
3534 cc_object {
3535 name: "afl-compiler-rt",
3536 vendor_available: true,
3537 host_supported: true,
3538 cflags: [
3539 "-fPIC",
3540 ],
3541 srcs: [
3542 "instrumentation/afl-compiler-rt.o.c",
3543 ],
3544 }
3545 cc_library {
3546 name: "second_static_lib",
3547 host_supported: true,
3548 srcs: ["second_file.c"],
3549 }
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003550 cc_object {
Cory Barkera1da26f2022-06-07 20:12:06 +00003551 name: "aflpp_driver",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003552 host_supported: true,
Cory Barkera1da26f2022-06-07 20:12:06 +00003553 srcs: [
3554 "aflpp_driver.c",
3555 ],
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003556 }`
3557
3558 testEnv := map[string]string{
3559 "FUZZ_FRAMEWORK": "AFL",
3560 }
3561
3562 ctx := android.GroupFixturePreparers(prepareForCcTest, android.FixtureMergeEnv(testEnv)).RunTestWithBp(t, bp)
Cory Barkera1da26f2022-06-07 20:12:06 +00003563
3564 checkPcGuardFlag := func(
3565 modName string, variantName string, shouldHave bool) {
3566 cc := ctx.ModuleForTests(modName, variantName).Rule("cc")
3567
3568 cFlags, ok := cc.Args["cFlags"]
3569 if !ok {
3570 t.Errorf("Could not find cFlags for module %s and variant %s",
3571 modName, variantName)
3572 }
3573
3574 if strings.Contains(
3575 cFlags, "-fsanitize-coverage=trace-pc-guard") != shouldHave {
3576 t.Errorf("Flag was found: %t. Expected to find flag: %t. "+
3577 "Test failed for module %s and variant %s",
3578 !shouldHave, shouldHave, modName, variantName)
3579 }
3580 }
3581
Cory Barkera1da26f2022-06-07 20:12:06 +00003582 moduleName := "test_afl_fuzz_target"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003583 checkPcGuardFlag(moduleName, variant+"_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003584
3585 moduleName = "afl_fuzz_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003586 checkPcGuardFlag(moduleName, variant+"_static", false)
3587 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003588
3589 moduleName = "second_static_lib"
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003590 checkPcGuardFlag(moduleName, variant+"_static", false)
3591 checkPcGuardFlag(moduleName, variant+"_static_fuzzer", true)
Cory Barkera1da26f2022-06-07 20:12:06 +00003592
3593 ctx.ModuleForTests("afl_fuzz_shared_lib",
3594 "android_arm64_armv8-a_shared").Rule("cc")
3595 ctx.ModuleForTests("afl_fuzz_shared_lib",
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003596 "android_arm64_armv8-a_shared_fuzzer").Rule("cc")
3597}
3598
3599func TestAFLFuzzTargetForDevice(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003600 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003601 VerifyAFLFuzzTargetVariant(t, "android_arm64_armv8-a")
3602}
3603
3604func TestAFLFuzzTargetForLinuxHost(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003605 t.Parallel()
Cory Barker9cfcf6d2022-07-22 17:22:02 +00003606 if runtime.GOOS != "linux" {
3607 t.Skip("requires linux")
3608 }
3609
3610 VerifyAFLFuzzTargetVariant(t, "linux_glibc_x86_64")
Cory Barkera1da26f2022-06-07 20:12:06 +00003611}
3612
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003613// Simple smoke test for the cc_fuzz target that ensures the rule compiles
3614// correctly.
3615func TestFuzzTarget(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003616 t.Parallel()
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003617 ctx := testCc(t, `
3618 cc_fuzz {
3619 name: "fuzz_smoke_test",
3620 srcs: ["foo.c"],
3621 }`)
3622
Paul Duffin075c4172019-12-19 19:06:13 +00003623 variant := "android_arm64_armv8-a_fuzzer"
Mitch Phillipsda9a4632019-07-15 09:34:09 -07003624 ctx.ModuleForTests("fuzz_smoke_test", variant).Rule("cc")
3625}
3626
Jooyung Han38002912019-05-16 04:01:54 +09003627func assertString(t *testing.T, got, expected string) {
3628 t.Helper()
3629 if got != expected {
3630 t.Errorf("expected %q got %q", expected, got)
3631 }
3632}
3633
3634func assertArrayString(t *testing.T, got, expected []string) {
3635 t.Helper()
3636 if len(got) != len(expected) {
3637 t.Errorf("expected %d (%q) got (%d) %q", len(expected), expected, len(got), got)
3638 return
3639 }
3640 for i := range got {
3641 if got[i] != expected[i] {
3642 t.Errorf("expected %d-th %q (%q) got %q (%q)",
3643 i, expected[i], expected, got[i], got)
3644 return
3645 }
3646 }
3647}
Colin Crosse1bb5d02019-09-24 14:55:04 -07003648
Jooyung Han0302a842019-10-30 18:43:49 +09003649func assertMapKeys(t *testing.T, m map[string]string, expected []string) {
3650 t.Helper()
3651 assertArrayString(t, android.SortedStringKeys(m), expected)
3652}
3653
Colin Crosse1bb5d02019-09-24 14:55:04 -07003654func TestDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003655 t.Parallel()
Colin Crosse1bb5d02019-09-24 14:55:04 -07003656 ctx := testCc(t, `
3657 cc_defaults {
3658 name: "defaults",
3659 srcs: ["foo.c"],
3660 static: {
3661 srcs: ["bar.c"],
3662 },
3663 shared: {
3664 srcs: ["baz.c"],
3665 },
Liz Kammer3cf52112021-03-31 15:42:03 -04003666 bazel_module: {
3667 bp2build_available: true,
3668 },
Colin Crosse1bb5d02019-09-24 14:55:04 -07003669 }
3670
3671 cc_library_static {
3672 name: "libstatic",
3673 defaults: ["defaults"],
3674 }
3675
3676 cc_library_shared {
3677 name: "libshared",
3678 defaults: ["defaults"],
3679 }
3680
3681 cc_library {
3682 name: "libboth",
3683 defaults: ["defaults"],
3684 }
3685
3686 cc_binary {
3687 name: "binary",
3688 defaults: ["defaults"],
3689 }`)
3690
Colin Cross7113d202019-11-20 16:39:12 -08003691 shared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003692 if g, w := pathsToBase(shared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3693 t.Errorf("libshared ld rule wanted %q, got %q", w, g)
3694 }
Colin Cross7113d202019-11-20 16:39:12 -08003695 bothShared := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_shared").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003696 if g, w := pathsToBase(bothShared.Inputs), []string{"foo.o", "baz.o"}; !reflect.DeepEqual(w, g) {
3697 t.Errorf("libboth ld rule wanted %q, got %q", w, g)
3698 }
Colin Cross7113d202019-11-20 16:39:12 -08003699 binary := ctx.ModuleForTests("binary", "android_arm64_armv8-a").Rule("ld")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003700 if g, w := pathsToBase(binary.Inputs), []string{"foo.o"}; !reflect.DeepEqual(w, g) {
3701 t.Errorf("binary ld rule wanted %q, got %q", w, g)
3702 }
3703
Colin Cross7113d202019-11-20 16:39:12 -08003704 static := ctx.ModuleForTests("libstatic", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003705 if g, w := pathsToBase(static.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3706 t.Errorf("libstatic ar rule wanted %q, got %q", w, g)
3707 }
Colin Cross7113d202019-11-20 16:39:12 -08003708 bothStatic := ctx.ModuleForTests("libboth", "android_arm64_armv8-a_static").Rule("ar")
Colin Crosse1bb5d02019-09-24 14:55:04 -07003709 if g, w := pathsToBase(bothStatic.Inputs), []string{"foo.o", "bar.o"}; !reflect.DeepEqual(w, g) {
3710 t.Errorf("libboth ar rule wanted %q, got %q", w, g)
3711 }
3712}
Colin Crosseabaedd2020-02-06 17:01:55 -08003713
3714func TestProductVariableDefaults(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003715 t.Parallel()
Colin Crosseabaedd2020-02-06 17:01:55 -08003716 bp := `
3717 cc_defaults {
3718 name: "libfoo_defaults",
3719 srcs: ["foo.c"],
3720 cppflags: ["-DFOO"],
3721 product_variables: {
3722 debuggable: {
3723 cppflags: ["-DBAR"],
3724 },
3725 },
3726 }
3727
3728 cc_library {
3729 name: "libfoo",
3730 defaults: ["libfoo_defaults"],
3731 }
3732 `
3733
Paul Duffin8567f222021-03-23 00:02:06 +00003734 result := android.GroupFixturePreparers(
3735 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003736 android.PrepareForTestWithVariables,
Colin Crosseabaedd2020-02-06 17:01:55 -08003737
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003738 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
3739 variables.Debuggable = BoolPtr(true)
3740 }),
3741 ).RunTestWithBp(t, bp)
Colin Crosseabaedd2020-02-06 17:01:55 -08003742
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003743 libfoo := result.Module("libfoo", "android_arm64_armv8-a_static").(*Module)
Paul Duffine84b1332021-03-12 11:59:43 +00003744 android.AssertStringListContains(t, "cppflags", libfoo.flags.Local.CppFlags, "-DBAR")
Colin Crosseabaedd2020-02-06 17:01:55 -08003745}
Colin Crosse4f6eba2020-09-22 18:11:25 -07003746
3747func TestEmptyWholeStaticLibsAllowMissingDependencies(t *testing.T) {
3748 t.Parallel()
3749 bp := `
3750 cc_library_static {
3751 name: "libfoo",
3752 srcs: ["foo.c"],
3753 whole_static_libs: ["libbar"],
3754 }
3755
3756 cc_library_static {
3757 name: "libbar",
3758 whole_static_libs: ["libmissing"],
3759 }
3760 `
3761
Paul Duffin8567f222021-03-23 00:02:06 +00003762 result := android.GroupFixturePreparers(
3763 prepareForCcTest,
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003764 android.PrepareForTestWithAllowMissingDependencies,
3765 ).RunTestWithBp(t, bp)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003766
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003767 libbar := result.ModuleForTests("libbar", "android_arm64_armv8-a_static").Output("libbar.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003768 android.AssertDeepEquals(t, "libbar rule", android.ErrorRule, libbar.Rule)
Colin Crosse4f6eba2020-09-22 18:11:25 -07003769
Paul Duffine84b1332021-03-12 11:59:43 +00003770 android.AssertStringDoesContain(t, "libbar error", libbar.Args["error"], "missing dependencies: libmissing")
Colin Crosse4f6eba2020-09-22 18:11:25 -07003771
Paul Duffin7d8a8ad2021-03-07 15:58:39 +00003772 libfoo := result.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Output("libfoo.a")
Paul Duffine84b1332021-03-12 11:59:43 +00003773 android.AssertStringListContains(t, "libfoo.a dependencies", libfoo.Inputs.Strings(), libbar.Output.String())
Colin Crosse4f6eba2020-09-22 18:11:25 -07003774}
Colin Crosse9fe2942020-11-10 18:12:15 -08003775
3776func TestInstallSharedLibs(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003777 t.Parallel()
Colin Crosse9fe2942020-11-10 18:12:15 -08003778 bp := `
3779 cc_binary {
3780 name: "bin",
3781 host_supported: true,
3782 shared_libs: ["libshared"],
3783 runtime_libs: ["libruntime"],
3784 srcs: [":gen"],
3785 }
3786
3787 cc_library_shared {
3788 name: "libshared",
3789 host_supported: true,
3790 shared_libs: ["libtransitive"],
3791 }
3792
3793 cc_library_shared {
3794 name: "libtransitive",
3795 host_supported: true,
3796 }
3797
3798 cc_library_shared {
3799 name: "libruntime",
3800 host_supported: true,
3801 }
3802
3803 cc_binary_host {
3804 name: "tool",
3805 srcs: ["foo.cpp"],
3806 }
3807
3808 genrule {
3809 name: "gen",
3810 tools: ["tool"],
3811 out: ["gen.cpp"],
3812 cmd: "$(location tool) $(out)",
3813 }
3814 `
3815
Paul Duffinc3e6ce02021-03-22 23:21:32 +00003816 config := TestConfig(t.TempDir(), android.Android, nil, bp, nil)
Colin Crosse9fe2942020-11-10 18:12:15 -08003817 ctx := testCcWithConfig(t, config)
3818
3819 hostBin := ctx.ModuleForTests("bin", config.BuildOSTarget.String()).Description("install")
3820 hostShared := ctx.ModuleForTests("libshared", config.BuildOSTarget.String()+"_shared").Description("install")
3821 hostRuntime := ctx.ModuleForTests("libruntime", config.BuildOSTarget.String()+"_shared").Description("install")
3822 hostTransitive := ctx.ModuleForTests("libtransitive", config.BuildOSTarget.String()+"_shared").Description("install")
3823 hostTool := ctx.ModuleForTests("tool", config.BuildOSTarget.String()).Description("install")
3824
3825 if g, w := hostBin.Implicits.Strings(), hostShared.Output.String(); !android.InList(w, g) {
3826 t.Errorf("expected host bin dependency %q, got %q", w, g)
3827 }
3828
3829 if g, w := hostBin.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3830 t.Errorf("expected host bin dependency %q, got %q", w, g)
3831 }
3832
3833 if g, w := hostShared.Implicits.Strings(), hostTransitive.Output.String(); !android.InList(w, g) {
3834 t.Errorf("expected host bin dependency %q, got %q", w, g)
3835 }
3836
3837 if g, w := hostBin.Implicits.Strings(), hostRuntime.Output.String(); !android.InList(w, g) {
3838 t.Errorf("expected host bin dependency %q, got %q", w, g)
3839 }
3840
3841 if g, w := hostBin.Implicits.Strings(), hostTool.Output.String(); android.InList(w, g) {
3842 t.Errorf("expected no host bin dependency %q, got %q", w, g)
3843 }
3844
3845 deviceBin := ctx.ModuleForTests("bin", "android_arm64_armv8-a").Description("install")
3846 deviceShared := ctx.ModuleForTests("libshared", "android_arm64_armv8-a_shared").Description("install")
3847 deviceTransitive := ctx.ModuleForTests("libtransitive", "android_arm64_armv8-a_shared").Description("install")
3848 deviceRuntime := ctx.ModuleForTests("libruntime", "android_arm64_armv8-a_shared").Description("install")
3849
3850 if g, w := deviceBin.OrderOnly.Strings(), deviceShared.Output.String(); !android.InList(w, g) {
3851 t.Errorf("expected device bin dependency %q, got %q", w, g)
3852 }
3853
3854 if g, w := deviceBin.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3855 t.Errorf("expected device bin dependency %q, got %q", w, g)
3856 }
3857
3858 if g, w := deviceShared.OrderOnly.Strings(), deviceTransitive.Output.String(); !android.InList(w, g) {
3859 t.Errorf("expected device bin dependency %q, got %q", w, g)
3860 }
3861
3862 if g, w := deviceBin.OrderOnly.Strings(), deviceRuntime.Output.String(); !android.InList(w, g) {
3863 t.Errorf("expected device bin dependency %q, got %q", w, g)
3864 }
3865
3866 if g, w := deviceBin.OrderOnly.Strings(), hostTool.Output.String(); android.InList(w, g) {
3867 t.Errorf("expected no device bin dependency %q, got %q", w, g)
3868 }
3869
3870}
Jiyong Park1ad8e162020-12-01 23:40:09 +09003871
3872func TestStubsLibReexportsHeaders(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003873 t.Parallel()
Jiyong Park1ad8e162020-12-01 23:40:09 +09003874 ctx := testCc(t, `
3875 cc_library_shared {
3876 name: "libclient",
3877 srcs: ["foo.c"],
3878 shared_libs: ["libfoo#1"],
3879 }
3880
3881 cc_library_shared {
3882 name: "libfoo",
3883 srcs: ["foo.c"],
3884 shared_libs: ["libbar"],
3885 export_shared_lib_headers: ["libbar"],
3886 stubs: {
3887 symbol_file: "foo.map.txt",
3888 versions: ["1", "2", "3"],
3889 },
3890 }
3891
3892 cc_library_shared {
3893 name: "libbar",
3894 export_include_dirs: ["include/libbar"],
3895 srcs: ["foo.c"],
3896 }`)
3897
3898 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3899
3900 if !strings.Contains(cFlags, "-Iinclude/libbar") {
3901 t.Errorf("expected %q in cflags, got %q", "-Iinclude/libbar", cFlags)
3902 }
3903}
Jooyung Hane197d8b2021-01-05 10:33:16 +09003904
3905func TestAidlFlagsPassedToTheAidlCompiler(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003906 t.Parallel()
Jooyung Hane197d8b2021-01-05 10:33:16 +09003907 ctx := testCc(t, `
3908 cc_library {
3909 name: "libfoo",
3910 srcs: ["a/Foo.aidl"],
3911 aidl: { flags: ["-Werror"], },
3912 }
3913 `)
3914
3915 libfoo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static")
3916 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3917 aidlCommand := manifest.Commands[0].GetCommand()
3918 expectedAidlFlag := "-Werror"
3919 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3920 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3921 }
3922}
Evgenii Stepanov193ac2e2020-04-28 15:09:12 -07003923
Jooyung Han07f70c02021-11-06 07:08:45 +09003924func TestAidlFlagsWithMinSdkVersion(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003925 t.Parallel()
Jooyung Han07f70c02021-11-06 07:08:45 +09003926 for _, tc := range []struct {
3927 name string
3928 sdkVersion string
3929 variant string
3930 expected string
3931 }{
3932 {
3933 name: "default is current",
3934 sdkVersion: "",
3935 variant: "android_arm64_armv8-a_static",
3936 expected: "platform_apis",
3937 },
3938 {
3939 name: "use sdk_version",
3940 sdkVersion: `sdk_version: "29"`,
3941 variant: "android_arm64_armv8-a_static",
3942 expected: "platform_apis",
3943 },
3944 {
3945 name: "use sdk_version(sdk variant)",
3946 sdkVersion: `sdk_version: "29"`,
3947 variant: "android_arm64_armv8-a_sdk_static",
3948 expected: "29",
3949 },
3950 {
3951 name: "use min_sdk_version",
3952 sdkVersion: `min_sdk_version: "29"`,
3953 variant: "android_arm64_armv8-a_static",
3954 expected: "29",
3955 },
3956 } {
3957 t.Run(tc.name, func(t *testing.T) {
3958 ctx := testCc(t, `
3959 cc_library {
3960 name: "libfoo",
3961 stl: "none",
3962 srcs: ["a/Foo.aidl"],
3963 `+tc.sdkVersion+`
3964 }
3965 `)
3966 libfoo := ctx.ModuleForTests("libfoo", tc.variant)
3967 manifest := android.RuleBuilderSboxProtoForTests(t, libfoo.Output("aidl.sbox.textproto"))
3968 aidlCommand := manifest.Commands[0].GetCommand()
3969 expectedAidlFlag := "--min_sdk_version=" + tc.expected
3970 if !strings.Contains(aidlCommand, expectedAidlFlag) {
3971 t.Errorf("aidl command %q does not contain %q", aidlCommand, expectedAidlFlag)
3972 }
3973 })
3974 }
3975}
3976
Jiyong Parka008fb02021-03-16 17:15:53 +09003977func TestMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003978 t.Parallel()
Jiyong Parka008fb02021-03-16 17:15:53 +09003979 ctx := testCc(t, `
3980 cc_library_shared {
3981 name: "libfoo",
3982 srcs: ["foo.c"],
3983 min_sdk_version: "29",
3984 }`)
3985
3986 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
3987 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android29")
3988}
3989
Vinh Tranf1924742022-06-24 16:40:11 -04003990func TestNonDigitMinSdkVersionInClangTriple(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04003991 t.Parallel()
Vinh Tranf1924742022-06-24 16:40:11 -04003992 bp := `
3993 cc_library_shared {
3994 name: "libfoo",
3995 srcs: ["foo.c"],
3996 min_sdk_version: "S",
3997 }
3998 `
3999 result := android.GroupFixturePreparers(
4000 prepareForCcTest,
4001 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4002 variables.Platform_version_active_codenames = []string{"UpsideDownCake", "Tiramisu"}
4003 }),
4004 ).RunTestWithBp(t, bp)
4005 ctx := result.TestContext
4006 cFlags := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4007 android.AssertStringDoesContain(t, "min sdk version", cFlags, "-target aarch64-linux-android31")
4008}
4009
Paul Duffin3cb603e2021-02-19 13:57:10 +00004010func TestIncludeDirsExporting(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004011 t.Parallel()
Paul Duffin3cb603e2021-02-19 13:57:10 +00004012
4013 // Trim spaces from the beginning, end and immediately after any newline characters. Leaves
4014 // embedded newline characters alone.
4015 trimIndentingSpaces := func(s string) string {
4016 return strings.TrimSpace(regexp.MustCompile("(^|\n)\\s+").ReplaceAllString(s, "$1"))
4017 }
4018
4019 checkPaths := func(t *testing.T, message string, expected string, paths android.Paths) {
4020 t.Helper()
4021 expected = trimIndentingSpaces(expected)
4022 actual := trimIndentingSpaces(strings.Join(android.FirstUniqueStrings(android.NormalizePathsForTesting(paths)), "\n"))
4023 if expected != actual {
4024 t.Errorf("%s: expected:\n%s\n actual:\n%s\n", message, expected, actual)
4025 }
4026 }
4027
4028 type exportedChecker func(t *testing.T, name string, exported FlagExporterInfo)
4029
4030 checkIncludeDirs := func(t *testing.T, ctx *android.TestContext, module android.Module, checkers ...exportedChecker) {
4031 t.Helper()
4032 exported := ctx.ModuleProvider(module, FlagExporterInfoProvider).(FlagExporterInfo)
4033 name := module.Name()
4034
4035 for _, checker := range checkers {
4036 checker(t, name, exported)
4037 }
4038 }
4039
4040 expectedIncludeDirs := func(expectedPaths string) exportedChecker {
4041 return func(t *testing.T, name string, exported FlagExporterInfo) {
4042 t.Helper()
4043 checkPaths(t, fmt.Sprintf("%s: include dirs", name), expectedPaths, exported.IncludeDirs)
4044 }
4045 }
4046
4047 expectedSystemIncludeDirs := func(expectedPaths string) exportedChecker {
4048 return func(t *testing.T, name string, exported FlagExporterInfo) {
4049 t.Helper()
4050 checkPaths(t, fmt.Sprintf("%s: system include dirs", name), expectedPaths, exported.SystemIncludeDirs)
4051 }
4052 }
4053
4054 expectedGeneratedHeaders := func(expectedPaths string) exportedChecker {
4055 return func(t *testing.T, name string, exported FlagExporterInfo) {
4056 t.Helper()
4057 checkPaths(t, fmt.Sprintf("%s: generated headers", name), expectedPaths, exported.GeneratedHeaders)
4058 }
4059 }
4060
4061 expectedOrderOnlyDeps := func(expectedPaths string) exportedChecker {
4062 return func(t *testing.T, name string, exported FlagExporterInfo) {
4063 t.Helper()
4064 checkPaths(t, fmt.Sprintf("%s: order only deps", name), expectedPaths, exported.Deps)
4065 }
4066 }
4067
4068 genRuleModules := `
4069 genrule {
4070 name: "genrule_foo",
4071 cmd: "generate-foo",
4072 out: [
4073 "generated_headers/foo/generated_header.h",
4074 ],
4075 export_include_dirs: [
4076 "generated_headers",
4077 ],
4078 }
4079
4080 genrule {
4081 name: "genrule_bar",
4082 cmd: "generate-bar",
4083 out: [
4084 "generated_headers/bar/generated_header.h",
4085 ],
4086 export_include_dirs: [
4087 "generated_headers",
4088 ],
4089 }
4090 `
4091
4092 t.Run("ensure exported include dirs are not automatically re-exported from shared_libs", func(t *testing.T) {
4093 ctx := testCc(t, genRuleModules+`
4094 cc_library {
4095 name: "libfoo",
4096 srcs: ["foo.c"],
4097 export_include_dirs: ["foo/standard"],
4098 export_system_include_dirs: ["foo/system"],
4099 generated_headers: ["genrule_foo"],
4100 export_generated_headers: ["genrule_foo"],
4101 }
4102
4103 cc_library {
4104 name: "libbar",
4105 srcs: ["bar.c"],
4106 shared_libs: ["libfoo"],
4107 export_include_dirs: ["bar/standard"],
4108 export_system_include_dirs: ["bar/system"],
4109 generated_headers: ["genrule_bar"],
4110 export_generated_headers: ["genrule_bar"],
4111 }
4112 `)
4113 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4114 checkIncludeDirs(t, ctx, foo,
4115 expectedIncludeDirs(`
4116 foo/standard
4117 .intermediates/genrule_foo/gen/generated_headers
4118 `),
4119 expectedSystemIncludeDirs(`foo/system`),
4120 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4121 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4122 )
4123
4124 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4125 checkIncludeDirs(t, ctx, bar,
4126 expectedIncludeDirs(`
4127 bar/standard
4128 .intermediates/genrule_bar/gen/generated_headers
4129 `),
4130 expectedSystemIncludeDirs(`bar/system`),
4131 expectedGeneratedHeaders(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4132 expectedOrderOnlyDeps(`.intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h`),
4133 )
4134 })
4135
4136 t.Run("ensure exported include dirs are automatically re-exported from whole_static_libs", func(t *testing.T) {
4137 ctx := testCc(t, genRuleModules+`
4138 cc_library {
4139 name: "libfoo",
4140 srcs: ["foo.c"],
4141 export_include_dirs: ["foo/standard"],
4142 export_system_include_dirs: ["foo/system"],
4143 generated_headers: ["genrule_foo"],
4144 export_generated_headers: ["genrule_foo"],
4145 }
4146
4147 cc_library {
4148 name: "libbar",
4149 srcs: ["bar.c"],
4150 whole_static_libs: ["libfoo"],
4151 export_include_dirs: ["bar/standard"],
4152 export_system_include_dirs: ["bar/system"],
4153 generated_headers: ["genrule_bar"],
4154 export_generated_headers: ["genrule_bar"],
4155 }
4156 `)
4157 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4158 checkIncludeDirs(t, ctx, foo,
4159 expectedIncludeDirs(`
4160 foo/standard
4161 .intermediates/genrule_foo/gen/generated_headers
4162 `),
4163 expectedSystemIncludeDirs(`foo/system`),
4164 expectedGeneratedHeaders(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4165 expectedOrderOnlyDeps(`.intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h`),
4166 )
4167
4168 bar := ctx.ModuleForTests("libbar", "android_arm64_armv8-a_shared").Module()
4169 checkIncludeDirs(t, ctx, bar,
4170 expectedIncludeDirs(`
4171 bar/standard
4172 foo/standard
4173 .intermediates/genrule_foo/gen/generated_headers
4174 .intermediates/genrule_bar/gen/generated_headers
4175 `),
4176 expectedSystemIncludeDirs(`
4177 bar/system
4178 foo/system
4179 `),
4180 expectedGeneratedHeaders(`
4181 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4182 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4183 `),
4184 expectedOrderOnlyDeps(`
4185 .intermediates/genrule_foo/gen/generated_headers/foo/generated_header.h
4186 .intermediates/genrule_bar/gen/generated_headers/bar/generated_header.h
4187 `),
4188 )
4189 })
4190
Paul Duffin3cb603e2021-02-19 13:57:10 +00004191 t.Run("ensure only aidl headers are exported", func(t *testing.T) {
4192 ctx := testCc(t, genRuleModules+`
4193 cc_library_shared {
4194 name: "libfoo",
4195 srcs: [
4196 "foo.c",
4197 "b.aidl",
4198 "a.proto",
4199 ],
4200 aidl: {
4201 export_aidl_headers: true,
4202 }
4203 }
4204 `)
4205 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4206 checkIncludeDirs(t, ctx, foo,
4207 expectedIncludeDirs(`
4208 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl
4209 `),
4210 expectedSystemIncludeDirs(``),
4211 expectedGeneratedHeaders(`
4212 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4213 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4214 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004215 `),
4216 expectedOrderOnlyDeps(`
4217 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/b.h
4218 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bnb.h
4219 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/aidl/Bpb.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004220 `),
4221 )
4222 })
4223
Paul Duffin3cb603e2021-02-19 13:57:10 +00004224 t.Run("ensure only proto headers are exported", func(t *testing.T) {
4225 ctx := testCc(t, genRuleModules+`
4226 cc_library_shared {
4227 name: "libfoo",
4228 srcs: [
4229 "foo.c",
4230 "b.aidl",
4231 "a.proto",
4232 ],
4233 proto: {
4234 export_proto_headers: true,
4235 }
4236 }
4237 `)
4238 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4239 checkIncludeDirs(t, ctx, foo,
4240 expectedIncludeDirs(`
4241 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto
4242 `),
4243 expectedSystemIncludeDirs(``),
4244 expectedGeneratedHeaders(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004245 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4246 `),
4247 expectedOrderOnlyDeps(`
Paul Duffin3cb603e2021-02-19 13:57:10 +00004248 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/proto/a.pb.h
4249 `),
4250 )
4251 })
4252
Paul Duffin33056e82021-02-19 13:49:08 +00004253 t.Run("ensure only sysprop headers are exported", func(t *testing.T) {
Paul Duffin3cb603e2021-02-19 13:57:10 +00004254 ctx := testCc(t, genRuleModules+`
4255 cc_library_shared {
4256 name: "libfoo",
4257 srcs: [
4258 "foo.c",
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004259 "path/to/a.sysprop",
Paul Duffin3cb603e2021-02-19 13:57:10 +00004260 "b.aidl",
4261 "a.proto",
4262 ],
4263 }
4264 `)
4265 foo := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module()
4266 checkIncludeDirs(t, ctx, foo,
4267 expectedIncludeDirs(`
4268 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include
4269 `),
4270 expectedSystemIncludeDirs(``),
4271 expectedGeneratedHeaders(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004272 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004273 `),
4274 expectedOrderOnlyDeps(`
Trevor Radcliffe3092a8e2022-08-24 15:25:25 +00004275 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/include/path/to/a.sysprop.h
4276 .intermediates/libfoo/android_arm64_armv8-a_shared/gen/sysprop/public/include/path/to/a.sysprop.h
Paul Duffin3cb603e2021-02-19 13:57:10 +00004277 `),
4278 )
4279 })
4280}
Colin Crossae628182021-06-14 16:52:28 -07004281
4282func TestIncludeDirectoryOrdering(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004283 t.Parallel()
Liz Kammer08572c62021-09-30 10:11:04 -04004284 baseExpectedFlags := []string{
4285 "${config.ArmThumbCflags}",
4286 "${config.ArmCflags}",
4287 "${config.CommonGlobalCflags}",
4288 "${config.DeviceGlobalCflags}",
4289 "${config.ExternalCflags}",
4290 "${config.ArmToolchainCflags}",
4291 "${config.ArmArmv7ANeonCflags}",
4292 "${config.ArmGenericCflags}",
4293 "-target",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004294 "armv7a-linux-androideabi21",
Liz Kammer08572c62021-09-30 10:11:04 -04004295 }
4296
4297 expectedIncludes := []string{
4298 "external/foo/android_arm_export_include_dirs",
4299 "external/foo/lib32_export_include_dirs",
4300 "external/foo/arm_export_include_dirs",
4301 "external/foo/android_export_include_dirs",
4302 "external/foo/linux_export_include_dirs",
4303 "external/foo/export_include_dirs",
4304 "external/foo/android_arm_local_include_dirs",
4305 "external/foo/lib32_local_include_dirs",
4306 "external/foo/arm_local_include_dirs",
4307 "external/foo/android_local_include_dirs",
4308 "external/foo/linux_local_include_dirs",
4309 "external/foo/local_include_dirs",
4310 "external/foo",
4311 "external/foo/libheader1",
4312 "external/foo/libheader2",
4313 "external/foo/libwhole1",
4314 "external/foo/libwhole2",
4315 "external/foo/libstatic1",
4316 "external/foo/libstatic2",
4317 "external/foo/libshared1",
4318 "external/foo/libshared2",
4319 "external/foo/liblinux",
4320 "external/foo/libandroid",
4321 "external/foo/libarm",
4322 "external/foo/lib32",
4323 "external/foo/libandroid_arm",
4324 "defaults/cc/common/ndk_libc++_shared",
Liz Kammer08572c62021-09-30 10:11:04 -04004325 }
4326
4327 conly := []string{"-fPIC", "${config.CommonGlobalConlyflags}"}
4328 cppOnly := []string{"-fPIC", "${config.CommonGlobalCppflags}", "${config.DeviceGlobalCppflags}", "${config.ArmCppflags}"}
4329
Elliott Hughesed4a27b2022-05-18 13:15:00 -07004330 cflags := []string{"-Werror", "-std=candcpp"}
Elliott Hughesab5e4c62022-03-28 16:47:17 -07004331 cstd := []string{"-std=gnu11", "-std=conly"}
Liz Kammer9dc65772021-12-16 11:38:50 -05004332 cppstd := []string{"-std=gnu++17", "-std=cpp", "-fno-rtti"}
Liz Kammer08572c62021-09-30 10:11:04 -04004333
4334 lastIncludes := []string{
4335 "out/soong/ndk/sysroot/usr/include",
4336 "out/soong/ndk/sysroot/usr/include/arm-linux-androideabi",
4337 }
4338
4339 combineSlices := func(slices ...[]string) []string {
4340 var ret []string
4341 for _, s := range slices {
4342 ret = append(ret, s...)
4343 }
4344 return ret
4345 }
4346
4347 testCases := []struct {
4348 name string
4349 src string
4350 expected []string
4351 }{
4352 {
4353 name: "c",
4354 src: "foo.c",
Stephen Hinese24303f2021-12-14 15:07:08 -08004355 expected: combineSlices(baseExpectedFlags, conly, expectedIncludes, cflags, cstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004356 },
4357 {
4358 name: "cc",
4359 src: "foo.cc",
Stephen Hinese24303f2021-12-14 15:07:08 -08004360 expected: combineSlices(baseExpectedFlags, cppOnly, expectedIncludes, cflags, cppstd, lastIncludes, []string{"${config.NoOverrideGlobalCflags}", "${config.NoOverrideExternalGlobalCflags}"}),
Liz Kammer08572c62021-09-30 10:11:04 -04004361 },
4362 {
4363 name: "assemble",
4364 src: "foo.s",
Liz Kammere4d1bda2022-06-22 21:02:08 +00004365 expected: combineSlices(baseExpectedFlags, []string{"${config.CommonGlobalAsflags}"}, expectedIncludes, lastIncludes),
Liz Kammer08572c62021-09-30 10:11:04 -04004366 },
4367 }
4368
4369 for _, tc := range testCases {
4370 t.Run(tc.name, func(t *testing.T) {
4371 bp := fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004372 cc_library {
4373 name: "libfoo",
Liz Kammer08572c62021-09-30 10:11:04 -04004374 srcs: ["%s"],
Liz Kammer9dc65772021-12-16 11:38:50 -05004375 cflags: ["-std=candcpp"],
4376 conlyflags: ["-std=conly"],
4377 cppflags: ["-std=cpp"],
Colin Crossae628182021-06-14 16:52:28 -07004378 local_include_dirs: ["local_include_dirs"],
4379 export_include_dirs: ["export_include_dirs"],
4380 export_system_include_dirs: ["export_system_include_dirs"],
4381 static_libs: ["libstatic1", "libstatic2"],
4382 whole_static_libs: ["libwhole1", "libwhole2"],
4383 shared_libs: ["libshared1", "libshared2"],
4384 header_libs: ["libheader1", "libheader2"],
4385 target: {
4386 android: {
4387 shared_libs: ["libandroid"],
4388 local_include_dirs: ["android_local_include_dirs"],
4389 export_include_dirs: ["android_export_include_dirs"],
4390 },
4391 android_arm: {
4392 shared_libs: ["libandroid_arm"],
4393 local_include_dirs: ["android_arm_local_include_dirs"],
4394 export_include_dirs: ["android_arm_export_include_dirs"],
4395 },
4396 linux: {
4397 shared_libs: ["liblinux"],
4398 local_include_dirs: ["linux_local_include_dirs"],
4399 export_include_dirs: ["linux_export_include_dirs"],
4400 },
4401 },
4402 multilib: {
4403 lib32: {
4404 shared_libs: ["lib32"],
4405 local_include_dirs: ["lib32_local_include_dirs"],
4406 export_include_dirs: ["lib32_export_include_dirs"],
4407 },
4408 },
4409 arch: {
4410 arm: {
4411 shared_libs: ["libarm"],
4412 local_include_dirs: ["arm_local_include_dirs"],
4413 export_include_dirs: ["arm_export_include_dirs"],
4414 },
4415 },
4416 stl: "libc++",
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004417 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004418 }
4419
4420 cc_library_headers {
4421 name: "libheader1",
4422 export_include_dirs: ["libheader1"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004423 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004424 stl: "none",
4425 }
4426
4427 cc_library_headers {
4428 name: "libheader2",
4429 export_include_dirs: ["libheader2"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004430 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004431 stl: "none",
4432 }
Liz Kammer08572c62021-09-30 10:11:04 -04004433 `, tc.src)
Colin Crossae628182021-06-14 16:52:28 -07004434
Liz Kammer08572c62021-09-30 10:11:04 -04004435 libs := []string{
4436 "libstatic1",
4437 "libstatic2",
4438 "libwhole1",
4439 "libwhole2",
4440 "libshared1",
4441 "libshared2",
4442 "libandroid",
4443 "libandroid_arm",
4444 "liblinux",
4445 "lib32",
4446 "libarm",
4447 }
Colin Crossae628182021-06-14 16:52:28 -07004448
Liz Kammer08572c62021-09-30 10:11:04 -04004449 for _, lib := range libs {
4450 bp += fmt.Sprintf(`
Colin Crossae628182021-06-14 16:52:28 -07004451 cc_library {
4452 name: "%s",
4453 export_include_dirs: ["%s"],
Dan Albert6bfb6bb2022-08-17 20:11:57 +00004454 sdk_version: "minimum",
Colin Crossae628182021-06-14 16:52:28 -07004455 stl: "none",
4456 }
4457 `, lib, lib)
Liz Kammer08572c62021-09-30 10:11:04 -04004458 }
4459
4460 ctx := android.GroupFixturePreparers(
4461 PrepareForIntegrationTestWithCc,
4462 android.FixtureAddTextFile("external/foo/Android.bp", bp),
4463 ).RunTest(t)
4464 // Use the arm variant instead of the arm64 variant so that it gets headers from
4465 // ndk_libandroid_support to test LateStaticLibs.
4466 cflags := ctx.ModuleForTests("libfoo", "android_arm_armv7-a-neon_sdk_static").Output("obj/external/foo/foo.o").Args["cFlags"]
4467
4468 var includes []string
4469 flags := strings.Split(cflags, " ")
4470 for _, flag := range flags {
4471 if strings.HasPrefix(flag, "-I") {
4472 includes = append(includes, strings.TrimPrefix(flag, "-I"))
4473 } else if flag == "-isystem" {
4474 // skip isystem, include next
4475 } else if len(flag) > 0 {
4476 includes = append(includes, flag)
4477 }
4478 }
4479
4480 android.AssertArrayString(t, "includes", tc.expected, includes)
4481 })
Colin Crossae628182021-06-14 16:52:28 -07004482 }
4483
Colin Crossae628182021-06-14 16:52:28 -07004484}
Alixb5f6d9e2022-04-20 23:00:58 +00004485
zijunzhao933e3802023-01-12 07:26:20 +00004486func TestAddnoOverride64GlobalCflags(t *testing.T) {
4487 t.Parallel()
4488 ctx := testCc(t, `
4489 cc_library_shared {
4490 name: "libclient",
4491 srcs: ["foo.c"],
4492 shared_libs: ["libfoo#1"],
4493 }
4494
4495 cc_library_shared {
4496 name: "libfoo",
4497 srcs: ["foo.c"],
4498 shared_libs: ["libbar"],
4499 export_shared_lib_headers: ["libbar"],
4500 stubs: {
4501 symbol_file: "foo.map.txt",
4502 versions: ["1", "2", "3"],
4503 },
4504 }
4505
4506 cc_library_shared {
4507 name: "libbar",
4508 export_include_dirs: ["include/libbar"],
4509 srcs: ["foo.c"],
4510 }`)
4511
4512 cFlags := ctx.ModuleForTests("libclient", "android_arm64_armv8-a_shared").Rule("cc").Args["cFlags"]
4513
4514 if !strings.Contains(cFlags, "${config.NoOverride64GlobalCflags}") {
4515 t.Errorf("expected %q in cflags, got %q", "${config.NoOverride64GlobalCflags}", cFlags)
4516 }
4517}
4518
Alixb5f6d9e2022-04-20 23:00:58 +00004519func TestCcBuildBrokenClangProperty(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004520 t.Parallel()
Alixb5f6d9e2022-04-20 23:00:58 +00004521 tests := []struct {
4522 name string
4523 clang bool
4524 BuildBrokenClangProperty bool
4525 err string
4526 }{
4527 {
4528 name: "error when clang is set to false",
4529 clang: false,
4530 err: "is no longer supported",
4531 },
4532 {
4533 name: "error when clang is set to true",
4534 clang: true,
4535 err: "property is deprecated, see Changes.md",
4536 },
4537 {
4538 name: "no error when BuildBrokenClangProperty is explicitly set to true",
4539 clang: true,
4540 BuildBrokenClangProperty: true,
4541 },
4542 }
4543
4544 for _, test := range tests {
4545 t.Run(test.name, func(t *testing.T) {
4546 bp := fmt.Sprintf(`
4547 cc_library {
4548 name: "foo",
4549 clang: %t,
4550 }`, test.clang)
4551
4552 if test.err == "" {
4553 android.GroupFixturePreparers(
4554 prepareForCcTest,
4555 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4556 if test.BuildBrokenClangProperty {
4557 variables.BuildBrokenClangProperty = test.BuildBrokenClangProperty
4558 }
4559 }),
4560 ).RunTestWithBp(t, bp)
4561 } else {
4562 prepareForCcTest.
4563 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4564 RunTestWithBp(t, bp)
4565 }
4566 })
4567 }
4568}
Alix Espinoef47e542022-09-14 19:10:51 +00004569
4570func TestCcBuildBrokenClangAsFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004571 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004572 tests := []struct {
4573 name string
4574 clangAsFlags []string
4575 BuildBrokenClangAsFlags bool
4576 err string
4577 }{
4578 {
4579 name: "error when clang_asflags is set",
4580 clangAsFlags: []string{"-a", "-b"},
4581 err: "clang_asflags: property is deprecated",
4582 },
4583 {
4584 name: "no error when BuildBrokenClangAsFlags is explicitly set to true",
4585 clangAsFlags: []string{"-a", "-b"},
4586 BuildBrokenClangAsFlags: true,
4587 },
4588 }
4589
4590 for _, test := range tests {
4591 t.Run(test.name, func(t *testing.T) {
4592 bp := fmt.Sprintf(`
4593 cc_library {
4594 name: "foo",
4595 clang_asflags: %s,
4596 }`, `["`+strings.Join(test.clangAsFlags, `","`)+`"]`)
4597
4598 if test.err == "" {
4599 android.GroupFixturePreparers(
4600 prepareForCcTest,
4601 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4602 if test.BuildBrokenClangAsFlags {
4603 variables.BuildBrokenClangAsFlags = test.BuildBrokenClangAsFlags
4604 }
4605 }),
4606 ).RunTestWithBp(t, bp)
4607 } else {
4608 prepareForCcTest.
4609 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4610 RunTestWithBp(t, bp)
4611 }
4612 })
4613 }
4614}
4615
4616func TestCcBuildBrokenClangCFlags(t *testing.T) {
Liz Kammer7c5d1592022-10-31 16:27:38 -04004617 t.Parallel()
Alix Espinoef47e542022-09-14 19:10:51 +00004618 tests := []struct {
4619 name string
4620 clangCFlags []string
4621 BuildBrokenClangCFlags bool
4622 err string
4623 }{
4624 {
4625 name: "error when clang_cflags is set",
4626 clangCFlags: []string{"-a", "-b"},
4627 err: "clang_cflags: property is deprecated",
4628 },
4629 {
4630 name: "no error when BuildBrokenClangCFlags is explicitly set to true",
4631 clangCFlags: []string{"-a", "-b"},
4632 BuildBrokenClangCFlags: true,
4633 },
4634 }
4635
4636 for _, test := range tests {
4637 t.Run(test.name, func(t *testing.T) {
4638 bp := fmt.Sprintf(`
4639 cc_library {
4640 name: "foo",
4641 clang_cflags: %s,
4642 }`, `["`+strings.Join(test.clangCFlags, `","`)+`"]`)
4643
4644 if test.err == "" {
4645 android.GroupFixturePreparers(
4646 prepareForCcTest,
4647 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
4648 if test.BuildBrokenClangCFlags {
4649 variables.BuildBrokenClangCFlags = test.BuildBrokenClangCFlags
4650 }
4651 }),
4652 ).RunTestWithBp(t, bp)
4653 } else {
4654 prepareForCcTest.
4655 ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(test.err)).
4656 RunTestWithBp(t, bp)
4657 }
4658 })
4659 }
4660}