blob: 53a097abaf2d8bd938409d8984edecaa6299cdba [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// Copyright 2016 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
15package cc
16
17import (
18 "path/filepath"
nelsonli0d7111e2019-09-17 16:35:23 +080019 "strconv"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "strings"
21
Julien Desprez3b933d32021-01-14 11:49:15 -080022 "github.com/google/blueprint/proptools"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Jingwen Chen537242c2022-08-24 11:53:27 +000025 "android/soong/bazel"
Jingwen Chen3952a902022-12-12 12:20:58 +000026 "android/soong/bazel/cquery"
Colin Cross303e21f2018-08-07 16:49:25 -070027 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070028)
29
Trevor Radcliffef389cb42022-03-24 21:06:14 +000030// TestLinkerProperties properties to be registered via the linker
31type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070032 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070033 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070034
Trevor Radcliffecd8fd672022-05-03 14:51:16 +000035 // if set, use the isolated gtest runner. Defaults to true if gtest is also true and the arch is Windows, false
36 // otherwise.
Christopher Ferris9df92d62018-08-21 12:40:08 -070037 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070038}
Colin Cross4d9c2d12016-07-29 12:48:20 -070039
Trevor Radcliffef389cb42022-03-24 21:06:14 +000040// TestInstallerProperties properties to be registered via the installer
41type TestInstallerProperties struct {
42 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
43 Test_suites []string `android:"arch_variant"`
44}
45
yelinhsieh9fc60402018-10-01 19:23:14 +080046// Test option struct.
47type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080048 android.CommonTestOptions
49
yelinhsieh9fc60402018-10-01 19:23:14 +080050 // The UID that you want to run the test as on a device.
51 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070052
David Srbecky519db272020-06-18 18:07:00 +010053 // A list of free-formed strings without spaces that categorize the test.
54 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070055
56 // a list of extra test configuration files that should be installed with the module.
57 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080058
Justin Yun46f66052021-05-04 18:42:24 +090059 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090060 // for the shipping api level is less than the min_shipping_api_level, skip this module.
61 Min_shipping_api_level *int64
62
63 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
64 // shipping api level and vendor api level properties are less than the
65 // vsr_min_shipping_api_level, skip this module.
66 // As this includes the shipping api level check, it is not allowed to define
67 // min_shipping_api_level at the same time with this property.
68 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090069
70 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090071 // integer value and the value is less than the min_vndk_version, skip this module.
72 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000073
74 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
75 // The "key" is optional in each of these.
76 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080077}
78
Colin Crossb916a382016-07-29 17:28:03 -070079type TestBinaryProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070080 // Create a separate binary for each source file. Useful when there is
81 // global state that can not be torn down and reset between each test suite.
82 Test_per_src *bool
Dan Willemsen3340d602016-12-27 14:40:40 -080083
84 // Disables the creation of a test-specific directory when used with
85 // relative_install_path. Useful if several tests need to be in the same
86 // directory, but test_per_src doesn't work.
87 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080088
89 // list of files or filegroup modules that provide data that should be installed alongside
90 // the test
Dan Shi67a88342020-02-25 16:34:39 -080091 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070092
Chris Parsons79d66a52020-06-05 17:26:16 -040093 // list of shared library modules that should be installed alongside the test
94 Data_libs []string `android:"arch_variant"`
95
Colin Crossc8caa062021-09-24 16:50:14 -070096 // list of binary modules that should be installed alongside the test
97 Data_bins []string `android:"arch_variant"`
98
Julien Despreze146e392018-08-02 15:00:46 -070099 // the name of the test configuration (for example "AndroidTest.xml") that should be
100 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800101 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700102
103 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
104 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800106
107 // Test options.
108 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700109
110 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
111 // with root permission.
112 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700113
114 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
115 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800116
Dan Shi6ffaaa82019-09-26 11:41:36 -0700117 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
118 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
119 // explicitly.
120 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800121
122 // Add parameterized mainline modules to auto generated test config. The options will be
123 // handled by TradeFed to download and install the specified modules on the device.
124 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700125
126 // Install the test into a folder named for the module in all test suites.
127 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700128}
129
130func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700131 android.RegisterModuleType("cc_test", TestFactory)
132 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
133 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
134 android.RegisterModuleType("cc_test_host", TestHostFactory)
135 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136}
137
Patrice Arrudac249c712019-03-19 17:00:29 -0700138// cc_test generates a test config file and an executable binary file to test
139// specific functionality on a device. The executable binary gets an implicit
140// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700141func TestFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000142 module := NewTest(android.HostAndDeviceSupported, true)
Jingwen Chen3952a902022-12-12 12:20:58 +0000143 module.bazelHandler = &ccTestBazelHandler{module: module}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 return module.Init()
145}
146
Patrice Arrudac249c712019-03-19 17:00:29 -0700147// cc_test_library creates an archive of files (i.e. .o files) which is later
148// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
149// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700150func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151 module := NewTestLibrary(android.HostAndDeviceSupported)
152 return module.Init()
153}
154
Patrice Arrudac249c712019-03-19 17:00:29 -0700155// cc_benchmark compiles an executable binary that performs benchmark testing
156// of a specific component in a device. Additional files such as test suites
157// and test configuration are installed on the side of the compiled executed
158// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700159func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700160 module := NewBenchmark(android.HostAndDeviceSupported)
161 return module.Init()
162}
163
Patrice Arrudac249c712019-03-19 17:00:29 -0700164// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700165func TestHostFactory() android.Module {
Jingwen Chen537242c2022-08-24 11:53:27 +0000166 module := NewTest(android.HostSupported, true)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 return module.Init()
168}
169
Patrice Arrudac249c712019-03-19 17:00:29 -0700170// cc_benchmark_host compiles an executable binary that performs benchmark
171// testing of a specific component in the host. Additional files such as
172// test suites and test configuration are installed on the side of the
173// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700174func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 module := NewBenchmark(android.HostSupported)
176 return module.Init()
177}
178
Colin Crossb916a382016-07-29 17:28:03 -0700179type testPerSrc interface {
180 testPerSrc() bool
181 srcs() []string
Roland Levillainf2fad972019-06-28 15:41:19 +0100182 isAllTestsVariation() bool
Colin Crossb916a382016-07-29 17:28:03 -0700183 setSrc(string, string)
Roland Levillainf2fad972019-06-28 15:41:19 +0100184 unsetSrc()
Colin Crossb916a382016-07-29 17:28:03 -0700185}
186
187func (test *testBinary) testPerSrc() bool {
188 return Bool(test.Properties.Test_per_src)
189}
190
191func (test *testBinary) srcs() []string {
192 return test.baseCompiler.Properties.Srcs
193}
194
Chris Parsons216e10a2020-07-09 17:12:52 -0400195func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700196 return test.data
197}
198
Roland Levillainf2fad972019-06-28 15:41:19 +0100199func (test *testBinary) isAllTestsVariation() bool {
200 stem := test.binaryDecorator.Properties.Stem
201 return stem != nil && *stem == ""
202}
203
Colin Crossb916a382016-07-29 17:28:03 -0700204func (test *testBinary) setSrc(name, src string) {
205 test.baseCompiler.Properties.Srcs = []string{src}
Nan Zhang0007d812017-11-07 10:57:05 -0800206 test.binaryDecorator.Properties.Stem = StringPtr(name)
Colin Crossb916a382016-07-29 17:28:03 -0700207}
208
Roland Levillainf2fad972019-06-28 15:41:19 +0100209func (test *testBinary) unsetSrc() {
210 test.baseCompiler.Properties.Srcs = nil
211 test.binaryDecorator.Properties.Stem = StringPtr("")
212}
213
Jingwen Chen537242c2022-08-24 11:53:27 +0000214func (test *testBinary) testBinary() bool {
215 return true
216}
217
Colin Crossb916a382016-07-29 17:28:03 -0700218var _ testPerSrc = (*testBinary)(nil)
219
Roland Levillain9b5fde92019-06-28 15:41:19 +0100220func TestPerSrcMutator(mctx android.BottomUpMutatorContext) {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 if m, ok := mctx.Module().(*Module); ok {
Colin Crossb916a382016-07-29 17:28:03 -0700222 if test, ok := m.linker.(testPerSrc); ok {
Roland Levillainf2fad972019-06-28 15:41:19 +0100223 numTests := len(test.srcs())
224 if test.testPerSrc() && numTests > 0 {
Chih-Hung Hsieha5f22ed2019-10-24 20:47:54 -0700225 if duplicate, found := android.CheckDuplicate(test.srcs()); found {
Jooyung Hana61ff2c2019-02-28 18:06:34 +0900226 mctx.PropertyErrorf("srcs", "found a duplicate entry %q", duplicate)
227 return
228 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100229 testNames := make([]string, numTests)
Colin Crossb916a382016-07-29 17:28:03 -0700230 for i, src := range test.srcs() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 testNames[i] = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src))
232 }
Roland Levillainf2fad972019-06-28 15:41:19 +0100233 // In addition to creating one variation per test source file,
234 // create an additional "all tests" variation named "", and have it
235 // depends on all other test_per_src variations. This is useful to
236 // create subsequent dependencies of a given module on all
237 // test_per_src variations created above: by depending on
238 // variation "", that module will transitively depend on all the
239 // other test_per_src variations without the need to know their
240 // name or even their number.
241 testNames = append(testNames, "")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 tests := mctx.CreateLocalVariations(testNames...)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800243 allTests := tests[numTests]
244 allTests.(*Module).linker.(testPerSrc).unsetSrc()
Roland Levillainf2fad972019-06-28 15:41:19 +0100245 // Prevent the "all tests" variation from being installable nor
246 // exporting to Make, as it won't create any output file.
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800247 allTests.(*Module).Properties.PreventInstall = true
248 allTests.(*Module).Properties.HideFromMake = true
Colin Crossb916a382016-07-29 17:28:03 -0700249 for i, src := range test.srcs() {
250 tests[i].(*Module).linker.(testPerSrc).setSrc(testNames[i], src)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800251 mctx.AddInterVariantDependency(testPerSrcDepTag, allTests, tests[i])
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252 }
Colin Cross90dab342020-08-21 15:55:50 -0700253 mctx.AliasVariation("")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 }
255 }
256 }
257}
258
Colin Crossb916a382016-07-29 17:28:03 -0700259type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000260 LinkerProperties TestLinkerProperties
261 InstallerProperties TestInstallerProperties
262 installer *baseInstaller
263 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700264}
265
Colin Cross600c9df2016-09-13 12:26:16 -0700266func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000267 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700268}
269
Spandan Dasf5a86552023-07-22 03:16:53 +0000270func (test *testDecorator) isolated(ctx android.EarlyModuleContext) bool {
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000271 return BoolDefault(test.LinkerProperties.Isolated, false)
272}
273
Jingwen Chen537242c2022-08-24 11:53:27 +0000274// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700275func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700276 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700277 return flags
278 }
279
Colin Cross4af21ed2019-11-04 09:37:55 -0800280 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700281 if ctx.Host() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800282 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700283
284 switch ctx.Os() {
285 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800286 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700287 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800288 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800290 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700291 }
292 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800293 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700294 }
295
296 return flags
297}
298
Colin Crossb916a382016-07-29 17:28:03 -0700299func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700300 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700301 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800302 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000303 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700304 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700305 // The isolated library requires liblog, but adding it
306 // as a static library means unit tests cannot override
307 // liblog functions. Instead make it a shared library
308 // dependency.
309 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700310 } else {
311 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
312 }
313 }
Colin Crossb916a382016-07-29 17:28:03 -0700314
Colin Cross4d9c2d12016-07-29 12:48:20 -0700315 return deps
316}
317
Colin Crossb916a382016-07-29 17:28:03 -0700318func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000319 return []interface{}{&test.LinkerProperties}
320}
321
322func (test *testDecorator) installerProps() []interface{} {
323 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700324}
325
Colin Crossb916a382016-07-29 17:28:03 -0700326func NewTestInstaller() *baseInstaller {
327 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700328}
329
Colin Crossb916a382016-07-29 17:28:03 -0700330type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000331 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700332 *binaryDecorator
333 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700334 Properties TestBinaryProperties
335 data []android.DataPath
336 testConfig android.Path
337 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700338}
339
340func (test *testBinary) linkerProps() []interface{} {
341 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
342 props = append(props, &test.Properties)
343 return props
344}
345
Colin Cross37047f12016-12-13 17:06:13 -0800346func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700347 deps = test.testDecorator.linkerDeps(ctx, deps)
348 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400349 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700350 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700351 return deps
352}
353
Colin Crossb916a382016-07-29 17:28:03 -0700354func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
355 flags = test.binaryDecorator.linkerFlags(ctx, flags)
356 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700357 return flags
358}
359
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000360func (test *testBinary) installerProps() []interface{} {
361 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
362}
363
Colin Crossb916a382016-07-29 17:28:03 -0700364func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400365 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
366
367 for _, dataSrcPath := range dataSrcPaths {
368 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
369 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400370
371 ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
372 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400373 linkableDep, ok := dep.(LinkableInterface)
Chris Parsons79d66a52020-06-05 17:26:16 -0400374 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400375 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400376 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400377 if linkableDep.OutputFile().Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400378 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400379 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
380 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Chris Parsons79d66a52020-06-05 17:26:16 -0400381 }
382 })
Colin Crossc8caa062021-09-24 16:50:14 -0700383 ctx.VisitDirectDepsWithTag(dataBinDepTag, func(dep android.Module) {
384 depName := ctx.OtherModuleName(dep)
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400385 linkableDep, ok := dep.(LinkableInterface)
Colin Crossc8caa062021-09-24 16:50:14 -0700386 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400387 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700388 }
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400389 if linkableDep.OutputFile().Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700390 test.data = append(test.data,
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400391 android.DataPath{SrcPath: linkableDep.OutputFile().Path(),
392 RelativeInstallPath: linkableDep.RelativeInstallPath()})
Colin Crossc8caa062021-09-24 16:50:14 -0700393 }
394 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400395
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000396 useVendor := ctx.inVendor() || ctx.useVndk()
397 testInstallBase := getTestInstallBase(useVendor)
398 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx))
yelinhsieh9fc60402018-10-01 19:23:14 +0800399
Cole Faust21680542022-12-07 18:18:37 -0800400 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
401 TestConfigProp: test.Properties.Test_config,
402 TestConfigTemplateProp: test.Properties.Test_config_template,
403 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
404 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000405 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800406 AutoGenConfig: test.Properties.Auto_gen_config,
407 TestInstallBase: testInstallBase,
408 DeviceTemplate: "${NativeTestConfigTemplate}",
409 HostTemplate: "${NativeHostTestConfigTemplate}",
410 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800411
Dan Shi95d19422020-08-15 12:24:26 -0700412 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
413
Colin Cross600c9df2016-09-13 12:26:16 -0700414 test.binaryDecorator.baseInstaller.dir = "nativetest"
415 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800416
417 if !Bool(test.Properties.No_named_install_directory) {
418 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800419 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800420 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
421 }
422
Julien Desprez8908b372021-02-04 10:10:16 -0800423 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800424 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
425 }
Dan Willemsen1d577e22016-08-29 15:53:15 -0700426 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700427}
428
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000429func getTestInstallBase(useVendor bool) string {
430 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
431 testInstallBase := "/data/local/tmp"
432 if useVendor {
433 testInstallBase = "/data/local/tests/vendor"
434 }
435 return testInstallBase
436}
437
438func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool) []tradefed.Config {
439 var configs []tradefed.Config
440
441 for _, module := range properties.Test_mainline_modules {
442 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
443 }
444 if Bool(properties.Require_root) {
445 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
446 } else {
447 var options []tradefed.Option
448 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
449 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
450 }
451 if Bool(properties.Disable_framework) {
452 var options []tradefed.Option
453 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
454 }
455 if isolated {
456 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
457 }
458 if properties.Test_options.Run_test_as != nil {
459 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
460 }
461 for _, tag := range properties.Test_options.Test_suite_tag {
462 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
463 }
464 if properties.Test_options.Min_shipping_api_level != nil {
465 if properties.Test_options.Vsr_min_shipping_api_level != nil {
466 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
467 }
468 var options []tradefed.Option
469 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
470 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
471 }
472 if properties.Test_options.Vsr_min_shipping_api_level != nil {
473 var options []tradefed.Option
474 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
475 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
476 }
477 if properties.Test_options.Min_vndk_version != nil {
478 var options []tradefed.Option
479 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
480 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
481 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
482 }
483 return configs
484}
485
Jingwen Chen537242c2022-08-24 11:53:27 +0000486func NewTest(hod android.HostOrDeviceSupported, bazelable bool) *Module {
487 module, binary := newBinary(hod, bazelable)
Jingwen Chen3952a902022-12-12 12:20:58 +0000488 module.bazelable = bazelable
Colin Crossb916a382016-07-29 17:28:03 -0700489 module.multilib = android.MultilibBoth
Dan Willemsen1d577e22016-08-29 15:53:15 -0700490 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700491
492 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000493 testDecorator: &testDecorator{
494 linker: binary.baseLinker,
495 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700496 },
Colin Crossb916a382016-07-29 17:28:03 -0700497 binaryDecorator: binary,
498 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700499 }
Colin Crossb916a382016-07-29 17:28:03 -0700500 module.compiler = test
501 module.linker = test
502 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700503 return module
504}
505
Colin Crossb916a382016-07-29 17:28:03 -0700506type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000507 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700508 *libraryDecorator
509}
510
Jingwen Chen537242c2022-08-24 11:53:27 +0000511func (test *testLibrary) testLibrary() bool {
512 return true
513}
514
Colin Crossb916a382016-07-29 17:28:03 -0700515func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000516 var props []interface{}
517 props = append(props, test.testDecorator.linkerProps()...)
518 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700519}
520
Colin Cross37047f12016-12-13 17:06:13 -0800521func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700522 deps = test.testDecorator.linkerDeps(ctx, deps)
523 deps = test.libraryDecorator.linkerDeps(ctx, deps)
524 return deps
525}
526
527func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
528 flags = test.libraryDecorator.linkerFlags(ctx, flags)
529 flags = test.testDecorator.linkerFlags(ctx, flags)
530 return flags
531}
532
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000533func (test *testLibrary) installerProps() []interface{} {
534 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
535}
536
Colin Cross4d9c2d12016-07-29 12:48:20 -0700537func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800538 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700539 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700540 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000541 testDecorator: &testDecorator{
542 linker: library.baseLinker,
543 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700544 },
Colin Crossb916a382016-07-29 17:28:03 -0700545 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700546 }
Colin Crossb916a382016-07-29 17:28:03 -0700547 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000548 module.installer = test
Jingwen Chen537242c2022-08-24 11:53:27 +0000549 module.bazelable = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700550 return module
551}
552
Colin Crosse28f4e22017-04-24 18:10:29 -0700553type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700554 // list of files or filegroup modules that provide data that should be installed alongside
555 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800556 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700557
Colin Crosse28f4e22017-04-24 18:10:29 -0700558 // list of compatibility suites (for example "cts", "vts") that the module should be
559 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700560 Test_suites []string `android:"arch_variant"`
561
562 // the name of the test configuration (for example "AndroidTest.xml") that should be
563 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800564 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700565
566 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
567 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800568 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700569
570 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
571 // with root permission.
572 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700573
574 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
575 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
576 // explicitly.
577 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700578}
579
Colin Crossb916a382016-07-29 17:28:03 -0700580type benchmarkDecorator struct {
581 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700582 Properties BenchmarkProperties
Anders Lewisb97e8182017-07-14 15:20:13 -0700583 data android.Paths
Colin Cross303e21f2018-08-07 16:49:25 -0700584 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700585}
586
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400587func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
588 return true
589}
590
Colin Crosse28f4e22017-04-24 18:10:29 -0700591func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
592 props := benchmark.binaryDecorator.linkerProps()
593 props = append(props, &benchmark.Properties)
594 return props
595}
596
Colin Cross37047f12016-12-13 17:06:13 -0800597func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700598 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700599 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
600 return deps
601}
602
Colin Crossb916a382016-07-29 17:28:03 -0700603func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross8a497952019-03-05 22:25:09 -0800604 benchmark.data = android.PathsForModuleSrc(ctx, benchmark.Properties.Data)
Chris Parsons79d66a52020-06-05 17:26:16 -0400605
Dan Shi37ee3b82019-06-06 16:23:32 -0700606 var configs []tradefed.Config
607 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800608 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700609 }
Cole Faust21680542022-12-07 18:18:37 -0800610 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
611 TestConfigProp: benchmark.Properties.Test_config,
612 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
613 TestSuites: benchmark.Properties.Test_suites,
614 Config: configs,
615 AutoGenConfig: benchmark.Properties.Auto_gen_config,
616 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
617 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
618 })
Colin Cross303e21f2018-08-07 16:49:25 -0700619
Colin Cross28690e92017-09-08 16:20:30 -0700620 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
621 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Dan Willemsen1d577e22016-08-29 15:53:15 -0700622 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700623}
624
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400626 module, binary := newBinary(hod, false)
Colin Crossb916a382016-07-29 17:28:03 -0700627 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700628 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700629
630 benchmark := &benchmarkDecorator{
631 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700632 }
Colin Crossb916a382016-07-29 17:28:03 -0700633 module.linker = benchmark
634 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700635 return module
636}
Jingwen Chen537242c2022-08-24 11:53:27 +0000637
Jingwen Chen3952a902022-12-12 12:20:58 +0000638type ccTestBazelHandler struct {
639 module *Module
640}
641
642var _ BazelHandler = (*ccTestBazelHandler)(nil)
643
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000644// The top level target named $label is a test_suite target,
645// not the internal cc_test executable target.
646//
647// This is to ensure `b test //$label` runs the test_suite target directly,
648// which depends on tradefed_test targets, instead of the internal cc_test
649// target, which doesn't have tradefed integrations.
650//
651// However, for cquery, we want the internal cc_test executable target, which
652// has the suffix "__tf_internal".
653func mixedBuildsTestLabel(label string) string {
654 return label + "__tf_internal"
655}
656
Jingwen Chen3952a902022-12-12 12:20:58 +0000657func (handler *ccTestBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
658 bazelCtx := ctx.Config().BazelContext
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000659 bazelCtx.QueueBazelRequest(mixedBuildsTestLabel(label), cquery.GetCcUnstrippedInfo, android.GetConfigKey(ctx))
Jingwen Chen3952a902022-12-12 12:20:58 +0000660}
661
662func (handler *ccTestBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
663 bazelCtx := ctx.Config().BazelContext
Jingwen Chen6ee23ad2023-07-24 14:56:28 +0000664 info, err := bazelCtx.GetCcUnstrippedInfo(mixedBuildsTestLabel(label), android.GetConfigKey(ctx))
Jingwen Chen3952a902022-12-12 12:20:58 +0000665 if err != nil {
666 ctx.ModuleErrorf(err.Error())
667 return
668 }
669
Sam Delmerico4ed95e22023-02-03 18:12:15 -0500670 var outputFilePath android.Path = android.PathForBazelOut(ctx, info.OutputFile)
671 if len(info.TidyFiles) > 0 {
672 handler.module.tidyFiles = android.PathsForBazelOut(ctx, info.TidyFiles)
673 outputFilePath = android.AttachValidationActions(ctx, outputFilePath, handler.module.tidyFiles)
674 }
Jingwen Chen3952a902022-12-12 12:20:58 +0000675 handler.module.outputFile = android.OptionalPathForPath(outputFilePath)
676 handler.module.linker.(*testBinary).unstrippedOutputFile = android.PathForBazelOut(ctx, info.UnstrippedOutput)
Sam Delmerico5fb794a2023-01-27 16:01:37 -0500677
678 handler.module.setAndroidMkVariablesFromCquery(info.CcAndroidMkInfo)
Jingwen Chen3952a902022-12-12 12:20:58 +0000679}
680
Jingwen Chen537242c2022-08-24 11:53:27 +0000681// binaryAttributes contains Bazel attributes corresponding to a cc test
682type testBinaryAttributes struct {
683 binaryAttributes
684
Spandan Dasd1cd3512023-07-22 02:19:42 +0000685 Gtest bool
Sam Delmericofb3bb322022-10-21 10:42:24 -0400686
687 tidyAttributes
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000688 tradefed.TestConfigAttributes
Jingwen Chen537242c2022-08-24 11:53:27 +0000689}
690
691// testBinaryBp2build is the bp2build converter for cc_test modules. A cc_test's
692// dependency graph and compilation/linking steps are functionally similar to a
693// cc_binary, but has additional dependencies on test deps like gtest, and
694// produces additional runfiles like XML plans for Tradefed orchestration
695//
696// TODO(b/244432609): handle `isolated` property.
697// TODO(b/244432134): handle custom runpaths for tests that assume runfile layouts not
698// default to bazel. (see linkerInit function)
Jingwen Chen537242c2022-08-24 11:53:27 +0000699func testBinaryBp2build(ctx android.TopDownMutatorContext, m *Module) {
700 var testBinaryAttrs testBinaryAttributes
701 testBinaryAttrs.binaryAttributes = binaryBp2buildAttrs(ctx, m)
702
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000703 var data bazel.LabelListAttribute
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000704 var tags bazel.StringListAttribute
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000705
Jingwen Chen537242c2022-08-24 11:53:27 +0000706 testBinaryProps := m.GetArchVariantProperties(ctx, &TestBinaryProperties{})
707 for axis, configToProps := range testBinaryProps {
708 for config, props := range configToProps {
709 if p, ok := props.(*TestBinaryProperties); ok {
710 // Combine data, data_bins and data_libs into a single 'data' attribute.
711 var combinedData bazel.LabelList
712 combinedData.Append(android.BazelLabelForModuleSrc(ctx, p.Data))
713 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_bins))
714 combinedData.Append(android.BazelLabelForModuleDeps(ctx, p.Data_libs))
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000715 data.SetSelectValue(axis, config, combinedData)
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000716 tags.SetSelectValue(axis, config, p.Test_options.Tags)
Jingwen Chen537242c2022-08-24 11:53:27 +0000717 }
718 }
719 }
720
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400721 m.convertTidyAttributes(ctx, &testBinaryAttrs.tidyAttributes)
Sam Delmericofb3bb322022-10-21 10:42:24 -0400722
Spandan Dasf5a86552023-07-22 03:16:53 +0000723 gtestIsolated := m.linker.(*testBinary).isolated(ctx)
Jingwen Chen537242c2022-08-24 11:53:27 +0000724 for _, propIntf := range m.GetProperties() {
725 if testLinkerProps, ok := propIntf.(*TestLinkerProperties); ok {
726 testBinaryAttrs.Gtest = proptools.BoolDefault(testLinkerProps.Gtest, true)
Jingwen Chen537242c2022-08-24 11:53:27 +0000727 break
728 }
729 }
730
Spandan Dasd1cd3512023-07-22 02:19:42 +0000731 addImplicitGtestDeps(ctx, &testBinaryAttrs, gtestIsolated)
Spandan Das651203d2023-07-21 22:55:32 +0000732
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000733 for _, testProps := range m.GetProperties() {
734 if p, ok := testProps.(*TestBinaryProperties); ok {
735 useVendor := false // TODO Bug: 262914724
736 testInstallBase := getTestInstallBase(useVendor)
737 testConfigAttributes := tradefed.GetTestConfigAttributes(
738 ctx,
739 p.Test_config,
740 p.Test_options.Extra_test_configs,
741 p.Auto_gen_config,
742 p.Test_options.Test_suite_tag,
743 p.Test_config_template,
Spandan Dasf5a86552023-07-22 03:16:53 +0000744 getTradefedConfigOptions(ctx, p, gtestIsolated),
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000745 &testInstallBase,
746 )
747 testBinaryAttrs.TestConfigAttributes = testConfigAttributes
748 }
749 }
750
751 // TODO (b/262914724): convert to tradefed_cc_test and tradefed_cc_test_host
Jingwen Chen537242c2022-08-24 11:53:27 +0000752 ctx.CreateBazelTargetModule(
753 bazel.BazelTargetModuleProperties{
754 Rule_class: "cc_test",
755 Bzl_load_location: "//build/bazel/rules/cc:cc_test.bzl",
756 },
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000757 android.CommonAttributes{
758 Name: m.Name(),
759 Data: data,
Jingwen Chenfbff97a2022-09-16 02:32:03 +0000760 Tags: tags,
Jingwen Chenebdc20f2022-09-01 18:54:50 +0000761 },
Jingwen Chen537242c2022-08-24 11:53:27 +0000762 &testBinaryAttrs)
763}
Spandan Das651203d2023-07-21 22:55:32 +0000764
765// cc_test that builds using gtest needs some additional deps
766// addImplicitGtestDeps makes these deps explicit in the generated BUILD files
Spandan Dasd1cd3512023-07-22 02:19:42 +0000767func addImplicitGtestDeps(ctx android.BazelConversionPathContext, attrs *testBinaryAttributes, gtestIsolated bool) {
768 addDepsAndDedupe := func(lla *bazel.LabelListAttribute, modules []string) {
769 moduleLabels := android.BazelLabelForModuleDeps(ctx, modules)
770 lla.Value.Append(moduleLabels)
771 // Dedupe
772 lla.Value = bazel.FirstUniqueBazelLabelList(lla.Value)
773 }
774 // this must be kept in sync with Soong's implementation in:
775 // https://cs.android.com/android/_/android/platform/build/soong/+/460fb2d6d546b5ab493a7e5479998c4933a80f73:cc/test.go;l=300-313;drc=ec7314336a2b35ea30ce5438b83949c28e3ac429;bpv=1;bpt=0
Spandan Das651203d2023-07-21 22:55:32 +0000776 if attrs.Gtest {
Spandan Dasd1cd3512023-07-22 02:19:42 +0000777 // TODO - b/244433197: Handle canUseSdk
778 if gtestIsolated {
779 addDepsAndDedupe(&attrs.Deps, []string{"libgtest_isolated_main"})
780 addDepsAndDedupe(&attrs.Dynamic_deps, []string{"liblog"})
781 } else {
782 addDepsAndDedupe(&attrs.Deps, []string{
Spandan Das651203d2023-07-21 22:55:32 +0000783 "libgtest_main",
784 "libgtest",
Spandan Dasd1cd3512023-07-22 02:19:42 +0000785 })
786 }
Spandan Das651203d2023-07-21 22:55:32 +0000787 }
Spandan Das651203d2023-07-21 22:55:32 +0000788}