blob: 2c5c36eace795d8872a1429a3403e2c8fb5a4840 [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
Chris Parsons79d66a52020-06-05 17:26:16 -040097 // list of shared library modules that should be installed alongside the test
98 Data_libs []string `android:"arch_variant"`
99
Colin Crossc8caa062021-09-24 16:50:14 -0700100 // list of binary modules that should be installed alongside the test
101 Data_bins []string `android:"arch_variant"`
102
Julien Despreze146e392018-08-02 15:00:46 -0700103 // the name of the test configuration (for example "AndroidTest.xml") that should be
104 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800105 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700106
107 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
108 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800109 Test_config_template *string `android:"path,arch_variant"`
yelinhsieh9fc60402018-10-01 19:23:14 +0800110
111 // Test options.
112 Test_options TestOptions
Dan Shi37ee3b82019-06-06 16:23:32 -0700113
114 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
115 // with root permission.
116 Require_root *bool
Dan Shi20ccd212019-08-27 10:37:24 -0700117
118 // Add RunCommandTargetPreparer to stop framework before the test and start it after the test.
119 Disable_framework *bool
nelsonli0d7111e2019-09-17 16:35:23 +0800120
Dan Shi6ffaaa82019-09-26 11:41:36 -0700121 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
122 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
123 // explicitly.
124 Auto_gen_config *bool
easoncylee1e3fdcd2020-04-30 10:08:33 +0800125
126 // Add parameterized mainline modules to auto generated test config. The options will be
127 // handled by TradeFed to download and install the specified modules on the device.
128 Test_mainline_modules []string
Colin Crosscfb0f5e2021-09-24 15:47:17 -0700129
130 // Install the test into a folder named for the module in all test suites.
131 Per_testcase_directory *bool
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800132
133 // Install the test's dependencies into a folder named standalone-libs relative to the
134 // test's installation path. ld-library-path will be set to this path in the test's
135 // auto-generated config. This way the dependencies can be used by the test without having
136 // to manually install them to the device. See more details in
137 // go/standalone-native-device-tests.
138 Standalone_test *bool
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139}
140
141func init() {
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700142 android.RegisterModuleType("cc_test", TestFactory)
143 android.RegisterModuleType("cc_test_library", TestLibraryFactory)
144 android.RegisterModuleType("cc_benchmark", BenchmarkFactory)
145 android.RegisterModuleType("cc_test_host", TestHostFactory)
146 android.RegisterModuleType("cc_benchmark_host", BenchmarkHostFactory)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700147}
148
Patrice Arrudac249c712019-03-19 17:00:29 -0700149// cc_test generates a test config file and an executable binary file to test
150// specific functionality on a device. The executable binary gets an implicit
151// static_libs dependency on libgtests unless the gtest flag is set to false.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700152func TestFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800153 module := NewTest(android.HostAndDeviceSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700154 return module.Init()
155}
156
Patrice Arrudac249c712019-03-19 17:00:29 -0700157// cc_test_library creates an archive of files (i.e. .o files) which is later
158// referenced by another module (such as cc_test, cc_defaults or cc_test_library)
159// for archiving or linking.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700160func TestLibraryFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700161 module := NewTestLibrary(android.HostAndDeviceSupported)
162 return module.Init()
163}
164
Patrice Arrudac249c712019-03-19 17:00:29 -0700165// cc_benchmark compiles an executable binary that performs benchmark testing
166// of a specific component in a device. Additional files such as test suites
167// and test configuration are installed on the side of the compiled executed
168// binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700169func BenchmarkFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700170 module := NewBenchmark(android.HostAndDeviceSupported)
Aditya Choudhary87b2ab22023-11-17 15:27:06 +0000171 module.testModule = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700172 return module.Init()
173}
174
Patrice Arrudac249c712019-03-19 17:00:29 -0700175// cc_test_host compiles a test host binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700176func TestHostFactory() android.Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800177 module := NewTest(android.HostSupported)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700178 return module.Init()
179}
180
Patrice Arrudac249c712019-03-19 17:00:29 -0700181// cc_benchmark_host compiles an executable binary that performs benchmark
182// testing of a specific component in the host. Additional files such as
183// test suites and test configuration are installed on the side of the
184// compiled executed binary.
Steven Moreland87c9d7b2017-11-02 21:38:28 -0700185func BenchmarkHostFactory() android.Module {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700186 module := NewBenchmark(android.HostSupported)
187 return module.Init()
188}
189
Chris Parsons216e10a2020-07-09 17:12:52 -0400190func (test *testBinary) dataPaths() []android.DataPath {
Liz Kammer1c14a212020-05-12 15:26:55 -0700191 return test.data
192}
193
Jingwen Chen537242c2022-08-24 11:53:27 +0000194func (test *testBinary) testBinary() bool {
195 return true
196}
197
Colin Crossb916a382016-07-29 17:28:03 -0700198type testDecorator struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000199 LinkerProperties TestLinkerProperties
200 InstallerProperties TestInstallerProperties
201 installer *baseInstaller
202 linker *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -0700203}
204
Colin Cross600c9df2016-09-13 12:26:16 -0700205func (test *testDecorator) gtest() bool {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000206 return BoolDefault(test.LinkerProperties.Gtest, true)
Colin Cross600c9df2016-09-13 12:26:16 -0700207}
208
Elliott Hughes90e76f12025-01-16 13:48:31 -0800209func (test *testDecorator) isolated(ctx android.BaseModuleContext) bool {
210 return BoolDefault(test.LinkerProperties.Isolated, false) && !ctx.Windows()
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000211}
212
Jingwen Chen537242c2022-08-24 11:53:27 +0000213// NOTE: Keep this in sync with cc/cc_test.bzl#gtest_copts
Colin Crossb916a382016-07-29 17:28:03 -0700214func (test *testDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross600c9df2016-09-13 12:26:16 -0700215 if !test.gtest() {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 return flags
217 }
218
Colin Cross4af21ed2019-11-04 09:37:55 -0800219 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_HAS_STD_STRING")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700220 if ctx.Host() {
Liana Kazanovab88279f2024-07-15 18:45:23 +0000221 flags.Local.CFlags = append(flags.Local.CFlags, "-O0", "-g")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
223 switch ctx.Os() {
224 case android.Windows:
Colin Cross4af21ed2019-11-04 09:37:55 -0800225 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_WINDOWS")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 case android.Linux:
Colin Cross4af21ed2019-11-04 09:37:55 -0800227 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 case android.Darwin:
Colin Cross4af21ed2019-11-04 09:37:55 -0800229 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_MAC")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 }
231 } else {
Colin Cross4af21ed2019-11-04 09:37:55 -0800232 flags.Local.CFlags = append(flags.Local.CFlags, "-DGTEST_OS_LINUX_ANDROID")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233 }
234
235 return flags
236}
237
Colin Crossb916a382016-07-29 17:28:03 -0700238func (test *testDecorator) linkerDeps(ctx BaseModuleContext, deps Deps) Deps {
Colin Cross600c9df2016-09-13 12:26:16 -0700239 if test.gtest() {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700240 if ctx.useSdk() && ctx.Device() {
Dan Albert7dd58992018-02-09 15:22:59 -0800241 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main_ndk_c++", "libgtest_ndk_c++")
Trevor Radcliffecd8fd672022-05-03 14:51:16 +0000242 } else if test.isolated(ctx) {
Christopher Ferris9df92d62018-08-21 12:40:08 -0700243 deps.StaticLibs = append(deps.StaticLibs, "libgtest_isolated_main")
Christopher Ferris34cbba62019-07-17 15:46:29 -0700244 // The isolated library requires liblog, but adding it
245 // as a static library means unit tests cannot override
246 // liblog functions. Instead make it a shared library
247 // dependency.
248 deps.SharedLibs = append(deps.SharedLibs, "liblog")
Colin Cross4d9c2d12016-07-29 12:48:20 -0700249 } else {
250 deps.StaticLibs = append(deps.StaticLibs, "libgtest_main", "libgtest")
251 }
252 }
Colin Crossb916a382016-07-29 17:28:03 -0700253
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 return deps
255}
256
Colin Crossb916a382016-07-29 17:28:03 -0700257func (test *testDecorator) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000258 return []interface{}{&test.LinkerProperties}
259}
260
261func (test *testDecorator) installerProps() []interface{} {
262 return []interface{}{&test.InstallerProperties}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700263}
264
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800265func (test *testDecorator) moduleInfoJSON(ctx android.ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
266 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
267 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
268 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
269 }
270}
271
Colin Crossb916a382016-07-29 17:28:03 -0700272func NewTestInstaller() *baseInstaller {
273 return NewBaseInstaller("nativetest", "nativetest64", InstallInData)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700274}
275
Colin Crossb916a382016-07-29 17:28:03 -0700276type testBinary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000277 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700278 *binaryDecorator
279 *baseCompiler
Dan Shi95d19422020-08-15 12:24:26 -0700280 Properties TestBinaryProperties
281 data []android.DataPath
282 testConfig android.Path
283 extraTestConfigs android.Paths
Colin Crossb916a382016-07-29 17:28:03 -0700284}
285
286func (test *testBinary) linkerProps() []interface{} {
287 props := append(test.testDecorator.linkerProps(), test.binaryDecorator.linkerProps()...)
288 props = append(props, &test.Properties)
289 return props
290}
291
Colin Cross37047f12016-12-13 17:06:13 -0800292func (test *testBinary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700293 deps = test.testDecorator.linkerDeps(ctx, deps)
294 deps = test.binaryDecorator.linkerDeps(ctx, deps)
Chris Parsons79d66a52020-06-05 17:26:16 -0400295 deps.DataLibs = append(deps.DataLibs, test.Properties.Data_libs...)
Colin Crossc8caa062021-09-24 16:50:14 -0700296 deps.DataBins = append(deps.DataBins, test.Properties.Data_bins...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700297 return deps
298}
299
Colin Crossb916a382016-07-29 17:28:03 -0700300func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
301 flags = test.binaryDecorator.linkerFlags(ctx, flags)
302 flags = test.testDecorator.linkerFlags(ctx, flags)
Colin Crossb881e322024-05-10 13:24:17 -0700303
304 // Add a default rpath to allow tests to dlopen libraries specified in data_libs.
305 // Host modules already get an rpath specified in linker.go.
306 if !ctx.Host() {
307 flags.Global.LdFlags = append(flags.Global.LdFlags, `-Wl,-rpath,\$$ORIGIN`)
308 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700309 return flags
310}
311
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800312func (test *testBinary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
313 if ctx.Host() && Bool(test.Properties.Test_options.Unit_test) {
314 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "host-unit-tests")
315 }
316 moduleInfoJSON.TestOptionsTags = append(moduleInfoJSON.TestOptionsTags, test.Properties.Test_options.Tags...)
317 moduleInfoJSON.TestMainlineModules = append(moduleInfoJSON.TestMainlineModules, test.Properties.Test_mainline_modules...)
318 if test.testConfig != nil {
319 if _, ok := test.testConfig.(android.WritablePath); ok {
320 moduleInfoJSON.AutoTestConfig = []string{"true"}
321 }
322 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.testConfig.String())
323 }
324 moduleInfoJSON.TestConfig = append(moduleInfoJSON.TestConfig, test.extraTestConfigs.Strings()...)
325
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800326 moduleInfoJSON.DataDependencies = append(moduleInfoJSON.DataDependencies, test.Properties.Data_bins...)
327
328 if len(test.InstallerProperties.Test_suites) > 0 {
329 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
330 } else {
331 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
332 }
333
334 test.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
335 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
336 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
337
338}
339
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000340func (test *testBinary) installerProps() []interface{} {
341 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
342}
343
Colin Crossb916a382016-07-29 17:28:03 -0700344func (test *testBinary) install(ctx ModuleContext, file android.Path) {
Chris Parsons216e10a2020-07-09 17:12:52 -0400345 dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700346 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_common_data)...)
347 dataSrcPaths = append(dataSrcPaths, android.PathsForModuleSrc(ctx, test.Properties.Device_first_data)...)
Chris Parsons216e10a2020-07-09 17:12:52 -0400348
349 for _, dataSrcPath := range dataSrcPaths {
350 test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
351 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400352
Yu Liu97880e12025-01-07 19:03:34 +0000353 ctx.VisitDirectDepsProxyWithTag(dataLibDepTag, func(dep android.ModuleProxy) {
Chris Parsons79d66a52020-06-05 17:26:16 -0400354 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000355 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Chris Parsons79d66a52020-06-05 17:26:16 -0400356 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400357 ctx.ModuleErrorf("data_lib %q is not a LinkableInterface module", depName)
Chris Parsons79d66a52020-06-05 17:26:16 -0400358 }
Yu Liu97880e12025-01-07 19:03:34 +0000359 if linkableDep.OutputFile.Valid() {
Chris Parsons216e10a2020-07-09 17:12:52 -0400360 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000361 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
362 RelativeInstallPath: linkableDep.RelativeInstallPath})
Chris Parsons79d66a52020-06-05 17:26:16 -0400363 }
364 })
Yu Liu97880e12025-01-07 19:03:34 +0000365 ctx.VisitDirectDepsProxyWithTag(dataBinDepTag, func(dep android.ModuleProxy) {
Colin Crossc8caa062021-09-24 16:50:14 -0700366 depName := ctx.OtherModuleName(dep)
Yu Liu97880e12025-01-07 19:03:34 +0000367 linkableDep, ok := android.OtherModuleProvider(ctx, dep, LinkableInfoProvider)
Colin Crossc8caa062021-09-24 16:50:14 -0700368 if !ok {
Ivan Lozano4e5f07d2021-11-04 14:09:38 -0400369 ctx.ModuleErrorf("data_bin %q is not a LinkableInterface module", depName)
Colin Crossc8caa062021-09-24 16:50:14 -0700370 }
Yu Liu97880e12025-01-07 19:03:34 +0000371 if linkableDep.OutputFile.Valid() {
Colin Crossc8caa062021-09-24 16:50:14 -0700372 test.data = append(test.data,
Yu Liu97880e12025-01-07 19:03:34 +0000373 android.DataPath{SrcPath: linkableDep.OutputFile.Path(),
374 RelativeInstallPath: linkableDep.RelativeInstallPath})
Colin Crossc8caa062021-09-24 16:50:14 -0700375 }
376 })
Chris Parsons79d66a52020-06-05 17:26:16 -0400377
Kiyoung Kimaa394802024-01-08 12:55:45 +0900378 testInstallBase := getTestInstallBase(ctx.InVendorOrProduct())
Dan Shiad042502023-08-02 13:53:00 -0700379 configs := getTradefedConfigOptions(ctx, &test.Properties, test.isolated(ctx), ctx.Device())
yelinhsieh9fc60402018-10-01 19:23:14 +0800380
Cole Faust21680542022-12-07 18:18:37 -0800381 test.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
382 TestConfigProp: test.Properties.Test_config,
383 TestConfigTemplateProp: test.Properties.Test_config_template,
384 TestSuites: test.testDecorator.InstallerProperties.Test_suites,
385 Config: configs,
Dan Shiec731432023-05-26 04:21:44 +0000386 TestRunnerOptions: test.Properties.Test_options.Test_runner_options,
Cole Faust21680542022-12-07 18:18:37 -0800387 AutoGenConfig: test.Properties.Auto_gen_config,
388 TestInstallBase: testInstallBase,
389 DeviceTemplate: "${NativeTestConfigTemplate}",
390 HostTemplate: "${NativeHostTestConfigTemplate}",
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800391 StandaloneTest: test.Properties.Standalone_test,
Cole Faust21680542022-12-07 18:18:37 -0800392 })
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800393
Dan Shi95d19422020-08-15 12:24:26 -0700394 test.extraTestConfigs = android.PathsForModuleSrc(ctx, test.Properties.Test_options.Extra_test_configs)
395
Colin Cross600c9df2016-09-13 12:26:16 -0700396 test.binaryDecorator.baseInstaller.dir = "nativetest"
397 test.binaryDecorator.baseInstaller.dir64 = "nativetest64"
Dan Willemsen3340d602016-12-27 14:40:40 -0800398
399 if !Bool(test.Properties.No_named_install_directory) {
400 test.binaryDecorator.baseInstaller.relative = ctx.ModuleName()
Nan Zhang0007d812017-11-07 10:57:05 -0800401 } else if String(test.binaryDecorator.baseInstaller.Properties.Relative_install_path) == "" {
Dan Willemsen3340d602016-12-27 14:40:40 -0800402 ctx.PropertyErrorf("no_named_install_directory", "Module install directory may only be disabled if relative_install_path is set")
403 }
404
Julien Desprez8908b372021-02-04 10:10:16 -0800405 if ctx.Host() && test.gtest() && test.Properties.Test_options.Unit_test == nil {
Julien Desprez3b933d32021-01-14 11:49:15 -0800406 test.Properties.Test_options.Unit_test = proptools.BoolPtr(true)
407 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800408
Spandan Das2f238a92025-01-28 23:39:13 +0000409 if !ctx.Config().KatiEnabled() { // TODO(spandandas): Remove the special case for kati
Spandan Das323631f2025-01-24 21:57:47 +0000410 // Install the test config in testcases/ directory for atest.
Spandan Das323631f2025-01-24 21:57:47 +0000411 c, ok := ctx.Module().(*Module)
412 if !ok {
413 ctx.ModuleErrorf("Not a cc_test module")
414 }
Spandan Das2f238a92025-01-28 23:39:13 +0000415 // Install configs in the root of $PRODUCT_OUT/testcases/$module
Spandan Das323631f2025-01-24 21:57:47 +0000416 testCases := android.PathForModuleInPartitionInstall(ctx, "testcases", ctx.ModuleName()+c.SubName())
Spandan Das2f238a92025-01-28 23:39:13 +0000417 if ctx.PrimaryArch() {
418 if test.testConfig != nil {
419 ctx.InstallFile(testCases, ctx.ModuleName()+".config", test.testConfig)
420 }
Cole Faustec71dfb2025-02-18 13:31:12 -0800421 dynamicConfig := android.ExistentPathForSource(ctx, ctx.ModuleDir(), "DynamicConfig.xml")
422 if dynamicConfig.Valid() {
423 ctx.InstallFile(testCases, ctx.ModuleName()+".dynamic", dynamicConfig.Path())
424 }
Spandan Das2f238a92025-01-28 23:39:13 +0000425 for _, extraTestConfig := range test.extraTestConfigs {
426 ctx.InstallFile(testCases, extraTestConfig.Base(), extraTestConfig)
427 }
Spandan Das323631f2025-01-24 21:57:47 +0000428 }
Spandan Das2f238a92025-01-28 23:39:13 +0000429 // Install tests and data in arch specific subdir $PRODUCT_OUT/testcases/$module/$arch
430 testCases = testCases.Join(ctx, ctx.Target().Arch.ArchType.String())
Spandan Das323631f2025-01-24 21:57:47 +0000431 ctx.InstallTestData(testCases, test.data)
432 ctx.InstallFile(testCases, file.Base(), file)
433 }
434
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800435 test.binaryDecorator.baseInstaller.installTestData(ctx, test.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700436 test.binaryDecorator.baseInstaller.install(ctx, file)
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800437 if Bool(test.Properties.Standalone_test) {
438 packagingSpecsBuilder := depset.NewBuilder[android.PackagingSpec](depset.TOPOLOGICAL)
439
440 ctx.VisitDirectDeps(func(dep android.Module) {
441 deps := android.OtherModuleProviderOrDefault(ctx, dep, android.InstallFilesProvider)
442 packagingSpecsBuilder.Transitive(deps.TransitivePackagingSpecs)
443 })
444
445 for _, standaloneTestDep := range packagingSpecsBuilder.Build().ToList() {
446 if standaloneTestDep.ToGob().SrcPath == nil {
447 continue
448 }
Yifeng Zeng02c56e92025-02-12 13:25:16 -0800449 if standaloneTestDep.SkipInstall() {
450 continue
451 }
Yifeng Zeng1876f5b2025-02-05 17:00:56 -0800452 test.binaryDecorator.baseInstaller.installStandaloneTestDep(ctx, standaloneTestDep)
453 }
454 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700455}
456
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000457func getTestInstallBase(useVendor bool) string {
458 // TODO: (b/167308193) Switch to /data/local/tests/unrestricted as the default install base.
459 testInstallBase := "/data/local/tmp"
460 if useVendor {
461 testInstallBase = "/data/local/tests/vendor"
462 }
463 return testInstallBase
464}
465
Dan Shiad042502023-08-02 13:53:00 -0700466func getTradefedConfigOptions(ctx android.EarlyModuleContext, properties *TestBinaryProperties, isolated bool, device bool) []tradefed.Config {
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000467 var configs []tradefed.Config
468
469 for _, module := range properties.Test_mainline_modules {
470 configs = append(configs, tradefed.Option{Name: "config-descriptor:metadata", Key: "mainline-param", Value: module})
471 }
Dan Shiad042502023-08-02 13:53:00 -0700472 if device {
473 if Bool(properties.Require_root) {
474 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
475 } else {
476 var options []tradefed.Option
477 options = append(options, tradefed.Option{Name: "force-root", Value: "false"})
478 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
479 }
480 if Bool(properties.Disable_framework) {
481 var options []tradefed.Option
482 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.StopServicesSetup", options})
483 }
Kevin Dagostino32edd1a2022-12-04 11:16:42 +0000484 }
485 if isolated {
486 configs = append(configs, tradefed.Option{Name: "not-shardable", Value: "true"})
487 }
488 if properties.Test_options.Run_test_as != nil {
489 configs = append(configs, tradefed.Option{Name: "run-test-as", Value: String(properties.Test_options.Run_test_as)})
490 }
491 for _, tag := range properties.Test_options.Test_suite_tag {
492 configs = append(configs, tradefed.Option{Name: "test-suite-tag", Value: tag})
493 }
494 if properties.Test_options.Min_shipping_api_level != nil {
495 if properties.Test_options.Vsr_min_shipping_api_level != nil {
496 ctx.PropertyErrorf("test_options.min_shipping_api_level", "must not be set at the same time as 'vsr_min_shipping_api_level'.")
497 }
498 var options []tradefed.Option
499 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_shipping_api_level), 10)})
500 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
501 }
502 if properties.Test_options.Vsr_min_shipping_api_level != nil {
503 var options []tradefed.Option
504 options = append(options, tradefed.Option{Name: "vsr-min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Vsr_min_shipping_api_level), 10)})
505 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.ShippingApiLevelModuleController", options})
506 }
507 if properties.Test_options.Min_vndk_version != nil {
508 var options []tradefed.Option
509 options = append(options, tradefed.Option{Name: "min-api-level", Value: strconv.FormatInt(int64(*properties.Test_options.Min_vndk_version), 10)})
510 options = append(options, tradefed.Option{Name: "api-level-prop", Value: "ro.vndk.version"})
511 configs = append(configs, tradefed.Object{"module_controller", "com.android.tradefed.testtype.suite.module.MinApiLevelModuleController", options})
512 }
513 return configs
514}
515
Colin Cross8ff10582023-12-07 13:10:56 -0800516func NewTest(hod android.HostOrDeviceSupported) *Module {
517 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700518 module.multilib = android.MultilibBoth
Aditya Choudhary4b6eaf42023-11-21 15:38:37 +0000519 module.testModule = true
Dan Willemsen1d577e22016-08-29 15:53:15 -0700520 binary.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700521
522 test := &testBinary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000523 testDecorator: &testDecorator{
524 linker: binary.baseLinker,
525 installer: binary.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700526 },
Colin Crossb916a382016-07-29 17:28:03 -0700527 binaryDecorator: binary,
528 baseCompiler: NewBaseCompiler(),
Colin Cross4d9c2d12016-07-29 12:48:20 -0700529 }
Colin Crossb916a382016-07-29 17:28:03 -0700530 module.compiler = test
531 module.linker = test
532 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700533 return module
534}
535
Colin Crossb916a382016-07-29 17:28:03 -0700536type testLibrary struct {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000537 *testDecorator
Colin Crossb916a382016-07-29 17:28:03 -0700538 *libraryDecorator
539}
540
Jingwen Chen537242c2022-08-24 11:53:27 +0000541func (test *testLibrary) testLibrary() bool {
542 return true
543}
544
Colin Crossb916a382016-07-29 17:28:03 -0700545func (test *testLibrary) linkerProps() []interface{} {
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000546 var props []interface{}
547 props = append(props, test.testDecorator.linkerProps()...)
548 return append(props, test.libraryDecorator.linkerProps()...)
Colin Crossb916a382016-07-29 17:28:03 -0700549}
550
Colin Cross37047f12016-12-13 17:06:13 -0800551func (test *testLibrary) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700552 deps = test.testDecorator.linkerDeps(ctx, deps)
553 deps = test.libraryDecorator.linkerDeps(ctx, deps)
554 return deps
555}
556
557func (test *testLibrary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
558 flags = test.libraryDecorator.linkerFlags(ctx, flags)
559 flags = test.testDecorator.linkerFlags(ctx, flags)
560 return flags
561}
562
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800563func (test *testLibrary) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
564 if len(test.InstallerProperties.Test_suites) > 0 {
565 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, test.InstallerProperties.Test_suites...)
566 }
567
568 test.libraryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
569 test.testDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
570}
571
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000572func (test *testLibrary) installerProps() []interface{} {
573 return append(test.baseInstaller.installerProps(), test.testDecorator.installerProps()...)
574}
575
Colin Cross4d9c2d12016-07-29 12:48:20 -0700576func NewTestLibrary(hod android.HostOrDeviceSupported) *Module {
Colin Crossab3b7322016-12-09 14:46:15 -0800577 module, library := NewLibrary(android.HostAndDeviceSupported)
Dan Willemsen28bda512016-08-31 16:32:55 -0700578 library.baseInstaller = NewTestInstaller()
Colin Crossb916a382016-07-29 17:28:03 -0700579 test := &testLibrary{
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000580 testDecorator: &testDecorator{
581 linker: library.baseLinker,
582 installer: library.baseInstaller,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700583 },
Colin Crossb916a382016-07-29 17:28:03 -0700584 libraryDecorator: library,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700585 }
Colin Crossb916a382016-07-29 17:28:03 -0700586 module.linker = test
Trevor Radcliffef389cb42022-03-24 21:06:14 +0000587 module.installer = test
Colin Cross4d9c2d12016-07-29 12:48:20 -0700588 return module
589}
590
Colin Crosse28f4e22017-04-24 18:10:29 -0700591type BenchmarkProperties struct {
Anders Lewisb97e8182017-07-14 15:20:13 -0700592 // list of files or filegroup modules that provide data that should be installed alongside
593 // the test
Colin Cross27b922f2019-03-04 22:35:41 -0800594 Data []string `android:"path"`
Anders Lewisb97e8182017-07-14 15:20:13 -0700595
Colin Crosse28f4e22017-04-24 18:10:29 -0700596 // list of compatibility suites (for example "cts", "vts") that the module should be
597 // installed into.
Julien Despreze146e392018-08-02 15:00:46 -0700598 Test_suites []string `android:"arch_variant"`
599
600 // the name of the test configuration (for example "AndroidTest.xml") that should be
601 // installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800602 Test_config *string `android:"path,arch_variant"`
Jack He33338892018-09-19 02:21:28 -0700603
604 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
605 // should be installed with the module.
Colin Cross27b922f2019-03-04 22:35:41 -0800606 Test_config_template *string `android:"path,arch_variant"`
Dan Shi37ee3b82019-06-06 16:23:32 -0700607
608 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
609 // with root permission.
610 Require_root *bool
Dan Shi6ffaaa82019-09-26 11:41:36 -0700611
612 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
613 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
614 // explicitly.
615 Auto_gen_config *bool
Colin Crosse28f4e22017-04-24 18:10:29 -0700616}
617
Colin Crossb916a382016-07-29 17:28:03 -0700618type benchmarkDecorator struct {
619 *binaryDecorator
Colin Crosse28f4e22017-04-24 18:10:29 -0700620 Properties BenchmarkProperties
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800621 data []android.DataPath
Colin Cross303e21f2018-08-07 16:49:25 -0700622 testConfig android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -0700623}
624
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400625func (benchmark *benchmarkDecorator) benchmarkBinary() bool {
626 return true
627}
628
Colin Crosse28f4e22017-04-24 18:10:29 -0700629func (benchmark *benchmarkDecorator) linkerProps() []interface{} {
630 props := benchmark.binaryDecorator.linkerProps()
631 props = append(props, &benchmark.Properties)
632 return props
633}
634
Colin Cross37047f12016-12-13 17:06:13 -0800635func (benchmark *benchmarkDecorator) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Crossb916a382016-07-29 17:28:03 -0700636 deps = benchmark.binaryDecorator.linkerDeps(ctx, deps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700637 deps.StaticLibs = append(deps.StaticLibs, "libgoogle-benchmark")
638 return deps
639}
640
Colin Crossb916a382016-07-29 17:28:03 -0700641func (benchmark *benchmarkDecorator) install(ctx ModuleContext, file android.Path) {
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800642 for _, d := range android.PathsForModuleSrc(ctx, benchmark.Properties.Data) {
643 benchmark.data = append(benchmark.data, android.DataPath{SrcPath: d})
644 }
Chris Parsons79d66a52020-06-05 17:26:16 -0400645
Dan Shi37ee3b82019-06-06 16:23:32 -0700646 var configs []tradefed.Config
647 if Bool(benchmark.Properties.Require_root) {
nelsonli0d7111e2019-09-17 16:35:23 +0800648 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
Dan Shi37ee3b82019-06-06 16:23:32 -0700649 }
Cole Faust21680542022-12-07 18:18:37 -0800650 benchmark.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
651 TestConfigProp: benchmark.Properties.Test_config,
652 TestConfigTemplateProp: benchmark.Properties.Test_config_template,
653 TestSuites: benchmark.Properties.Test_suites,
654 Config: configs,
655 AutoGenConfig: benchmark.Properties.Auto_gen_config,
656 DeviceTemplate: "${NativeBenchmarkTestConfigTemplate}",
657 HostTemplate: "${NativeBenchmarkTestConfigTemplate}",
658 })
Colin Cross303e21f2018-08-07 16:49:25 -0700659
Colin Cross28690e92017-09-08 16:20:30 -0700660 benchmark.binaryDecorator.baseInstaller.dir = filepath.Join("benchmarktest", ctx.ModuleName())
661 benchmark.binaryDecorator.baseInstaller.dir64 = filepath.Join("benchmarktest64", ctx.ModuleName())
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800662 benchmark.binaryDecorator.baseInstaller.installTestData(ctx, benchmark.data)
Dan Willemsen1d577e22016-08-29 15:53:15 -0700663 benchmark.binaryDecorator.baseInstaller.install(ctx, file)
Colin Crossb916a382016-07-29 17:28:03 -0700664}
665
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800666func (benchmark *benchmarkDecorator) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
667 benchmark.binaryDecorator.moduleInfoJSON(ctx, moduleInfoJSON)
668
669 moduleInfoJSON.Class = []string{"NATIVE_TESTS"}
670 if len(benchmark.Properties.Test_suites) > 0 {
671 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, benchmark.Properties.Test_suites...)
672 } else {
673 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "null-suite")
674 }
675
676 if android.PrefixInList(moduleInfoJSON.CompatibilitySuites, "mts-") &&
677 !android.InList("mts", moduleInfoJSON.CompatibilitySuites) {
678 moduleInfoJSON.CompatibilitySuites = append(moduleInfoJSON.CompatibilitySuites, "mts")
679 }
680
681 if benchmark.testConfig != nil {
682 if _, ok := benchmark.testConfig.(android.WritablePath); ok {
683 moduleInfoJSON.AutoTestConfig = []string{"true"}
684 }
685 moduleInfoJSON.TestConfig = []string{benchmark.testConfig.String()}
686 }
687}
688
Colin Cross4d9c2d12016-07-29 12:48:20 -0700689func NewBenchmark(hod android.HostOrDeviceSupported) *Module {
Colin Cross8ff10582023-12-07 13:10:56 -0800690 module, binary := newBinary(hod)
Colin Crossb916a382016-07-29 17:28:03 -0700691 module.multilib = android.MultilibBoth
Colin Cross28690e92017-09-08 16:20:30 -0700692 binary.baseInstaller = NewBaseInstaller("benchmarktest", "benchmarktest64", InstallInData)
Colin Crossb916a382016-07-29 17:28:03 -0700693
694 benchmark := &benchmarkDecorator{
695 binaryDecorator: binary,
Colin Cross4d9c2d12016-07-29 12:48:20 -0700696 }
Colin Crossb916a382016-07-29 17:28:03 -0700697 module.linker = benchmark
698 module.installer = benchmark
Colin Cross4d9c2d12016-07-29 12:48:20 -0700699 return module
700}