blob: ccd83fec34ee5796e9bd88797a7c85829892566e [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
Yifeng Zeng1876f5b2025-02-05 17:00:56 -080021 "github.com/google/blueprint/depset"
Cole Faust65cb40a2024-10-21 15:41:42 -070022 "github.com/google/blueprint/proptools"
23
Colin Cross4d9c2d12016-07-29 12:48:20 -070024 "android/soong/android"
Colin Cross303e21f2018-08-07 16:49:25 -070025 "android/soong/tradefed"
Colin Cross4d9c2d12016-07-29 12:48:20 -070026)
27
Trevor Radcliffef389cb42022-03-24 21:06:14 +000028// TestLinkerProperties properties to be registered via the linker
29type TestLinkerProperties struct {
Colin Cross4d9c2d12016-07-29 12:48:20 -070030 // if set, build against the gtest library. Defaults to true.
Colin Cross600c9df2016-09-13 12:26:16 -070031 Gtest *bool
Christopher Ferris9df92d62018-08-21 12:40:08 -070032
Elliott Hughes90e76f12025-01-16 13:48:31 -080033 // if set, use the isolated gtest runner. Defaults to false.
34 // Isolation is not supported on Windows.
Christopher Ferris9df92d62018-08-21 12:40:08 -070035 Isolated *bool
Colin Crossb916a382016-07-29 17:28:03 -070036}
Colin Cross4d9c2d12016-07-29 12:48:20 -070037
Trevor Radcliffef389cb42022-03-24 21:06:14 +000038// TestInstallerProperties properties to be registered via the installer
39type TestInstallerProperties struct {
40 // list of compatibility suites (for example "cts", "vts") that the module should be installed into.
41 Test_suites []string `android:"arch_variant"`
42}
43
yelinhsieh9fc60402018-10-01 19:23:14 +080044// Test option struct.
45type TestOptions struct {
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +080046 android.CommonTestOptions
47
yelinhsieh9fc60402018-10-01 19:23:14 +080048 // The UID that you want to run the test as on a device.
49 Run_test_as *string
Dan Shi95d19422020-08-15 12:24:26 -070050
David Srbecky519db272020-06-18 18:07:00 +010051 // A list of free-formed strings without spaces that categorize the test.
52 Test_suite_tag []string
Dan Shi95d19422020-08-15 12:24:26 -070053
54 // a list of extra test configuration files that should be installed with the module.
55 Extra_test_configs []string `android:"path,arch_variant"`
Dan Shid79572f2020-11-13 14:33:46 -080056
Justin Yun46f66052021-05-04 18:42:24 +090057 // Add ShippingApiLevelModuleController to auto generated test config. If the device properties
Justin Yuna364cfb2021-05-13 12:39:42 +090058 // for the shipping api level is less than the min_shipping_api_level, skip this module.
59 Min_shipping_api_level *int64
60
61 // Add ShippingApiLevelModuleController to auto generated test config. If any of the device
62 // shipping api level and vendor api level properties are less than the
63 // vsr_min_shipping_api_level, skip this module.
64 // As this includes the shipping api level check, it is not allowed to define
65 // min_shipping_api_level at the same time with this property.
66 Vsr_min_shipping_api_level *int64
Justin Yun46f66052021-05-04 18:42:24 +090067
68 // Add MinApiLevelModuleController with ro.vndk.version property. If ro.vndk.version has an
Justin Yuna364cfb2021-05-13 12:39:42 +090069 // integer value and the value is less than the min_vndk_version, skip this module.
70 Min_vndk_version *int64
Dan Shiec731432023-05-26 04:21:44 +000071
72 // Extra <option> tags to add to the auto generated test xml file under the test runner, e.g., GTest.
73 // The "key" is optional in each of these.
74 Test_runner_options []tradefed.Option
yelinhsieh9fc60402018-10-01 19:23:14 +080075}
76
Colin Crossb916a382016-07-29 17:28:03 -070077type TestBinaryProperties struct {
Dan Willemsen3340d602016-12-27 14:40:40 -080078 // Disables the creation of a test-specific directory when used with
79 // relative_install_path. Useful if several tests need to be in the same
Colin Cross3a02c7b2024-05-21 13:46:22 -070080 // directory.
Dan Willemsen3340d602016-12-27 14:40:40 -080081 No_named_install_directory *bool
Colin Crossfaeb7aa2017-02-01 14:12:44 -080082
83 // list of files or filegroup modules that provide data that should be installed alongside
84 // the test
Dan Shi67a88342020-02-25 16:34:39 -080085 Data []string `android:"path,arch_variant"`
Colin Crossa929db02017-03-27 16:27:50 -070086
Cole Faustc3489f42024-12-06 13:07:18 -080087 // Same as data, but adds dependencies on modules using the device's os variant, and common
Cole Faust65cb40a2024-10-21 15:41:42 -070088 // architecture's variant. Can be useful to add device-built apps to the data of a host
89 // test.
90 Device_common_data []string `android:"path_device_common"`
91
Cole Faustc3489f42024-12-06 13:07:18 -080092 // Same as data, but adds dependencies on modules using the device's os variant, and the
93 // device's first architecture's variant. Can be useful to add device-built apps to the data
94 // of a host test.
Cole Faust65cb40a2024-10-21 15:41:42 -070095 Device_first_data []string `android:"path_device_first"`
96
Colin Crossb3614422025-02-18 15:18:18 -080097 // Same as data, but will add dependencies on modules using the host's os variation and
98 // the common arch variation. Useful for a device test that wants to depend on a host
99 // module, for example to include a custom Tradefed test runner.
100 Host_common_data []string `android:"path_host_common"`
101
Chris Parsons79d66a52020-06-05 17:26:16 -0400102 // list of shared library modules that should be installed alongside the test
103 Data_libs []string `android:"arch_variant"`
104
Colin Crossc8caa062021-09-24 16:50:14 -0700105 // list of binary modules that should be installed alongside the test
106 Data_bins []string `android:"arch_variant"`
107
Julien Despreze146e392018-08-02 15:00:46 -0700108 // the name of the test configuration (for example "AndroidTest.xml") that should be
109 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800110 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700111
112 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
113 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800114 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800115
116 // Test options.
117 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700118
119 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
120 // with root permission.
121 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700122
123 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
124 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800125
Dan Shi6ffaaa82019-09-26 11:41:36 -0700126 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
127 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
128 // explicitly.
129 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800130
131 // Add parameterized mainline modules to auto generated test config. The options will be
132 // handled by TradeFed to download and install the specified modules on the device.
133 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700134
135 // Install the test into a folder named for the module in all test suites.
136 Per_testcase_directory *bool
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800137
138 // Install the test's dependencies into a folder named standalone-libs relative to the
139 // test's installation path. ld-library-path will be set to this path in the test's
140 // auto-generated config. This way the dependencies can be used by the test without having
141 // to manually install them to the device. See more details in
142 // go/standalone-native-device-tests.
143 Standalone_test *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144}
145
146func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700147 android.RegisterModuleType("cc_test", TestFactory)
148 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
149 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
150 android.RegisterModuleType("cc_test_host", TestHostFactory)
151 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700152}
153
Patrice Arrudac249c712019-03-19 17:00:29 -0700154// cc_test generates a test config file and an executable binary file to test
155// specific functionality on a device. The executable binary gets an implicit
156// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700157func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800158 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700159 return module.Init()
160}
161
Patrice Arrudac249c712019-03-19 17:00:29 -0700162// cc_test_library creates an archive of files (i.e. .o files) which is later
163// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
164// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700165func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700166 module := NewTestLibrary(android.HostAndDeviceSupported)
167 return module.Init()
168}
169
Patrice Arrudac249c712019-03-19 17:00:29 -0700170// cc_benchmark compiles an executable binary that performs benchmark testing
171// of a specific component in a device. Additional files such as test suites
172// and test configuration are installed on the side of the compiled executed
173// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700174func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700175 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000176 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700177 return module.Init()
178}
179
Patrice Arrudac249c712019-03-19 17:00:29 -0700180// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700181func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800182 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700183 return module.Init()
184}
185
Patrice Arrudac249c712019-03-19 17:00:29 -0700186// cc_benchmark_host compiles an executable binary that performs benchmark
187// testing of a specific component in the host. Additional files such as
188// test suites and test configuration are installed on the side of the
189// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700190func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700191 module := NewBenchmark(android.HostSupported)
192 return module.Init()
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
Jingwen Chen537242c2022-08-24 11:53:27 +0000199func (test *testBinary) testBinary() bool {
200 return true
201}
202
Colin Crossb916a382016-07-29 17:28:03 -0700203type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000204 LinkerProperties TestLinkerProperties
205 InstallerProperties TestInstallerProperties
206 installer *baseInstaller
207 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208}
209
Colin Cross600c9df2016-09-13 12:26:16 -0700210func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000211 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700212}
213
Elliott Hughes90e76f12025-01-16 13:48:31 -0800214func (test *testDecorator) isolated(ctx android.BaseModuleContext) bool {
215 return BoolDefault(test.LinkerProperties.Isolated, false) && !ctx.Windows()
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000216}
217
Jingwen Chen537242c2022-08-24 11:53:27 +0000218// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700219func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700220 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221 return flags
222 }
223
Colin Cross4af21ed2019-11-04 09:37:55 -0800224 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225 if ctx.Host() {
Liana Kazanovab88279f2024-07-15 18:45:23 +0000226 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
228 switch ctx.Os() {
229 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800230 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800232 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800234 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700235 }
236 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800237 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 }
239
240 return flags
241}
242
Colin Crossb916a382016-07-29 17:28:03 -0700243func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700244 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700245 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800246 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000247 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700248 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700249 // The isolated library requires liblog, but adding it
250 // as a static library means unit tests cannot override
251 // liblog functions. Instead make it a shared library
252 // dependency.
253 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 } else {
255 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
256 }
257 }
Colin Crossb916a382016-07-29 17:28:03 -0700258
Colin Cross4d9c2d12016-07-29 12:48:20 -0700259 return deps
260}
261
Colin Crossb916a382016-07-29 17:28:03 -0700262func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000263 return []interface{}{&test.LinkerProperties}
264}
265
266func (test *testDecorator) installerProps() []interface{} {
267 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268}
269
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800270func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
271 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
272 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
273 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
274 }
275}
276
Colin Crossb916a382016-07-29 17:28:03 -0700277func NewTestInstaller() *baseInstaller {
278 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279}
280
Colin Crossb916a382016-07-29 17:28:03 -0700281type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000282 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700283 *binaryDecorator
284 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700285 Properties TestBinaryProperties
286 data []android.DataPath
287 testConfig android.Path
288 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700289}
290
291func (test *testBinary) linkerProps() []interface{} {
292 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
293 props = append(props, &test.Properties)
294 return props
295}
296
Colin Cross37047f12016-12-13 17:06:13 -0800297func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700298 deps = test.testDecorator.linkerDeps(ctx, deps)
299 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400300 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700301 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700302 return deps
303}
304
Colin Crossb916a382016-07-29 17:28:03 -0700305func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
306 flags = test.binaryDecorator.linkerFlags(ctx, flags)
307 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700308
309 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
310 // Host modules already get an rpath specified in linker.go.
311 if !ctx.Host() {
312 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
313 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700314 return flags
315}
316
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800317func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
318 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
319 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
320 }
321 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
322 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
323 if test.testConfig != nil {
324 if _, ok := test.testConfig.(android.WritablePath); ok {
325 moduleInfoJSON.AutoTestConfig = []string{"true"}
326 }
327 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
328 }
329 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
330
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800331 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
332
333 if len(test.InstallerProperties.Test_suites) > 0 {
334 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
335 } else {
336 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
337 }
338
339 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
340 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
341 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
342
343}
344
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000345func (test *testBinary) installerProps() []interface{} {
346 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
347}
348
Colin Crossb916a382016-07-29 17:28:03 -0700349func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400350 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700351 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_common_data)...)
352 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_first_data)...)
Colin Crossb3614422025-02-18 15:18:18 -0800353 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Host_common_data)...)
Chris Parsons216e10a2020-07-09 17:12:52 -0400354
355 for _, dataSrcPath := range dataSrcPaths {
356 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
357 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400358
Yu Liu97880e12025-01-07 19:03:34 +0000359 ctx.VisitDirectDepsProxyWithTag(dataLibDepTag, func(dep android.ModuleProxy) {
Chris Parsons79d66a52020-06-05 17:26:16 -0400360 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000361 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Chris Parsons79d66a52020-06-05 17:26:16 -0400362 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400363 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400364 }
Yu Liu97880e12025-01-07 19:03:34 +0000365 if linkableDep.OutputFile.Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400366 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000367 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
368 RelativeInstallPath: linkableDep.RelativeInstallPath})
Chris Parsons79d66a52020-06-05 17:26:16 -0400369 }
370 })
Yu Liu97880e12025-01-07 19:03:34 +0000371 ctx.VisitDirectDepsProxyWithTag(dataBinDepTag, func(dep android.ModuleProxy) {
Colin Crossc8caa062021-09-24 16:50:14 -0700372 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000373 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Colin Crossc8caa062021-09-24 16:50:14 -0700374 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400375 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700376 }
Yu Liu97880e12025-01-07 19:03:34 +0000377 if linkableDep.OutputFile.Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700378 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000379 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
380 RelativeInstallPath: linkableDep.RelativeInstallPath})
Colin Crossc8caa062021-09-24 16:50:14 -0700381 }
382 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400383
Kiyoung Kimaa394802024-01-08 12:55:45 +0900384 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700385 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800386
Cole Faust21680542022-12-07 18:18:37 -0800387 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
388 TestConfigProp: test.Properties.Test_config,
389 TestConfigTemplateProp: test.Properties.Test_config_template,
390 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
391 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000392 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800393 AutoGenConfig: test.Properties.Auto_gen_config,
394 TestInstallBase: testInstallBase,
395 DeviceTemplate: "${NativeTestConfigTemplate}",
396 HostTemplate: "${NativeHostTestConfigTemplate}",
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800397 StandaloneTest: test.Properties.Standalone_test,
Cole Faust21680542022-12-07 18:18:37 -0800398 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800399
Dan Shi95d19422020-08-15 12:24:26 -0700400 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
401
Colin Cross600c9df2016-09-13 12:26:16 -0700402 test.binaryDecorator.baseInstaller.dir = "nativetest"
403 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800404
405 if !Bool(test.Properties.No_named_install_directory) {
406 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800407 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800408 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
409 }
410
Julien Desprez8908b372021-02-04 10:10:16 -0800411 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800412 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
413 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800414
Spandan Das2f238a92025-01-28 23:39:13 +0000415 if !ctx.Config().KatiEnabled() { // TODO(spandandas): Remove the special case for kati
Spandan Das323631f2025-01-24 21:57:47 +0000416 // Install the test config in testcases/ directory for atest.
Spandan Das323631f2025-01-24 21:57:47 +0000417 c, ok := ctx.Module().(*Module)
418 if !ok {
419 ctx.ModuleErrorf("Not a cc_test module")
420 }
Spandan Das2f238a92025-01-28 23:39:13 +0000421 // Install configs in the root of $PRODUCT_OUT/testcases/$module
Spandan Das323631f2025-01-24 21:57:47 +0000422 testCases := android.PathForModuleInPartitionInstall(ctx, "testcases", ctx.ModuleName()+c.SubName())
Spandan Das2f238a92025-01-28 23:39:13 +0000423 if ctx.PrimaryArch() {
424 if test.testConfig != nil {
425 ctx.InstallFile(testCases, ctx.ModuleName()+".config", test.testConfig)
426 }
427 for _, extraTestConfig := range test.extraTestConfigs {
428 ctx.InstallFile(testCases, extraTestConfig.Base(), extraTestConfig)
429 }
Spandan Das323631f2025-01-24 21:57:47 +0000430 }
Spandan Das2f238a92025-01-28 23:39:13 +0000431 // Install tests and data in arch specific subdir $PRODUCT_OUT/testcases/$module/$arch
432 testCases = testCases.Join(ctx, ctx.Target().Arch.ArchType.String())
Spandan Das323631f2025-01-24 21:57:47 +0000433 ctx.InstallTestData(testCases, test.data)
434 ctx.InstallFile(testCases, file.Base(), file)
435 }
436
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800437 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700438 test.binaryDecorator.baseInstaller.install(ctx, file)
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800439 if Bool(test.Properties.Standalone_test) {
440 packagingSpecsBuilder := depset.NewBuilder[android.PackagingSpec](depset.TOPOLOGICAL)
441
442 ctx.VisitDirectDeps(func(dep android.Module) {
443 deps := android.OtherModuleProviderOrDefault(ctx, dep, android.InstallFilesProvider)
444 packagingSpecsBuilder.Transitive(deps.TransitivePackagingSpecs)
445 })
446
447 for _, standaloneTestDep := range packagingSpecsBuilder.Build().ToList() {
448 if standaloneTestDep.ToGob().SrcPath == nil {
449 continue
450 }
Yifeng Zeng02c56e92025-02-12 13:25:16 -0800451 if standaloneTestDep.SkipInstall() {
452 continue
453 }
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800454 test.binaryDecorator.baseInstaller.installStandaloneTestDep(ctx, standaloneTestDep)
455 }
456 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700457}
458
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000459func getTestInstallBase(useVendor bool) string {
460 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
461 testInstallBase := "/data/local/tmp"
462 if useVendor {
463 testInstallBase = "/data/local/tests/vendor"
464 }
465 return testInstallBase
466}
467
Dan Shiad042502023-08-02 13:53:00 -0700468func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000469 var configs []tradefed.Config
470
471 for _, module := range properties.Test_mainline_modules {
472 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
473 }
Dan Shiad042502023-08-02 13:53:00 -0700474 if device {
475 if Bool(properties.Require_root) {
476 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
477 } else {
478 var options []tradefed.Option
479 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
480 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
481 }
482 if Bool(properties.Disable_framework) {
483 var options []tradefed.Option
484 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
485 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000486 }
487 if isolated {
488 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
489 }
490 if properties.Test_options.Run_test_as != nil {
491 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
492 }
493 for _, tag := range properties.Test_options.Test_suite_tag {
494 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
495 }
496 if properties.Test_options.Min_shipping_api_level != nil {
497 if properties.Test_options.Vsr_min_shipping_api_level != nil {
498 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
499 }
500 var options []tradefed.Option
501 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
502 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
503 }
504 if properties.Test_options.Vsr_min_shipping_api_level != nil {
505 var options []tradefed.Option
506 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
507 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
508 }
509 if properties.Test_options.Min_vndk_version != nil {
510 var options []tradefed.Option
511 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
512 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
513 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
514 }
515 return configs
516}
517
Colin Cross8ff10582023-12-07 13:10:56 -0800518func NewTest(hod android.HostOrDeviceSupported) *Module {
519 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700520 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000521 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700522 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700523
524 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000525 testDecorator: &testDecorator{
526 linker: binary.baseLinker,
527 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700528 },
Colin Crossb916a382016-07-29 17:28:03 -0700529 binaryDecorator: binary,
530 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700531 }
Colin Crossb916a382016-07-29 17:28:03 -0700532 module.compiler = test
533 module.linker = test
534 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700535 return module
536}
537
Colin Crossb916a382016-07-29 17:28:03 -0700538type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000539 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700540 *libraryDecorator
541}
542
Jingwen Chen537242c2022-08-24 11:53:27 +0000543func (test *testLibrary) testLibrary() bool {
544 return true
545}
546
Colin Crossb916a382016-07-29 17:28:03 -0700547func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000548 var props []interface{}
549 props = append(props, test.testDecorator.linkerProps()...)
550 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700551}
552
Colin Cross37047f12016-12-13 17:06:13 -0800553func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700554 deps = test.testDecorator.linkerDeps(ctx, deps)
555 deps = test.libraryDecorator.linkerDeps(ctx, deps)
556 return deps
557}
558
559func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
560 flags = test.libraryDecorator.linkerFlags(ctx, flags)
561 flags = test.testDecorator.linkerFlags(ctx, flags)
562 return flags
563}
564
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800565func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
566 if len(test.InstallerProperties.Test_suites) > 0 {
567 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
568 }
569
570 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
571 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
572}
573
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000574func (test *testLibrary) installerProps() []interface{} {
575 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
576}
577
Colin Cross4d9c2d12016-07-29 12:48:20 -0700578func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800579 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700580 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700581 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000582 testDecorator: &testDecorator{
583 linker: library.baseLinker,
584 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700585 },
Colin Crossb916a382016-07-29 17:28:03 -0700586 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700587 }
Colin Crossb916a382016-07-29 17:28:03 -0700588 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000589 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700590 return module
591}
592
Colin Crosse28f4e22017-04-24 18:10:29 -0700593type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700594 // list of files or filegroup modules that provide data that should be installed alongside
595 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800596 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700597
Colin Crosse28f4e22017-04-24 18:10:29 -0700598 // list of compatibility suites (for example "cts", "vts") that the module should be
599 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700600 Test_suites []string `android:"arch_variant"`
601
602 // the name of the test configuration (for example "AndroidTest.xml") that should be
603 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800604 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700605
606 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
607 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800608 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700609
610 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
611 // with root permission.
612 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700613
614 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
615 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
616 // explicitly.
617 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700618}
619
Colin Crossb916a382016-07-29 17:28:03 -0700620type benchmarkDecorator struct {
621 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700622 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800623 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700624 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700625}
626
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400627func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
628 return true
629}
630
Colin Crosse28f4e22017-04-24 18:10:29 -0700631func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
632 props := benchmark.binaryDecorator.linkerProps()
633 props = append(props, &benchmark.Properties)
634 return props
635}
636
Colin Cross37047f12016-12-13 17:06:13 -0800637func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700638 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700639 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
640 return deps
641}
642
Colin Crossb916a382016-07-29 17:28:03 -0700643func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800644 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
645 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
646 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400647
Dan Shi37ee3b82019-06-06 16:23:32 -0700648 var configs []tradefed.Config
649 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800650 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700651 }
Cole Faust21680542022-12-07 18:18:37 -0800652 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
653 TestConfigProp: benchmark.Properties.Test_config,
654 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
655 TestSuites: benchmark.Properties.Test_suites,
656 Config: configs,
657 AutoGenConfig: benchmark.Properties.Auto_gen_config,
658 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
659 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
660 })
Colin Cross303e21f2018-08-07 16:49:25 -0700661
Colin Cross28690e92017-09-08 16:20:30 -0700662 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
663 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800664 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700665 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700666}
667
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800668func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
669 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
670
671 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
672 if len(benchmark.Properties.Test_suites) > 0 {
673 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
674 } else {
675 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
676 }
677
678 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
679 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
680 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
681 }
682
683 if benchmark.testConfig != nil {
684 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
685 moduleInfoJSON.AutoTestConfig = []string{"true"}
686 }
687 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
688 }
689}
690
Colin Cross4d9c2d12016-07-29 12:48:20 -0700691func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800692 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700693 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700694 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700695
696 benchmark := &benchmarkDecorator{
697 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700698 }
Colin Crossb916a382016-07-29 17:28:03 -0700699 module.linker = benchmark
700 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700701 return module
702}