blob: ec20cb0b609adbed90e2e056d85319e32c2649a4 [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"
Julien Desprez3b933d32021-01-14 11:49:15 -080020
Cole Faust65cb40a2024-10-21 15:41:42 -070021 "github.com/google/blueprint/proptools"
22
Colin Cross4d9c2d12016-07-29 12:48:20 -070023 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070024 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070025)
26
Trevor Radcliffef389cb42022-03-24 21:06:14 +000027// TestLinkerProperties properties to be registered via the linker
28type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070029 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070030 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070031
Elliott Hughes90e76f12025-01-16 13:48:31 -080032 // if set, use the isolated gtest runner. Defaults to false.
33 // Isolation is not supported on Windows.
Christopher Ferris9df92d62018-08-21 12:40:08 -070034 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070035}
Colin Cross4d9c2d12016-07-29 12:48:20 -070036
Trevor Radcliffef389cb42022-03-24 21:06:14 +000037// TestInstallerProperties properties to be registered via the installer
38type TestInstallerProperties struct {
39 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
40 Test_suites []string `android:"arch_variant"`
41}
42
yelinhsieh9fc60402018-10-01 19:23:14 +080043// Test option struct.
44type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080045 android.CommonTestOptions
46
yelinhsieh9fc60402018-10-01 19:23:14 +080047 // The UID that you want to run the test as on a device.
48 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070049
David Srbecky519db272020-06-18 18:07:00 +010050 // A list of free-formed strings without spaces that categorize the test.
51 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070052
53 // a list of extra test configuration files that should be installed with the module.
54 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080055
Justin Yun46f66052021-05-04 18:42:24 +090056 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090057 // for the shipping api level is less than the min_shipping_api_level, skip this module.
58 Min_shipping_api_level *int64
59
60 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
61 // shipping api level and vendor api level properties are less than the
62 // vsr_min_shipping_api_level, skip this module.
63 // As this includes the shipping api level check, it is not allowed to define
64 // min_shipping_api_level at the same time with this property.
65 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090066
67 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090068 // integer value and the value is less than the min_vndk_version, skip this module.
69 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000070
71 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
72 // The "key" is optional in each of these.
73 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080074}
75
Colin Crossb916a382016-07-29 17:28:03 -070076type TestBinaryProperties struct {
Dan Willemsen3340d602016-12-27 14:40:40 -080077 // Disables the creation of a test-specific directory when used with
78 // relative_install_path. Useful if several tests need to be in the same
Colin Cross3a02c7b2024-05-21 13:46:22 -070079 // directory.
Dan Willemsen3340d602016-12-27 14:40:40 -080080 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080081
82 // list of files or filegroup modules that provide data that should be installed alongside
83 // the test
Dan Shi67a88342020-02-25 16:34:39 -080084 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070085
Cole Faustc3489f42024-12-06 13:07:18 -080086 // Same as data, but adds dependencies on modules using the device's os variant, and common
Cole Faust65cb40a2024-10-21 15:41:42 -070087 // architecture's variant. Can be useful to add device-built apps to the data of a host
88 // test.
89 Device_common_data []string `android:"path_device_common"`
90
Cole Faustc3489f42024-12-06 13:07:18 -080091 // Same as data, but adds dependencies on modules using the device's os variant, and the
92 // device's first architecture's variant. Can be useful to add device-built apps to the data
93 // of a host test.
Cole Faust65cb40a2024-10-21 15:41:42 -070094 Device_first_data []string `android:"path_device_first"`
95
Chris Parsons79d66a52020-06-05 17:26:16 -040096 // list of shared library modules that should be installed alongside the test
97 Data_libs []string `android:"arch_variant"`
98
Colin Crossc8caa062021-09-24 16:50:14 -070099 // list of binary modules that should be installed alongside the test
100 Data_bins []string `android:"arch_variant"`
101
Julien Despreze146e392018-08-02 15:00:46 -0700102 // the name of the test configuration (for example "AndroidTest.xml") that should be
103 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800104 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700105
106 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
107 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800108 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800109
110 // Test options.
111 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700112
113 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
114 // with root permission.
115 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700116
117 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
118 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800119
Dan Shi6ffaaa82019-09-26 11:41:36 -0700120 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
121 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
122 // explicitly.
123 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800124
125 // Add parameterized mainline modules to auto generated test config. The options will be
126 // handled by TradeFed to download and install the specified modules on the device.
127 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700128
129 // Install the test into a folder named for the module in all test suites.
130 Per_testcase_directory *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700131}
132
133func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700134 android.RegisterModuleType("cc_test", TestFactory)
135 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
136 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
137 android.RegisterModuleType("cc_test_host", TestHostFactory)
138 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
Patrice Arrudac249c712019-03-19 17:00:29 -0700141// cc_test generates a test config file and an executable binary file to test
142// specific functionality on a device. The executable binary gets an implicit
143// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700144func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800145 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700146 return module.Init()
147}
148
Patrice Arrudac249c712019-03-19 17:00:29 -0700149// cc_test_library creates an archive of files (i.e. .o files) which is later
150// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
151// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700152func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700153 module := NewTestLibrary(android.HostAndDeviceSupported)
154 return module.Init()
155}
156
Patrice Arrudac249c712019-03-19 17:00:29 -0700157// cc_benchmark compiles an executable binary that performs benchmark testing
158// of a specific component in a device. Additional files such as test suites
159// and test configuration are installed on the side of the compiled executed
160// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700161func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000163 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164 return module.Init()
165}
166
Patrice Arrudac249c712019-03-19 17:00:29 -0700167// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700168func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800169 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 return module.Init()
171}
172
Patrice Arrudac249c712019-03-19 17:00:29 -0700173// cc_benchmark_host compiles an executable binary that performs benchmark
174// testing of a specific component in the host. Additional files such as
175// test suites and test configuration are installed on the side of the
176// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700177func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 module := NewBenchmark(android.HostSupported)
179 return module.Init()
180}
181
Chris Parsons216e10a2020-07-09 17:12:52 -0400182func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700183 return test.data
184}
185
Jingwen Chen537242c2022-08-24 11:53:27 +0000186func (test *testBinary) testBinary() bool {
187 return true
188}
189
Colin Crossb916a382016-07-29 17:28:03 -0700190type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000191 LinkerProperties TestLinkerProperties
192 InstallerProperties TestInstallerProperties
193 installer *baseInstaller
194 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700195}
196
Colin Cross600c9df2016-09-13 12:26:16 -0700197func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000198 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700199}
200
Elliott Hughes90e76f12025-01-16 13:48:31 -0800201func (test *testDecorator) isolated(ctx android.BaseModuleContext) bool {
202 return BoolDefault(test.LinkerProperties.Isolated, false) && !ctx.Windows()
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000203}
204
Jingwen Chen537242c2022-08-24 11:53:27 +0000205// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700206func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700207 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208 return flags
209 }
210
Colin Cross4af21ed2019-11-04 09:37:55 -0800211 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700212 if ctx.Host() {
Liana Kazanovab88279f2024-07-15 18:45:23 +0000213 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700214
215 switch ctx.Os() {
216 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800217 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800219 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800221 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 }
223 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800224 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 }
226
227 return flags
228}
229
Colin Crossb916a382016-07-29 17:28:03 -0700230func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700231 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700232 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800233 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000234 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700235 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700236 // The isolated library requires liblog, but adding it
237 // as a static library means unit tests cannot override
238 // liblog functions. Instead make it a shared library
239 // dependency.
240 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700241 } else {
242 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
243 }
244 }
Colin Crossb916a382016-07-29 17:28:03 -0700245
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 return deps
247}
248
Colin Crossb916a382016-07-29 17:28:03 -0700249func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000250 return []interface{}{&test.LinkerProperties}
251}
252
253func (test *testDecorator) installerProps() []interface{} {
254 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255}
256
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800257func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
258 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
259 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
260 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
261 }
262}
263
Colin Crossb916a382016-07-29 17:28:03 -0700264func NewTestInstaller() *baseInstaller {
265 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700266}
267
Colin Crossb916a382016-07-29 17:28:03 -0700268type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000269 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700270 *binaryDecorator
271 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700272 Properties TestBinaryProperties
273 data []android.DataPath
274 testConfig android.Path
275 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700276}
277
278func (test *testBinary) linkerProps() []interface{} {
279 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
280 props = append(props, &test.Properties)
281 return props
282}
283
Colin Cross37047f12016-12-13 17:06:13 -0800284func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700285 deps = test.testDecorator.linkerDeps(ctx, deps)
286 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400287 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700288 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700289 return deps
290}
291
Colin Crossb916a382016-07-29 17:28:03 -0700292func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
293 flags = test.binaryDecorator.linkerFlags(ctx, flags)
294 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700295
296 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
297 // Host modules already get an rpath specified in linker.go.
298 if !ctx.Host() {
299 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
300 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700301 return flags
302}
303
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800304func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
305 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
306 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
307 }
308 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
309 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
310 if test.testConfig != nil {
311 if _, ok := test.testConfig.(android.WritablePath); ok {
312 moduleInfoJSON.AutoTestConfig = []string{"true"}
313 }
314 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
315 }
316 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
317
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800318 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
319
320 if len(test.InstallerProperties.Test_suites) > 0 {
321 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
322 } else {
323 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
324 }
325
326 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
327 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
328 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
329
330}
331
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000332func (test *testBinary) installerProps() []interface{} {
333 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
334}
335
Colin Crossb916a382016-07-29 17:28:03 -0700336func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400337 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700338 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_common_data)...)
339 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_first_data)...)
Chris Parsons216e10a2020-07-09 17:12:52 -0400340
341 for _, dataSrcPath := range dataSrcPaths {
342 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
343 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400344
Yu Liu97880e12025-01-07 19:03:34 +0000345 ctx.VisitDirectDepsProxyWithTag(dataLibDepTag, func(dep android.ModuleProxy) {
Chris Parsons79d66a52020-06-05 17:26:16 -0400346 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000347 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Chris Parsons79d66a52020-06-05 17:26:16 -0400348 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400349 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400350 }
Yu Liu97880e12025-01-07 19:03:34 +0000351 if linkableDep.OutputFile.Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400352 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000353 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
354 RelativeInstallPath: linkableDep.RelativeInstallPath})
Chris Parsons79d66a52020-06-05 17:26:16 -0400355 }
356 })
Yu Liu97880e12025-01-07 19:03:34 +0000357 ctx.VisitDirectDepsProxyWithTag(dataBinDepTag, func(dep android.ModuleProxy) {
Colin Crossc8caa062021-09-24 16:50:14 -0700358 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000359 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Colin Crossc8caa062021-09-24 16:50:14 -0700360 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400361 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700362 }
Yu Liu97880e12025-01-07 19:03:34 +0000363 if linkableDep.OutputFile.Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700364 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000365 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
366 RelativeInstallPath: linkableDep.RelativeInstallPath})
Colin Crossc8caa062021-09-24 16:50:14 -0700367 }
368 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400369
Kiyoung Kimaa394802024-01-08 12:55:45 +0900370 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700371 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800372
Cole Faust21680542022-12-07 18:18:37 -0800373 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
374 TestConfigProp: test.Properties.Test_config,
375 TestConfigTemplateProp: test.Properties.Test_config_template,
376 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
377 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000378 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800379 AutoGenConfig: test.Properties.Auto_gen_config,
380 TestInstallBase: testInstallBase,
381 DeviceTemplate: "${NativeTestConfigTemplate}",
382 HostTemplate: "${NativeHostTestConfigTemplate}",
383 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800384
Dan Shi95d19422020-08-15 12:24:26 -0700385 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
386
Colin Cross600c9df2016-09-13 12:26:16 -0700387 test.binaryDecorator.baseInstaller.dir = "nativetest"
388 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800389
390 if !Bool(test.Properties.No_named_install_directory) {
391 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800392 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800393 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
394 }
395
Julien Desprez8908b372021-02-04 10:10:16 -0800396 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800397 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
398 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800399
Spandan Das323631f2025-01-24 21:57:47 +0000400 if ctx.PrimaryArch() && !ctx.Config().KatiEnabled() { // TODO(spandandas): Remove the special case for kati
401 // Install the test config in testcases/ directory for atest.
402 // Use PrimaryArch and SubName to prevent duplicate installation rules
403 c, ok := ctx.Module().(*Module)
404 if !ok {
405 ctx.ModuleErrorf("Not a cc_test module")
406 }
407 testCases := android.PathForModuleInPartitionInstall(ctx, "testcases", ctx.ModuleName()+c.SubName())
408 if test.testConfig != nil {
Spandan Dasf54c80d2025-01-28 23:26:25 +0000409 ctx.InstallFile(testCases, ctx.ModuleName()+".config", test.testConfig)
Spandan Das323631f2025-01-24 21:57:47 +0000410 }
411 for _, extraTestConfig := range test.extraTestConfigs {
412 ctx.InstallFile(testCases, extraTestConfig.Base(), extraTestConfig)
413 }
414 ctx.InstallTestData(testCases, test.data)
415 ctx.InstallFile(testCases, file.Base(), file)
416 }
417
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800418 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700419 test.binaryDecorator.baseInstaller.install(ctx, file)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700420}
421
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000422func getTestInstallBase(useVendor bool) string {
423 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
424 testInstallBase := "/data/local/tmp"
425 if useVendor {
426 testInstallBase = "/data/local/tests/vendor"
427 }
428 return testInstallBase
429}
430
Dan Shiad042502023-08-02 13:53:00 -0700431func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000432 var configs []tradefed.Config
433
434 for _, module := range properties.Test_mainline_modules {
435 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
436 }
Dan Shiad042502023-08-02 13:53:00 -0700437 if device {
438 if Bool(properties.Require_root) {
439 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
440 } else {
441 var options []tradefed.Option
442 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
443 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
444 }
445 if Bool(properties.Disable_framework) {
446 var options []tradefed.Option
447 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
448 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000449 }
450 if isolated {
451 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
452 }
453 if properties.Test_options.Run_test_as != nil {
454 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
455 }
456 for _, tag := range properties.Test_options.Test_suite_tag {
457 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
458 }
459 if properties.Test_options.Min_shipping_api_level != nil {
460 if properties.Test_options.Vsr_min_shipping_api_level != nil {
461 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
462 }
463 var options []tradefed.Option
464 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
465 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
466 }
467 if properties.Test_options.Vsr_min_shipping_api_level != nil {
468 var options []tradefed.Option
469 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_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.Min_vndk_version != nil {
473 var options []tradefed.Option
474 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
475 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
476 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
477 }
478 return configs
479}
480
Colin Cross8ff10582023-12-07 13:10:56 -0800481func NewTest(hod android.HostOrDeviceSupported) *Module {
482 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700483 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000484 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700485 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700486
487 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000488 testDecorator: &testDecorator{
489 linker: binary.baseLinker,
490 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700491 },
Colin Crossb916a382016-07-29 17:28:03 -0700492 binaryDecorator: binary,
493 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700494 }
Colin Crossb916a382016-07-29 17:28:03 -0700495 module.compiler = test
496 module.linker = test
497 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700498 return module
499}
500
Colin Crossb916a382016-07-29 17:28:03 -0700501type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000502 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700503 *libraryDecorator
504}
505
Jingwen Chen537242c2022-08-24 11:53:27 +0000506func (test *testLibrary) testLibrary() bool {
507 return true
508}
509
Colin Crossb916a382016-07-29 17:28:03 -0700510func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000511 var props []interface{}
512 props = append(props, test.testDecorator.linkerProps()...)
513 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700514}
515
Colin Cross37047f12016-12-13 17:06:13 -0800516func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700517 deps = test.testDecorator.linkerDeps(ctx, deps)
518 deps = test.libraryDecorator.linkerDeps(ctx, deps)
519 return deps
520}
521
522func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
523 flags = test.libraryDecorator.linkerFlags(ctx, flags)
524 flags = test.testDecorator.linkerFlags(ctx, flags)
525 return flags
526}
527
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800528func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
529 if len(test.InstallerProperties.Test_suites) > 0 {
530 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
531 }
532
533 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
534 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
535}
536
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000537func (test *testLibrary) installerProps() []interface{} {
538 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
539}
540
Colin Cross4d9c2d12016-07-29 12:48:20 -0700541func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800542 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700543 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700544 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000545 testDecorator: &testDecorator{
546 linker: library.baseLinker,
547 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700548 },
Colin Crossb916a382016-07-29 17:28:03 -0700549 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700550 }
Colin Crossb916a382016-07-29 17:28:03 -0700551 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000552 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700553 return module
554}
555
Colin Crosse28f4e22017-04-24 18:10:29 -0700556type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700557 // list of files or filegroup modules that provide data that should be installed alongside
558 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800559 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700560
Colin Crosse28f4e22017-04-24 18:10:29 -0700561 // list of compatibility suites (for example "cts", "vts") that the module should be
562 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700563 Test_suites []string `android:"arch_variant"`
564
565 // the name of the test configuration (for example "AndroidTest.xml") that should be
566 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800567 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700568
569 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
570 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800571 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700572
573 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
574 // with root permission.
575 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700576
577 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
578 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
579 // explicitly.
580 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700581}
582
Colin Crossb916a382016-07-29 17:28:03 -0700583type benchmarkDecorator struct {
584 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700585 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800586 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700587 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588}
589
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400590func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
591 return true
592}
593
Colin Crosse28f4e22017-04-24 18:10:29 -0700594func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
595 props := benchmark.binaryDecorator.linkerProps()
596 props = append(props, &benchmark.Properties)
597 return props
598}
599
Colin Cross37047f12016-12-13 17:06:13 -0800600func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700601 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700602 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
603 return deps
604}
605
Colin Crossb916a382016-07-29 17:28:03 -0700606func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800607 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
608 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
609 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400610
Dan Shi37ee3b82019-06-06 16:23:32 -0700611 var configs []tradefed.Config
612 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800613 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700614 }
Cole Faust21680542022-12-07 18:18:37 -0800615 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
616 TestConfigProp: benchmark.Properties.Test_config,
617 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
618 TestSuites: benchmark.Properties.Test_suites,
619 Config: configs,
620 AutoGenConfig: benchmark.Properties.Auto_gen_config,
621 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
622 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
623 })
Colin Cross303e21f2018-08-07 16:49:25 -0700624
Colin Cross28690e92017-09-08 16:20:30 -0700625 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
626 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800627 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700628 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700629}
630
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800631func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
632 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
633
634 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
635 if len(benchmark.Properties.Test_suites) > 0 {
636 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
637 } else {
638 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
639 }
640
641 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
642 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
643 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
644 }
645
646 if benchmark.testConfig != nil {
647 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
648 moduleInfoJSON.AutoTestConfig = []string{"true"}
649 }
650 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
651 }
652}
653
Colin Cross4d9c2d12016-07-29 12:48:20 -0700654func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800655 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700656 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700657 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700658
659 benchmark := &benchmarkDecorator{
660 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700661 }
Colin Crossb916a382016-07-29 17:28:03 -0700662 module.linker = benchmark
663 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700664 return module
665}